将数据从活动A传递到B:应用程序意外停止

时间:2012-03-21 13:47:56

标签: java android

1.我试图将一个字符串数组从一个活动传递到另一个活动但应用程序正在给“应用程序意外停止”。我是Android的新手,我不知道该怎么做。

2.你能告诉我如何解决这些运行时错误以及如何使用logcat我没有   想法?

代码活动A:

private OnClickListener mylistener=new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        intent=new Intent(MsgSmsActivity.this,MsgSmsActivity2.class);
        MsgSmsActivity.this.startActivity(intent);
        intent.putExtra("bodyText", body);
        intent.putExtra("numbersText", number);

    }
};

代码活动B:

String[] arr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // TODO Auto-generated method stub
    arr=new String[3];
    arr=getIntent().getExtras().getStringArray("bodyText");
    TextView tx=new TextView(this);
    tx.setText(arr[0]);
    setContentView(tx); 
}

“body”和“number”是字符串数组。

这是我的LogCat:

03-21 13:07:03.477: ERROR/Zygote(32): setreuid() failed. errno: 2<br>
03-21 13:07:14.976: ERROR/Zygote(32): setreuid() failed. errno: 17<br>
03-21 13:07:17.116: ERROR/BatteryService(76): usbOnlinePath not found<br>
03-21 13:07:17.116: ERROR/BatteryService(76): batteryVoltagePath not found<br>
03-21 13:07:17.116: ERROR/BatteryService(76): batteryTemperaturePath not found<br>
03-21 13:07:17.156: ERROR/SurfaceFlinger(76): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake<br>
03-21 13:07:17.386: ERROR/SensorService(76): couldn't open device for module sensors (Invalid argument)<br>
03-21 13:07:43.016: ERROR/System(76): Failure starting core service<br>
03-21 13:07:43.016: ERROR/System(76): java.lang.SecurityException<br>
03-21 13:07:43.016: ERROR/System(76):     at android.os.BinderProxy.transact(Native Method)<br>
03-21 13:07:43.016: ERROR/System(76):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)<br>
03-21 13:07:43.016: ERROR/System(76):     at android.os.ServiceManager.addService(ServiceManager.java:72)<br>
03-21 13:07:43.016: ERROR/System(76):     at com.android.server.ServerThread.run(SystemServer.java:206)<br>
03-21 13:07:43.056: ERROR/EventHub(76): could not get driver version for /dev/input/mouse0, Not a typewriter<br>
03-21 13:07:43.056: ERROR/EventHub(76): could not get driver version for /dev/input/mice, Not a typewriter<br>
03-21 13:07:43.756: ERROR/SoundPool(76): error loading /system/media/audio/ui/Effect_Tick.ogg<br>
03-21 13:07:43.766: ERROR/SoundPool(76): error loading /system/media/audio/ui/KeypressStandard.ogg<br>
03-21 13:07:43.766: ERROR/SoundPool(76): error loading /system/media/audio/ui/KeypressSpacebar.ogg<br>
03-21 13:07:43.766: ERROR/SoundPool(76): error loading /system/media/audio/ui/KeypressDelete.ogg<br>
03-21 13:07:43.776: ERROR/SoundPool(76): error loading /system/media/audio/ui/KeypressReturn.ogg<br>
03-21 13:07:43.806: ERROR/UsbObserver(76): java.lang.NullPointerException<br>
03-21 13:07:43.806: ERROR/UsbObserver(76):     at com.android.server.UsbObserver.init(UsbObserver.java:131)<br>
03-21 13:07:43.806: ERROR/UsbObserver(76):     at com.android.server.UsbObserver.<init>(UsbObserver.java:65)<br>
03-21 13:07:43.806: ERROR/UsbObserver(76):     at com.android.server.ServerThread.run(SystemServer.java:402)<br>
03-21 13:07:48.077: ERROR/ThrottleService(76): Could not open GPS configuration file /etc/gps.conf<br>
03-21 13:07:48.128: ERROR/ThrottleService(76): Error reading data file<br>
03-21 13:07:48.606: ERROR/logwrapper(172): executing /system/bin/tc failed: No such file or directory<br>
03-21 13:07:48.687: ERROR/logwrapper(174): executing /system/bin/tc failed: No such file or directory<br>
03-21 13:07:48.756: ERROR/logwrapper(177): executing /system/bin/tc failed: No such file or directory<br>
03-21 13:07:54.707: ERROR/logwrapper(213): executing /system/bin/tc failed: No such file or directory<br>
03-21 13:07:54.737: ERROR/logwrapper(214): executing /system/bin/tc failed: No such file or directory<br>
03-21 13:07:54.767: ERROR/logwrapper(215): executing /system/bin/tc failed: No such file or directory<br>
03-21 18:08:58.619: ERROR/MetadataRetrieverClient(33): failed to extract an album art<br>
03-21 18:09:17.207: ERROR/ThrottleService(76): Error reading data file<br>
03-21 18:25:23.387: ERROR/InputDispatcher(76): channel '40517da8 com.example.ssms/com.example.ssms.MsgSmsActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8<br>
03-21 18:25:23.387: ERROR/InputDispatcher(76): channel '40517da8 com.example.ssms/com.example.ssms.MsgSmsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439): FATAL EXCEPTION: main<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ssms/com.example.ssms.MsgSmsActivity2}: java.lang.NullPointerException<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.os.Handler.dispatchMessage(Handler.java:99)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.os.Looper.loop(Looper.java:123)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.ActivityThread.main(ActivityThread.java:3647)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at java.lang.reflect.Method.invokeNative(Native Method)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at java.lang.reflect.Method.invoke(Method.java:507)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at dalvik.system.NativeStart.main(Native Method)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439): Caused by: java.lang.NullPointerException<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at com.example.ssms.MsgSmsActivity2.onCreate(MsgSmsActivity2.java:23)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)<br>
03-21 18:25:31.837: ERROR/AndroidRuntime(439):     ... 11 more

4 个答案:

答案 0 :(得分:0)

private OnClickListener mylistener=new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        intent=new Intent(MsgSmsActivity.this,MsgSmsActivity2.class);
        intent.putExtra("bodyText", body);
        intent.putExtra("numbersText", number);
        MsgSmsActivity.this.startActivity(intent);
    }
};

尝试使用startActivity切换putExtra行。

答案 1 :(得分:0)

arr 是一个字符串数组。 tx.setText(arr); 会给出异常,因为 setText 被定义为接受 CharSequence的

如果要在 TextView 上设置一些文本,则首先使用 arr [index] 从数组中获取String,然后将该String设置为textview。 / p>

答案 2 :(得分:0)

不要在活动A中使用putExtra(),而是尝试将bodynumber放入ArrayList,然后使用putStringArrayListExtra(),然后在活动B中使用getStringArrayListExtra()代替getExtra()

答案 3 :(得分:0)

发送字符串数据

我们已经知道我们可以使用Intents从一个Activity转移到另一个Activity。

Intent i = new Intent(MainActivity.this,GoActivity.class);

String data = “bla bla”;// Data String to send

i.putExtra("DATA",data );

接收数据

我们可以收到上面的字符串数据,如下所示。

Intent i = getIntent();

String data = i.getStringExtra("DATA");// you well get the String data i.e., BLA BLA

发送和接收数据时,红色数据应该相同。

发送对象

以下是将Object从一个类发送到另一个类的代码。发送Object的一个重要事情是类应该实现Serializable类。

以下红色文本应该相同。

Intent i = new Intent(MainActivity.this,startActivity.class);

ObjectClassName object = new ObjectClassName();

i.putExtra("THIS", Object);

接收对象

// startActivity.java

Intent i = getIntent();

ObjectClassName obj = (ObjectClassName) getIntent().getSerializableExtra("THIS");// TypeCasting needed