java.lang.NullPointerException:尝试在空对象引用上调用虚方法..

时间:2017-02-17 05:06:07

标签: android android-studio

我正在尝试编写一个联系程序,用户按下按钮添加联系人(在第二个活动上)。

我收到错误:

  

java.lang.NullPointerException:尝试调用虚方法' android.view.Window $ Callback android.view.Window.getCallback()'在空对象引用上

这是我的主要活动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) 

2 个答案:

答案 0 :(得分:0)

列表视图项未正确使用点击。您需要在列表视图布局中添加此行:

android:descendantFocusability="blocksDescendants"    

答案 1 :(得分:0)

在下面划线

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

然后尝试调试应用程序,从堆栈跟踪,你的listview对象为null,再次交叉此R.id.listcontact或更改activity_second.xml中listview的id,然后在第二个更新它活动。这可能会对你有帮助。