无法将数据传递给其他活动

时间:2013-05-23 19:13:54

标签: android

我不知道我哪里错了。所有信息来源都说这就是它的完成方式。简单的传递到这里就是这样。我需要改变什么才能简单地将一个数字传递给我的第二个活动? 这是我的代码

public class MainActivity extends Activity {

Button returnBtn;
EditText senderText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    returnBtn= (Button) findViewById(R.id.button1);
    senderText = (EditText) findViewById(R.id.editText1);

    returnBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String s = senderText.getText().toString();
            Intent i = new Intent(MainActivity.this, SecondActivity.class);
            i.putExtra("number", s);
            startActivity(i);   
        }
    }); 
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

第二项活动是

package com.example.returningnumbers;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends Activity {

TextView showNum;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_info);
    showNum = (TextView)findViewById(R.id.textView1);
    Intent intent = getIntent();
    String extraData = intent.getExtras().getString("number");
    showNum.setText(extraData);

}

}

Logcat信息

05-23 19:39:38.520: E/Trace(848): error opening trace file: No such file or directory (2)
    05-23 19:39:39.639: D/libEGL(848): loaded /system/lib/egl/libEGL_emulation.so
    05-23 19:39:39.649: D/(848): HostConnection::get() New Host Connection established 0x2a154d08, tid 848
    05-23 19:39:39.893: D/libEGL(848): loaded /system/lib/egl/libGLESv1_CM_emulation.so
    05-23 19:39:39.920: D/libEGL(848): loaded /system/lib/egl/libGLESv2_emulation.so
    05-23 19:39:40.220: W/EGL_emulation(848): eglSurfaceAttrib not implemented
    05-23 19:39:40.260: D/OpenGLRenderer(848): Enabling debug mode 0
    05-23 19:39:45.010: D/AndroidRuntime(848): Shutting down VM
    05-23 19:39:45.030: W/dalvikvm(848): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    05-23 19:39:45.120: E/AndroidRuntime(848): FATAL EXCEPTION: main
    05-23 19:39:45.120: E/AndroidRuntime(848): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.returningnumbers/com.example.returningnumbers.SecondActivity}: java.lang.NullPointerException
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.os.Handler.dispatchMessage(Handler.java:99)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.os.Looper.loop(Looper.java:137)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at java.lang.reflect.Method.invokeNative(Native Method)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at java.lang.reflect.Method.invoke(Method.java:511)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at dalvik.system.NativeStart.main(Native Method)
    05-23 19:39:45.120: E/AndroidRuntime(848): Caused by: java.lang.NullPointerException
    05-23 19:39:45.120: E/AndroidRuntime(848):  at com.example.returningnumbers.SecondActivity.onCreate(SecondActivity.java:20)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.Activity.performCreate(Activity.java:5104)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    05-23 19:39:45.120: E/AndroidRuntime(848):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    05-23 19:39:45.120: E/AndroidRuntime(848):  ... 11 more
    05-23 19:39:45.442: D/dalvikvm(848): GC_CONCURRENT freed 100K, 8% free 2719K/2940K, paused 78ms+118ms, total 548ms
    05-23 19:39:48.102: I/Process(848): Sending signal. PID: 848 SIG: 9`

3 个答案:

答案 0 :(得分:3)

  String extraData = intent.getExtras().getString("nunber");

应该是

  String extraData = intent.getExtras().getString("number"); 

在您的第一项活动中

    i.putExtra("number", s);

在你的第二项活动中

    intent.getExtras().getString("nunber");  // keys don't match 

编辑:

     setContentView(R.layout.second_info); // this should come first
     showNum = (TextView)findViewById(R.id.textView1); // initialize textview
     Intent intent = getIntent();
     String extraData = intent.getExtras().getString("number"); //get value using key
     showNum.setText(extraData); //set text with the value

答案 1 :(得分:1)

也许是拼写错误?

String extraData = intent.getExtras().getString("nunber");

答案 2 :(得分:1)

在致电setContentView()之前,您应致电findViewById()。我个人的偏好是在setContentView()之后立即致电super.onCreate(),以便我知道onCreate()方法的其余部分不太可能中断。