0

I am trying to write a contact program where the user presses a button to add a contact (on a second activity).

I get the error:

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

Here is my main activity 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();
    }

This is my java for my second activity

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();
        }
    });




}

My manifest

 <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>

Here is the full error log:

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) 
Community
  • 1
  • 1

2 Answers2

0

The click is not consumed properly by the list view item. You need to add this line in your list view layout:

android:descendantFocusability="blocksDescendants"    
Avinash Roy
  • 953
  • 1
  • 8
  • 25
0

Put a break point on below line

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

then try to debug the application , as from stack trace, your listview object is null, cross chekc this R.id.listcontact once more or change the id of your listview in your activity_second.xml and then update it in your second activity. This may help you.