我得到了活动 A 和活动 B。假设活动 A 应该将数据从 EditText 发送到活动 B 到 TextView 我试过了,但它唯一要做的就是让应用程序崩溃......
代码时间:
活动A代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butik);
addButtonListener();
}
private void addButtonListener() {
// find and activate elements
Button soeg = (Button) findViewById(R.id.soeg_butik_knap);
final EditText indtast_butik = (EditText) findViewById(R.id.find_butik);
soeg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// send you to activity B with the EditText data
Intent send_butik = new Intent(Butik.this, Butik_resultat.class);
String butik = indtast_butik.getText().toString();
send_butik.putExtra("BUTIK", butik);
startActivity(send_butik);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.butik, menu);
return true;
活动 B 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vare_resultat);
//receive data
TextView modtag_butik = (TextView) findViewById(R.id.modtag_butik);
modtag_butik.setText("Du søgte efter denne butik: " + getIntent().getExtras().getString("BUTIK"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.vare_resultat, menu);
return true;
}
这是 LogCat 输出:
05-25 14:59:10.446: E/AndroidRuntime(1506): FATAL EXCEPTION: main
05-25 14:59:10.446: E/AndroidRuntime(1506): java.lang.NullPointerException
05-25 14:59:10.446: E/AndroidRuntime(1506): at com.example.test.Butik$1.onClick(Butik.java:33)
05-25 14:59:10.446: E/AndroidRuntime(1506): at android.view.View.performClick(View.java:4204)
05-25 14:59:10.446: E/AndroidRuntime(1506): at android.view.View$PerformClick.run(View.java:17355)
05-25 14:59:10.446: E/AndroidRuntime(1506): at android.os.Handler.handleCallback(Handler.java:725)
05-25 14:59:10.446: E/AndroidRuntime(1506): at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 14:59:10.446: E/AndroidRuntime(1506): at android.os.Looper.loop(Looper.java:137)
05-25 14:59:10.446: E/AndroidRuntime(1506): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-25 14:59:10.446: E/AndroidRuntime(1506): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 14:59:10.446: E/AndroidRuntime(1506): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 14:59:10.446: E/AndroidRuntime(1506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-25 14:59:10.446: E/AndroidRuntime(1506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-25 14:59:10.446: E/AndroidRuntime(1506): at dalvik.system.NativeStart.main(Native Method)
05-25 14:59:10.606: D/dalvikvm(1506): GC_CONCURRENT freed 119K, 8% free 3053K/3292K, paused 9ms+10ms, total 141ms
提前致谢!