如何将EditText参数传递给Android上的另一个活动?

时间:2014-05-02 19:01:47

标签: java android xml android-activity

我对以下.java和.xml文件有一个主旨性。

MainActivity.java& fragment_main.xml

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.mydomain.testpro.MESSAGE";

......

public void sendNumber(View view) {
    // Need an intent to open the results page
    Intent intent = new Intent(this, Result.class);
    EditText editText = (EditText) findViewById(R.id.number1Id);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.canavars.testpro.MainActivity$PlaceholderFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/number1" />

    <EditText
        android:id="@+id/number1Id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/editTextHint1"
        android:inputType="number" />  

</LinearLayout>


<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/sendNumber"
    android:layout_gravity="center_horizontal"
    android:onClick="sendNumber" />

 </LinearLayout>

Result.java&amp; fragment_result.xml

public class Result extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = (TextView)findViewById(R.id.numberId);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);


}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.canavars.testpro.Result$PlaceholderFragment" >

<TextView
    android:id="@+id/numberId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/resultTitle" />

我在哪里犯错,什么是正确的方法?

这是错误日志

05-02 13:14:19.729: W/dalvikvm(1161): threadid=1: thread exiting with uncaught exception (group=0xb1a51ba8)
05-02 13:14:19.769: E/AndroidRuntime(1161): FATAL EXCEPTION: main
05-02 13:14:19.769: E/AndroidRuntime(1161): Process: com.canavars.testpro, PID: 1161
05-02 13:14:19.769: E/AndroidRuntime(1161): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.canavars.testpro/com.canavars.testpro.Result}: java.lang.NullPointerException
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.os.Looper.loop(Looper.java:136)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at java.lang.reflect.Method.invoke(Method.java:515)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at dalvik.system.NativeStart.main(Native Method)
05-02 13:14:19.769: E/AndroidRuntime(1161): Caused by: java.lang.NullPointerException
05-02 13:14:19.769: E/AndroidRuntime(1161):     at com.canavars.testpro.Result.onCreate(Result.java:28)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.Activity.performCreate(Activity.java:5231)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 13:14:19.769: E/AndroidRuntime(1161):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 13:14:19.769: E/AndroidRuntime(1161):     ... 11 more

2 个答案:

答案 0 :(得分:0)

错误是通过传递TextView而不是布局来设置内容活动视图。我想你正在尝试更新xml的内容。这不是它的工作原理。

删除

setContentView(textView);

你应用程序运行正常。

答案 1 :(得分:0)

Eclipse IDE存在问题。转移到Android Studio,它就像一个魅力没有任何问题。