gpt4 book ai didi

android - 按返回按钮时出错

转载 作者:行者123 更新时间:2023-11-29 17:32:23 26 4
gpt4 key购买 nike

在我的 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

我的 Activity 代码是这样的:

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

我在之前的 Activity 中通过以下代码运行 Activity 。在之前的 Activity 中是一个 ListView ,通过单击 ListView 项目显示对话框,然后按下此对话框中的按钮运行以下代码:

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

请帮忙

最佳答案

你的包是空的,因为你在设置所有这些资源之前调用了 Activity

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 时,请始终检查

Bundle extras=getIntent().getExtras();
if(extras!=null){
//Do something
}else{
finish();
}

关于android - 按返回按钮时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32368852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com