在活动之间推送数据。出了什么问题?

时间:2013-05-25 14:35:08

标签: java android android-intent android-edittext

我得到了活动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

提前致谢!

1 个答案:

答案 0 :(得分:0)

检查您是否在setContentView()

中使用了正确的XML布局文件