2015-03-02 126 views
0

好了,所以我有兩個活動,我試圖打開點擊一個按鈕活動插入。 但它給不幸的是活動意外停止的消息或任何..從主要活動代碼的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) 

+1

正如我理解你的代碼,你必須刪除此:'私人OnClickListener的onClick(){ 返回新OnClickListener(){' – Psypher 2015-03-02 12:17:57

+0

試圖調用虛擬方法的Java .lang.String android.content.Context.getPackageName()'在空對象參考 在android.content.ContextWrapper.getPackageName(ContextWrapper.java:131) 我認爲錯誤是在別的地方 – 2015-03-02 12:24:13

+0

@Ranjith我試試它出來了,如果它工作,我會標記你的答案正確.. – MihirK98 2015-03-03 13:41:20

回答

1

在你的代碼只需刪除該行:

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; 
     } 
    } 
+0

一個d您應該在活動 – 2015-03-02 12:25:45

+0

中的點擊偵聽器上實現或者爲偵聽器創建一個內部類。你有兩種方式來實現它的地方之前,你可以覆蓋'的onClick(視圖v)' – 2015-03-02 12:27:56

+0

@Ranjith生病嘗試,並儘快給您 – MihirK98 2015-03-03 13:39:24