2

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

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);

}

}

日志信息

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`
4

3 回答 3

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
于 2013-05-23T19:17:11.967 回答
1

也许是错字号码?

String extraData = intent.getExtras().getString("nunber");
于 2013-05-23T19:16:18.443 回答
1

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

于 2013-05-23T19:56:34.293 回答