在我的应用程序中,主要活动已启动,但在按钮点击时,第二个活动未通过意图启动。
这是我的log cat条目
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文件中我可以看到启动的主要活动,当我点击新任务按钮时,日志cat和强制关闭按钮上面提到的错误出现在模拟器上。
答案 0 :(得分:1)
第16行的问题是NewActivity
。这是你的问题。您在该位置获得了NullPointerException
。
答案 1 :(得分:0)
正如日志所说,你在NewTaskActivity中的findViewById中遇到了问题。 也许你正在寻找一个不存在的View,你正在对它进行一些操作导致NullPointerException。
请确保您在NewTaskActivity中正在膨胀正确的布局,并检查具有您正在使用的ID的视图是否存在于该布局中。