好了,所以我有兩個活動,我試圖打開點擊一個按鈕活動插入。 但它給不幸的是活動意外停止的消息或任何..從主要活動代碼的Android空對象引用
部分,其中按鈕調用的意圖:
//The intent typed earlier in the activity
Intent myIntent = new Intent(MainActivity.this, Insert.class);
private OnClickListener onClick() {
return new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addcolumnbutton:
if(value==k||value==10)
{
Toast.makeText(getApplicationContext(), "Maximum number of Columns created",
Toast.LENGTH_SHORT).show();
}
else {
k++;
mLayout.addView(createNewLayout());
}
break;
case R.id.createtablebutton:
if(value==k) {
MainActivity.this.startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "Incorrect number of Columns created",
Toast.LENGTH_SHORT).show();
}
break;
}
}
};
}
我logcat的位置:
03-02 17:21:34.342 12789-12789/com.cambiopune.tosdatabase D/AndroidRuntime﹕ Shutting down VM
03-02 17:21:34.345 12789-12789/com.cambiopune.tosdatabase E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cambiopune.tosdatabase, PID: 12789
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cambiopune.tosdatabase/com.cambiopune.tosdatabase.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:131)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:3996)
at com.cambiopune.tosdatabase.MainActivity.<init>(MainActivity.java:34)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
正如我理解你的代碼,你必須刪除此:'私人OnClickListener的onClick(){ 返回新OnClickListener(){' – Psypher 2015-03-02 12:17:57
試圖調用虛擬方法的Java .lang.String android.content.Context.getPackageName()'在空對象參考 在android.content.ContextWrapper.getPackageName(ContextWrapper.java:131) 我認爲錯誤是在別的地方 – 2015-03-02 12:24:13
@Ranjith我試試它出來了,如果它工作,我會標記你的答案正確.. – MihirK98 2015-03-03 13:41:20