首先,我想說這是我的第一篇文章,因此一般來說可能沒有做得很好。將各種editText傳遞給另一個活動
我的問題如下:我想從三個editText中使用三個不同的字符串,並將它們顯示在另一個活動的三個TextView中。我已經在尋找各種不同的方法(數組,捆綁),但它仍在繼續崩潰。請幫幫我。在這裏,你有我的代碼:
主要活動
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
EditText eText1 = (EditText) findViewById(R.id.editText1);
EditText eText2 = (EditText) findViewById(R.id.editText2);
EditText eText3 = (EditText) findViewById(R.id.editText3);
String m1 = eText1.getText().toString();
String m2 = eText2.getText().toString();
String m3 = eText3.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra("m1",m1);
intent.putExtra("m2",m2);
intent.putExtra("m3",m3);
startActivity(intent);
}
子活動:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Bundle extras = getIntent().getExtras();
// get the extras
String a = extras.getString("m1");
String b = extras.getString("m2");
String c = extras.getString("m3");
// Set the text views
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(a); // This is line 23
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setText(b);
TextView tv3 = (TextView) findViewById(R.id.textView3);
tv3.setText(c);
}
EDIT1:我已經改變了什麼@Squonk說,但仍然崩潰。我不知道如何上傳logcat,因爲它對於文本來說太大了,但我仍然無法上傳圖像。 不知道該怎麼辦:(
EDIT2:。由於再次@Squonk我終於成功地加入logcat的。這是我最近在「EDIT1」改變了代碼的logcat的
03-29 16:05:39.905: E/AndroidRuntime(327): FATAL EXCEPTION: main
03-29 16:05:39.905: E/AndroidRuntime(327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.os.Looper.loop(Looper.java:123)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 16:05:39.905: E/AndroidRuntime(327): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): at java.lang.reflect.Method.invoke(Method.java:521)
03-29 16:05:39.905: E/AndroidRuntime(327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 16:05:39.905: E/AndroidRuntime(327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 16:05:39.905: E/AndroidRuntime(327): at dalvik.system.NativeStart.main(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): Caused by: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:23)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 16:05:39.905: E/AndroidRuntime(327): ... 11 more
這是fragment_display_message.xml的textviews之一。有三個人。會不會是在子活動尋找textviews時,我必須寫在哪裏可以找到他們不要與主要活動混淆?
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="43dp"
android:hint="@string/edit_message"
android:textAppearance="?android:attr/textAppearanceLarge" />
編輯3:尤里卡!我有fi最終發現問題出在哪裏。我已將textview插入到fragment_display_message.xml中,而不是activity_display_message.xml。和mainactivity.java一樣,我使用fragment_main.xml,我認爲在一個子活動中會是一樣的。那麼,我錯了。謝謝大家! :d
感謝您的支持
你可以發佈logcat嗎? –
顯示錯誤的堆棧跟蹤。 – AxiomaticNexus
歡迎來到SO !.請發佈堆棧跟蹤,它會更容易幫助您 – rpax