选择上下文菜单项时出现空指针异常

时间:2013-01-18 23:37:34

标签: android nullpointerexception android-contextmenu

我有一个项目列表。当我长按一个项目时,我会打开一个上下文菜单,其中有3个选项可以编辑,删除和添加到收藏夹。但是,当我从上下文菜单中选择第一项来编辑联系人时,我收到以下错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.oopca2 / com.example.oopca2.EditActivity}:java.lang.NullPointerException

这是我的代码:

public class MainActivity extends ListActivity {

ActionBar ab;
ContactsListAdapter adapter;
ContactsDbAdapter dbAdapter;
List<Contact> contacts;
Context context;
private Contact selectedPerson;

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

    ab = this.getActionBar();
    ab.setDisplayShowTitleEnabled(true);
    ab.setTitle("All Contacts");

    dbAdapter = new ContactsDbAdapter(this);
    dbAdapter.open();
    contacts = dbAdapter.getAllContacts();
    adapter = new ContactsListAdapter(this, contacts, dbAdapter);
    this.setListAdapter(adapter);
    registerForContextMenu(this.getListView());

    context = this.getApplicationContext();

}

@Override
protected void onResume() {
    super.onResume();
    adapter.updateList();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Contact c = contacts.get(position);
    Toast.makeText(this, c.getName().toString() + " Clicked!", Toast.LENGTH_SHORT).show();
}

//Context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    this.selectedPerson = contacts.get(info.position);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.activity_context, menu);
    menu.setHeaderIcon(R.drawable.setting);
    menu.setHeaderTitle(this.selectedPerson.getName().toString() + " - Options");
}

@Override
public boolean onContextItemSelected(MenuItem item) {

AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

    long rowId = (info.id);

    switch(item.getItemId()){
    case R.id.menu_edit:
        Toast.makeText(this, "Edit " + rowId, Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(), EditActivity.class);
        i.putExtra("row", rowId);
        startActivity(i);

        return true;
    case R.id.menu_delete:
        Toast.makeText(this, "Delete " +  rowId , Toast.LENGTH_SHORT).show();
        dbAdapter.deleteContact(rowId);
        return true;

    case R.id.menu_favs:
        Toast.makeText(this, "Added to Favourites!" +  rowId, Toast.LENGTH_SHORT).show();

        return true;
    }

    return false;
}

//Options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
    case R.id.menu_add:
        Toast.makeText(this, "Add Contact", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(context, AddActivity.class);
        startActivity(i);
        return true;

    case R.id.menu_fav:
        Toast.makeText(this, "View Favourites", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}

}

这是EditActivity.class:

public class EditActivity extends Activity{

EditText name;
EditText number;
EditText email;
CheckBox fav;
ActionBar ab;
Button updateBtn;
ContactsDbAdapter dbAdapter;
Contact c;
Intent i;
Long rowId;
boolean favourite;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_edit);

    ab = this.getActionBar();
    ab.setTitle("Edit Contact");

    dbAdapter.open();

    i = getIntent();
    rowId = i.getExtras().getLong("row");
    c = dbAdapter.getContact(rowId);

    name = (EditText)findViewById(R.id.editText1);
    number = (EditText)findViewById(R.id.editText2);
    email = (EditText)findViewById(R.id.editText3);
    fav = (CheckBox)findViewById(R.id.checkBox1);


    name.setText(c.getName().toString());
    number.setText(c.getNumber().toString());
    email.setText(c.getEmail().toString());
    fav.setChecked(c.getFavourite());

    updateBtn = (Button)findViewById(R.id.editBtn);
    updateBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(fav.isChecked()){
                favourite = true;
            }else{
                favourite = false;
            }

            dbAdapter.updateContact(name.getText().toString(), number.getText().toString(), email.getText().toString(), favourite, rowId);
            dbAdapter.close();
            finish();
        }
    });
}

//Options menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
        case R.id.menu_add:
            Toast.makeText(this, "Add Contact", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(getApplicationContext(), AddActivity.class);
            startActivity(i);
            return true;

        case R.id.menu_fav:
            Toast.makeText(this, "View Favourites", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }

}

我不知道我哪里出错了。任何帮助将非常感激!感谢。

LOG:

01-18 23:48:16.319: E/AndroidRuntime(23708): FATAL EXCEPTION: main
01-18 23:48:16.319: E/AndroidRuntime(23708): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oopca2/com.example.oopca2.EditActivity}: java.lang.NullPointerException
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.os.Looper.loop(Looper.java:154)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.ActivityThread.main(ActivityThread.java:4944)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at java.lang.reflect.Method.invokeNative(Native Method)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at java.lang.reflect.Method.invoke(Method.java:511)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at dalvik.system.NativeStart.main(Native Method)
01-18 23:48:16.319: E/AndroidRuntime(23708): Caused by: java.lang.NullPointerException
01-18 23:48:16.319: E/AndroidRuntime(23708):    at com.example.oopca2.EditActivity.onCreate(EditActivity.java:50)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.Activity.performCreate(Activity.java:4531)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
01-18 23:48:16.319: E/AndroidRuntime(23708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
01-18 23:48:16.319: E/AndroidRuntime(23708):    ... 11 more

edit_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".EditActivity" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="17dp"
    android:text="@string/favLabel" />

<Button
    android:id="@+id/editBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/checkBox1"
    android:text="@string/editBtn" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="22dp"
    android:text="@string/emailLabel" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:layout_toRightOf="@+id/textView2"
    android:ems="10"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="phone" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="textEmailAddress" >

</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="22dp"
    android:text="@string/numberLabel" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentLeft="true"
    android:text="@string/nameLabel" />

1 个答案:

答案 0 :(得分:1)

您只是忘记初始化dbAdapter

dbAdapter = new ContactsDbAdapter(); // Something like this
dbAdapter.open();