android intent崩溃了我的程序

时间:2012-03-12 01:26:59

标签: android android-intent

我正在开发一个程序,我正在使用按钮中的意图来切换活动。

它一直在崩溃我的应用程序(它让我无法恢复活动,java.lang.nullPointer异常错误),我似乎无法弄清楚为什么我的代码在下面。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Intent i= new Intent(Main.this,LocationMonitor.class);
            startActivity(i);
        }
    });

    final Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            finish();
        }
    });
}

以下是正在推出的活动:

public class LocationMonitor extends Activity implements LocationListener {
    private static final String TAG = "LocationDemo";
    private static final String[] S = { "Out of Service",
        "Temporarily Unavailable", "Available" };

    private TextView output;
    private LocationManager locationManager;
    private String bestProvider;
    private double myLat;
    private double myLong;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locationlayout);


            output.append("\n\n" + location.toString());
    }

}

错误日志帖子:

  03-11 22:28:42.785: E/AndroidRuntime(6817): Uncaught handler: thread main exiting due to uncaught exception
  03-11 22:28:42.796: E/AndroidRuntime(6817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.POG_version1/com.android.POG_version1.LocationMonitor}: java.lang.NullPointerException
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.ActivityThread.access$2200(ActivityThread.java:119)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.os.Handler.dispatchMessage(Handler.java:99)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.os.Looper.loop(Looper.java:123)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.ActivityThread.main(ActivityThread.java:4363)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at java.lang.reflect.Method.invokeNative(Native Method)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at java.lang.reflect.Method.invoke(Method.java:521)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at dalvik.system.NativeStart.main(Native Method)
  03-11 22:28:42.796: E/AndroidRuntime(6817): Caused by: java.lang.NullPointerException
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at com.android.POG_version1.LocationMonitor.onCreate(LocationMonitor.java:60)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  03-11 22:28:42.796: E/AndroidRuntime(6817):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

1 个答案:

答案 0 :(得分:0)

确保在首次启动活动时正确初始化locationManager。当您尝试拨打电话NullPointerException时,您的实施会抛出locationManager.requestLocationUpdates()