我在将活动的 EditText 和 Spinner 的值传递给来自另一个活动的 TextView 时遇到问题。我只能将第一个活动链接到第二个活动。这些值不会显示在 textview 中。
另外,我得到了 NullPointerException ,它指向String name = b.getString("name");
哪个来自第二个活动。
下面是第一个活动代码(将值传递给第二个活动) - PersonalInformation:
btnView = (Button) findViewById(R.id.btnView);
btnView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
EditText txtDate = (EditText) findViewById(R.id.txtDate);
EditText txtType = (EditText) findViewById(R.id.txtType);
EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
Intent i = new Intent(PersonalInformation.this, SavedInfo.class);
Bundle b = new Bundle();
b.putString("name", nameSpinner.getSelectedItem().toString());
b.putString("date", txtDate.getText().toString());
b.putString("category", txtType.getText().toString());
b.putString("likes", txtLikes.getText().toString());
b.putString("dislikes", txtDislikes.getText().toString());
i.putExtras(b);
startActivity(i);
Intent viewIntent = new Intent(context, SavedInfo.class);
startActivity(viewIntent);
这些用于从第一个活动中获取值的代码 - SavedInfo.java
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_info);
Bundle b = this.getIntent().getExtras();
String name = b.getString("name");
String date = b.getString("date");
String category = b.getString("category");
String likes = b.getString("likes");
String dislikes = b.getString("dislikes");
((TextView)findViewById(R.id.textName)).setText(name);
((TextView)findViewById(R.id.textDate)).setText(date);
((TextView)findViewById(R.id.textType)).setText(category);
((TextView)findViewById(R.id.textLikes)).setText(likes);
((TextView)findViewById(R.id.textDislikes)).setText(dislikes);
下面是我的 LogCat
09-03 14:31:49.021: I/dalvikvm(573): threadid=3: reacting to signal 3
09-03 14:31:49.121: I/dalvikvm(573): Wrote stack traces to '/data/anr/traces.txt'
09-03 14:31:49.391: D/gralloc_goldfish(573): Emulator without GPU emulation detected.
09-03 14:31:51.960: D/dalvikvm(573): GC_FOR_ALLOC freed 101K, 3% free 9255K/9479K, paused 47ms
09-03 14:31:55.960: D/AndroidRuntime(573): Shutting down VM
09-03 14:31:55.960: W/dalvikvm(573): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-03 14:31:55.980: E/AndroidRuntime(573): FATAL EXCEPTION: main
09-03 14:31:55.980: E/AndroidRuntime(573): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.SavedInfo}: java.lang.NullPointerException
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.os.Looper.loop(Looper.java:137)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-03 14:31:55.980: E/AndroidRuntime(573): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 14:31:55.980: E/AndroidRuntime(573): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 14:31:55.980: E/AndroidRuntime(573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-03 14:31:55.980: E/AndroidRuntime(573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-03 14:31:55.980: E/AndroidRuntime(573): at dalvik.system.NativeStart.main(Native Method)
09-03 14:31:55.980: E/AndroidRuntime(573): Caused by: java.lang.NullPointerException
09-03 14:31:55.980: E/AndroidRuntime(573): at main.page.SavedInfo.onCreate(SavedInfo.java:19)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.Activity.performCreate(Activity.java:4465)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-03 14:31:55.980: E/AndroidRuntime(573): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-03 14:31:55.980: E/AndroidRuntime(573): ... 11 more
我需要将 EditText 和 Spinner 的值作为 TextView 传递给第二个活动。