Ohke这里是更新版本: 主xml:
<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.example.names.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:onClick="nextScreen"
android:text="@string/Next" />
<EditText
android:id="@+id/editText2"
android:hint="Player B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="31dp"
android:ems="10" />
<EditText
android:id="@+id/editText1"
android:hint="Player A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:ems="10" />
</RelativeLayout>
主要的下一步按钮调用方法nextScreen:
public void nextScreen (View view)
{
EditText ed1 = (EditText)findViewById(R.id.editText1);
EditText ed2 = (EditText)findViewById(R.id.editText2);
Intent intent = new Intent (this, ButtonsActivity.class);
intent.putExtra("string_A", ed1.getText());
intent.putExtra("string_B", ed2.getText());
startActivity(intent);
}
第二项活动中的onCreate:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
Button btn1 = (Button)findViewById(R.id.player_A);
Button btn2 = (Button)findViewById(R.id.player_B);
btn1.setText((CharSequence) getIntent().getExtras().get("string_A"));
btn2.setText((CharSequence) getIntent().getExtras().get("string_B"));
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
当我在EditTexts中输入一些文本时,按下一个按钮,应用程序崩溃。 logcat的 -
05-04 15:21:54.922: E/AndroidRuntime(2025): FATAL EXCEPTION: main
05-04 15:21:54.922: E/AndroidRuntime(2025): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.names/com.example.names.ButtonsActivity}: java.lang.NullPointerException
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.os.Looper.loop(Looper.java:137)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.ActivityThread.main(ActivityThread.java:4867)
05-04 15:21:54.922: E/AndroidRuntime(2025): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 15:21:54.922: E/AndroidRuntime(2025): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 15:21:54.922: E/AndroidRuntime(2025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-04 15:21:54.922: E/AndroidRuntime(2025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-04 15:21:54.922: E/AndroidRuntime(2025): at dalvik.system.NativeStart.main(Native Method)
05-04 15:21:54.922: E/AndroidRuntime(2025): Caused by: java.lang.NullPointerException
05-04 15:21:54.922: E/AndroidRuntime(2025): at com.example.names.ButtonsActivity.onCreate(ButtonsActivity.java:24)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.Activity.performCreate(Activity.java:5047)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-04 15:21:54.922: E/AndroidRuntime(2025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
05-04 15:21:54.922: E/AndroidRuntime(2025): ... 11 more
答案 0 :(得分:0)
您无法从未设置为上下文视图的布局文件中通过id获取视图。在调用第二个活动时,您应该将要在按钮上显示的字符串设置为Intent
的额外字符
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Str1", ed1.getText().toString());
intent.putExtra("Str2", ed2.getText().toString());
startActivity(intent);
在你的第二项活动中:
btn1.setText(getIntent().getExtras().get("Str1"));
btn2.setTextgetIntent().getExtras().get("Str2"));
这是在活动之间共享数据的最佳方式。
答案 1 :(得分:0)
这样做
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("string_A", ed1.getText().toString()); //see .toString();
intent.putExtra("string_B", ed2.getText().toString());
并得到这样的结果。
getIntent().getStringExtra("string_A");