我正在编写一个谷歌地图应用程序,我正在尝试实现一个图像按钮,将地图类型从普通更改为混合。
我在 onCreate 实例中声明了我的按钮:
ImageButton changeBtn =(ImageButton) findViewById(R.id.changeBtn);
changeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
changeMapType();
}
});
这是我的函数 changeMapType(); :
private void changeMapType(){
if(MapTypeInt == 0){
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
changeBtn.setBackgroundResource(R.drawable.satellite);
MapTypeInt = 1;
} else if (MapTypeInt == 1) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
changeBtn.setBackgroundResource(R.drawable.normal_map);
MapTypeInt = 0;
}
}
现在问题是,我不能在 onCreate 实例之外使用 imageButton“changeBtn”,当我在程序代码的顶部声明按钮时,它会导致程序崩溃。有人对此有解决方案吗?
Logcat 在类级别声明时:
01-10 10:22:00.555: E/AndroidRuntime(2872): FATAL EXCEPTION: main
01-10 10:22:00.555: E/AndroidRuntime(2872): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidbasic12/com.example.androidbasic12.map}: java.lang.NullPointerException
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.os.Looper.loop(Looper.java:137)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-10 10:22:00.555: E/AndroidRuntime(2872): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 10:22:00.555: E/AndroidRuntime(2872): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 10:22:00.555: E/AndroidRuntime(2872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-10 10:22:00.555: E/AndroidRuntime(2872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-10 10:22:00.555: E/AndroidRuntime(2872): at dalvik.system.NativeStart.main(Native Method)
01-10 10:22:00.555: E/AndroidRuntime(2872): Caused by: java.lang.NullPointerException
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.Activity.findViewById(Activity.java:1839)
01-10 10:22:00.555: E/AndroidRuntime(2872): at com.example.androidbasic12.map.<init>(map.java:24)
01-10 10:22:00.555: E/AndroidRuntime(2872): at java.lang.Class.newInstanceImpl(Native Method)
01-10 10:22:00.555: E/AndroidRuntime(2872): at java.lang.Class.newInstance(Class.java:1319)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-10 10:22:00.555: E/AndroidRuntime(2872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-10 10:22:00.555: E/AndroidRuntime(2872): ... 11 more
当我尝试在类级别声明它时的代码:
public class map extends FragmentActivity{
private static final LatLng SYDNEY = new LatLng(-33.88,151.21);
public static final LatLng sala = new LatLng(59.91602, 16.594108);
int travel = 0;
int MapTypeInt = 0;
private GoogleMap mMap;
GoogleMapOptions options = new GoogleMapOptions();
ImageButton changeBtn =(ImageButton) findViewById(R.id.changeBtn);