当按下按钮时,我试图将一个数字从 activity1 中的 EditText 框传递给 activity2,然后我希望当在 activity2 中按下操作栏按钮时,该数字出现在 toast 或对话框中。我已经建立了一个意图并编写了我认为应该工作的代码,但它似乎每次都在崩溃 activity2。我输入了应该获取变量的代码行。希望有人能够看到我哪里出错了。我知道在变量中传递数据应该是一项相当容易的任务。
任何帮助,将不胜感激。
Activity1:
public class activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.screen_settings);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final EditText inputTxt1 = (EditText) findViewById(R.id.conPhoneNum);
Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
saveBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNum1 = inputTxt1.getText().toString();
savenum1(phoneNum1);
Intent passIntent = new Intent();
passIntent.putExtra("phoneNum", phoneNum1);
}
});
}
public void savenum1(String phoneNum1) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Saved " + phoneNum1);
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
dlgAlert.create().show();
}
}
活动2:
public class Activity2 extends Activity {
//This is the line that stops the activity from running
String conPhoneNum = getIntent().getExtras().getString("phoneNum");
@SuppressLint("CutPasteId")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
actionBar.show();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
}
public boolean onCreateOptionsMenu1(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_load:
Toast toast= Toast.makeText(this, conPhoneNumber, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, -100);
toast.show();
break;
default:
break;
}
return true;
}
}
日志信息:
07-31 15:51:50.135: E/AndroidRuntime(779): FATAL EXCEPTION: main
07-31 15:51:50.135: E/AndroidRuntime(779): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cam.data /com.cam.data.Activity2}: java.lang.NullPointerException
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2135)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.ActivityThread.access$700 (ActivityThread.java:140)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1237)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.os.Handler.dispatchMessage (Handler.java:99)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.os.Looper.loop(Looper.java:137)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.ActivityThread.main (ActivityThread.java:4921)
07-31 15:51:50.135: E/AndroidRuntime(779): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 15:51:50.135: E/AndroidRuntime(779): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 15:51:50.135: E/AndroidRuntime(779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-31 15:51:50.135: E/AndroidRuntime(779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-31 15:51:50.135: E/AndroidRuntime(779): at dalvik.system.NativeStart.main(Native Method)
07-31 15:51:50.135: E/AndroidRuntime(779): Caused by: java.lang.NullPointerException
07-31 15:51:50.135: E/AndroidRuntime(779): at com.cam.data.Activity2.onCreate(Activity2.java:66)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.Activity.performCreate(Activity.java:5206)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-31 15:51:50.135: E/AndroidRuntime(779): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
更新 1:
conPhoneNum = getIntent().getExtras().getString("phoneNum");
仍在使活动崩溃。我发现如果我删除.getExtras()
它不会崩溃,但它也不会显示数据。我不知道这是否是导致应用程序崩溃的任何指标?也许有人可以帮忙
更新 2:
只是对此的更新,我最终放弃了这个,只使用在全局类中定义的全局变量,任何活动都可以随时使用。除非您绝对需要传递变量,否则这似乎是一种更好的方法来做这种事情。
显然,如果有人能弄清楚为什么它不起作用,那会很好,以防其他人遇到这个问题