我的代码问题我将数据从一个活动发送到另一个活动。 我不知道这里有什么问题,我的logcat中有很多错误。我是android的新手。谢谢!
这是我的发件人活动代码
public void sendFeedback(View button) {
EditText name = (EditText) findViewById(R.id.EditTextName);
String name1 = name.getText().toString();
//Create new Intent Object, and specify class
Intent intent = new Intent();
intent.setClass(MainActivity.this,result.class);
//Set your data using putExtra method which take
//any key and value which we want to send
intent.putExtra("senddata",name1);
//Use startActivity or startActivityForResult for Starting New Activity
MainActivity.this.startActivity(intent);
}
}
这是接收器活动的代码:
TextView name2 = (TextView) findViewById(R.id.tv1);
{
//obtain Intent Object send from SenderActivity
Intent intent = this.getIntent();
/* Obtain String from Intent */
if(intent !=null)
{
String strdata = intent.getExtras().getString("senddata");
// DO SOMETHING HERE
name2.setText(strdata);
}
else
{
// DO SOMETHING HERE
}
}
这是我的logcat:
07-17 02:02:52.576: E/AndroidRuntime(997): FATAL EXCEPTION: main
07-17 02:02:52.576: E/AndroidRuntime(997): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dev.msg.feedbackmessaging/dev.msg.feedbackmessaging.result}: java.lang.NullPointerException
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.os.Looper.loop(Looper.java:137)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-17 02:02:52.576: E/AndroidRuntime(997): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 02:02:52.576: E/AndroidRuntime(997): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 02:02:52.576: E/AndroidRuntime(997): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-17 02:02:52.576: E/AndroidRuntime(997): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-17 02:02:52.576: E/AndroidRuntime(997): at dalvik.system.NativeStart.main(Native Method)
07-17 02:02:52.576: E/AndroidRuntime(997): Caused by: java.lang.NullPointerException
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.Activity.findViewById(Activity.java:1839)
07-17 02:02:52.576: E/AndroidRuntime(997): at dev.msg.feedbackmessaging.result.<init>(result.java:17)
07-17 02:02:52.576: E/AndroidRuntime(997): at java.lang.Class.newInstanceImpl(Native Method)
07-17 02:02:52.576: E/AndroidRuntime(997): at java.lang.Class.newInstance(Class.java:1319)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-17 02:02:52.576: E/AndroidRuntime(997): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
谢谢你们。
答案 0 :(得分:0)
我认为当你设置值intent.putExtra(“senddata”,name1);然后name1为null并且
String strdata = intent.getExtras().getString("senddata");
这里你没有使用任何默认值
你应该使用默认值如
的getStringString strdata = intent.getExtras().getString("senddata","");
它可能会删除您的例外。