0

在我的应用程序中,主要活动已启动,但在单击按钮时,第二个活动并未通过意图启动。

这是我的日志猫条目

08-21 23:29:35.568: D/dalvikvm(680): GC_EXTERNAL_ALLOC freed 47K, 53% free 2544K/5379K, external 716K/1038K, paused 272ms
08-21 23:29:36.319: D/TimesheetMgmt Log(680): -->Main activity launched. 
08-21 23:29:53.908: D/TimesheetMgmt Log(680): -->New Task button clicked. 
08-21 23:29:54.128: D/AndroidRuntime(680): Shutting down VM
08-21 23:29:54.128: W/dalvikvm(680): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-21 23:29:54.198: E/AndroidRuntime(680): FATAL EXCEPTION: main
08-21 23:29:54.198: E/AndroidRuntime(680): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.timesheetmgmt/com.timesheetmgmt.NewTaskActivity}: java.lang.NullPointerException
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.os.Looper.loop(Looper.java:123)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-21 23:29:54.198: E/AndroidRuntime(680):  at java.lang.reflect.Method.invokeNative(Native Method)
08-21 23:29:54.198: E/AndroidRuntime(680):  at java.lang.reflect.Method.invoke(Method.java:507)
08-21 23:29:54.198: E/AndroidRuntime(680):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-21 23:29:54.198: E/AndroidRuntime(680):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-21 23:29:54.198: E/AndroidRuntime(680):  at dalvik.system.NativeStart.main(Native Method)
08-21 23:29:54.198: E/AndroidRuntime(680): Caused by: java.lang.NullPointerException
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.Activity.findViewById(Activity.java:1647)
08-21 23:29:54.198: E/AndroidRuntime(680):  at com.timesheetmgmt.NewTaskActivity.<init>(NewTaskActivity.java:16)
08-21 23:29:54.198: E/AndroidRuntime(680):  at java.lang.Class.newInstanceImpl(Native Method)
08-21 23:29:54.198: E/AndroidRuntime(680):  at java.lang.Class.newInstance(Class.java:1409)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-21 23:29:54.198: E/AndroidRuntime(680):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-21 23:29:54.198: E/AndroidRuntime(680):  ... 11 more
08-21 23:29:58.879: I/Process(680): Sending signal. PID: 680 SIG: 9

主要活动代码:

 package com.timesheetmgmt;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;

 public class MainActivity extends Activity implements OnClickListener{

private static final String DEBUG_TAG= "TimesheetMgmt Log";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(DEBUG_TAG, "-->Main activity launched. ");
    Button btnNewTask=(Button) findViewById(R.id.btnNewTask);
    btnNewTask.setOnClickListener((OnClickListener) this);

}

public void onClick(View v) {
    if(v.getId()==R.id.btnNewTask){
        try {
             Log.d(DEBUG_TAG, "-->New Task button clicked. ");
             Intent in=new Intent(this,NewTaskActivity.class);
             in.putExtra("str", "jjalj new task activity");
             startActivity(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


 }

在 log cat 文件中,我可以看到主要活动已启动,当我单击新任务按钮时,log cat 中的错误和强制关闭按钮出现在模拟器上。

4

2 回答 2

1

问题出NewActivity在第 16 行。那是你的问题。NullPointerException你在那个位置得到一个。

于 2012-08-21T18:20:12.830 回答
0

正如日志所说,您在 NewTaskActivity 的 findViewById 中有问题。也许您正在寻找一个不存在的视图,并且您正在对其进行一些操作,从而导致 NullPointerException。

确保在你的 NewTaskActivity 中你正在膨胀正确的布局,并检查你正在使用的 id 的视图是否存在于该布局中。

于 2012-08-27T10:17:56.237 回答