2017-02-17 88 views
0

我想寫一個聯繫程序,用戶按下按鈕添加一個聯繫人(第二個活動)。我不斷收到同樣的錯誤:常見的android錯誤,其他答案沒有幫助! java.lang.NullPointerException:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 

我已經看了很多其他的答案,但我就是無法找到爲什麼我的代碼,特別是不能正常工作。

這是我的主要活動的java:

public class MainActivity extends AppCompatActivity { 

private ListView lvContact; 
private Button bt; 
@Override 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final List<ContactBook> listContactBook = new ArrayList<ContactBook>(); 
    lvContact = (ListView)findViewById(R.id.listcontact); 
    final ContactBookAdapter adapter = new ContactBookAdapter(this, listContactBook); 
    lvContact.setAdapter(adapter); 

    lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Intent intent = new Intent(getApplicationContext(), secondActivity.class); 
      startActivity(intent); 

     } 
    }); 
    try { 
     bt = (Button) findViewById(R.id.add); 

     bt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(getApplicationContext(), secondActivity.class); 
       startActivity(intent); 
      } 

     }); 
    } 
    catch(NullPointerException e){ 
     System.out.checkError(); 
    } 

這是我的Java爲我的第二個活動

public class secondActivity extends AppCompatActivity { 
private ListView lvContact; 
private Button bt; 
private EditText first; 
private EditText mid; 
private EditText last; 
private EditText phone; 
private EditText birth; 
private EditText meeting; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    final List<ContactBook> listContactBook = new ArrayList<ContactBook>(); 
    lvContact = (ListView)findViewById(R.id.listcontact); 
    final ContactBookAdapter adapter = new ContactBookAdapter(this, listContactBook); 
    lvContact.setAdapter(adapter); 

    //get data 
    first = (EditText)findViewById(R.id.firstName); 
    mid = (EditText) findViewById(R.id.midInitial); 
    last = (EditText) findViewById(R.id.lName); 
    phone = (EditText) findViewById(R.id.pNumber); 
    birth = (EditText) findViewById(R.id.bDay); 
    meeting = (EditText) findViewById(R.id.fMeeting); 


    bt = (Button) findViewById(R.id.save); 

    bt.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String newf, newi, newl, newn, newb, newd; 

      newf = first.getText().toString(); 
      newi = mid.getText().toString(); 
      newl = last.getText().toString(); 
      newn = phone.getText().toString(); 
      newb = birth.getText().toString(); 
      newd = meeting.getText().toString(); 

      listContactBook.add(new ContactBook(newf, newi, newl, newn, newb, newd)); 
      adapter.notifyDataSetChanged(); 
     } 
    }); 




} 

我的清單

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".secondActivity" 
     android:label="@string/title_activity_second" 
     android:theme="@style/AppTheme.NoActionBar"></activity> 
</application> 

我知道這是一個非常常見的錯誤我應該能夠找到它,但由於某種原因,我不能,無論我嘗試什麼。

謝謝!

編輯: 這裏是完全錯誤日誌:

I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.bignerdranch.android.contactapp, PID: 16741 
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.contactapp/com.bignerdranch.android.contactapp.secondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440) 
        at android.app.ActivityThread.access$800(ActivityThread.java:162) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5422) 
        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:914) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
        at com.bignerdranch.android.contactapp.secondActivity.onCreate(secondActivity.java:37) 
        at android.app.Activity.performCreate(Activity.java:6057) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)  
        at android.app.ActivityThread.access$800(ActivityThread.java:162)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:135)  
        at android.app.ActivityThread.main(ActivityThread.java:5422)  
        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:914)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)  
+1

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –

+0

顯示完整的錯誤日誌。 – TruongHieu

+0

用完整的錯誤日誌編輯的問題! –

回答

0

的點擊不受列表視圖項正確消費。您需要在您的列表視圖佈局中加入這一行:

android:descendantFocusability="blocksDescendants"  
+0

我試過了,它不幸有相同的錯誤。 –

0

把一個破發點下面一行

lvContact = (ListView)findViewById(R.id.listcontact); 

然後嘗試調試應用程序,從堆棧跟蹤,你的列表視圖對象爲空,再次交叉這個R.id.listcontact,或者在您的activity_second.xml中更改您的列表視圖的ID,然後在第二個活動中更新它。這可能會幫助你。

相關問題