在我的android应用程序中按下后退按钮在logcat中显示此错误:
09-01 14:07:42.995: E/AndroidRuntime(6279): FATAL EXCEPTION: main
09-01 14:07:42.995: E/AndroidRuntime(6279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabege/com.example.nabege.Edit_Collection_Activity}: java.lang.NullPointerException
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.os.Looper.loop(Looper.java:137)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-01 14:07:42.995: E/AndroidRuntime(6279): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 14:07:42.995: E/AndroidRuntime(6279): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 14:07:42.995: E/AndroidRuntime(6279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-01 14:07:42.995: E/AndroidRuntime(6279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-01 14:07:42.995: E/AndroidRuntime(6279): at dalvik.system.NativeStart.main(Native Method)
09-01 14:07:42.995: E/AndroidRuntime(6279): Caused by: java.lang.NullPointerException
09-01 14:07:42.995: E/AndroidRuntime(6279): at com.example.nabege.Edit_Collection_Activity.onCreate(Edit_Collection_Activity.java:30)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.Activity.performCreate(Activity.java:4465)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-01 14:07:42.995: E/AndroidRuntime(6279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-01 14:07:42.995: E/AndroidRuntime(6279): ... 11 more
我的活动代码如下:
public class Edit_Collection_Activity extends ActionBarActivity {
private DB_Nabege_helper nabege_db = new DB_Nabege_helper(this);
private EditText txt_update_collection;
private Button btn_update_collection;
private Button btn_cancel_update_collection;
private int id_collection;
private String name_collection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit__collection);
Bundle extra = getIntent().getExtras();
id_collection = extra.getInt("id_collection");
name_collection = extra.getString("name_collection");
//
txt_update_collection = (EditText) findViewById(R.id.editText_update_collection);
txt_update_collection.setText(name_collection);
// update collection
btn_update_collection = (Button) findViewById(R.id.btn_update_collection);
btn_update_collection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nabege_db.open();
nabege_db.update_colletion(id_collection, txt_update_collection.getText().toString());
nabege_db.close();
Toast.makeText(getApplicationContext(), "تغییرات با موفقیت ثبت شد.", Toast.LENGTH_SHORT).show();
//go To pre Activity
goback();
}
});
//go back
btn_cancel_update_collection =(Button)findViewById(R.id.btn_update_collection_cancel);
btn_cancel_update_collection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goback();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit__collection_, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void goback() {
super.finish();
}
}
我通过之前活动中的以下代码运行活动。 在之前的活动中是一个列表视图,通过单击列表视图项目显示Dialog然后按此对话框中的按钮运行以下代码:
edit_collection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent goToEditCollectionActivity = new Intent(Collection_List_Activity.this, Edit_Collection_Activity.class);
startActivity(goToEditCollectionActivity);
goToEditCollectionActivity.putExtra("id_collection", id_tbl_collection[index_collection]);
goToEditCollectionActivity.putExtra("name_collection", name_collection_tbl_collection[index_collection]);
menu_collection_listDialog.cancel();
startActivity(goToEditCollectionActivity);
}
});
请帮忙
答案 0 :(得分:3)
您的捆绑包是空的,因为您在设置所有这些资源之前正在调用活动
edit_collection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent goToEditCollectionActivity = new Intent(Collection_List_Activity.this, Edit_Collection_Activity.class);
goToEditCollectionActivity.putExtra("id_collection", id_tbl_collection[index_collection]);
goToEditCollectionActivity.putExtra("name_collection", name_collection_tbl_collection[index_collection]);
menu_collection_listDialog.cancel();
startActivity(goToEditCollectionActivity);
}
});
此外,无论何时必须处理捆绑包,请务必检查
Bundle extras=getIntent().getExtras();
if(extras!=null){
//Do something
}else{
finish();
}