我试图在按下按钮时将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;
}
}
LogCat信息:
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:
只是对此的更新,我最终废弃了这个并且只使用全局类中定义的全局变量,任何时候都可以被任何活动使用。除非你绝对需要传递变量,否则它似乎是一种更好的方法来做这种事情
显然,如果有人能够弄清楚为什么它不起作用会很好,这会让其他人遇到这个问题
答案 0 :(得分:2)
替换此
Intent passIntent = new Intent();
passIntent.putExtra("phoneNum", phoneNum1);
通过
Intent passIntent = new Intent(Activity1.this,Activity2.class);
passIntent.putExtra("phoneNum", phoneNum1);
startActivity(passIntent);
进入Activity2:
将conPhoneNum
声明为类成员
String conPhoneNum ;
在onCreate
conPhnoneNum = getIntent().getExtras().getString("phoneNum");
然后
Toast toast= Toast.makeText(this, conPhoneNumber, Toast.LENGTH_SHORT);
答案 1 :(得分:0)
移动此
String conPhoneNum = getIntent().getExtras().getString("phoneNum");
到onCreate()
或至少是初始化
String conPhoneNum;
@SuppressLint("CutPasteId")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
conPhoneNum = getIntent().getExtras().getString("phoneNum"); // here
ActionBar actionBar = getActionBa
getIntent()
会在null
创建之前返回Activity
,因此您不得在之前调用它。
答案 2 :(得分:0)
在您的第一个活动中:
Intent passIntent = new Intent();
passIntent.putExtra("phoneNum", phoneNum1);
startActivity(intent)
在第二项活动中:
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
String conPhoneNum = getIntent().getExtras().getString("phoneNum");