我在传递EditText&的值时遇到了麻烦。来自另一个活动的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。
答案 0 :(得分:1)
您已在代码中为同一活动开始了两次活动。
使用此代码:
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);
并在SavedInfo.java文件中:
使用
从包中获取字符串n微调器值Bundle extras = i.getExtras();
String date = extra.getString("date");
...
...
希望它会对你有所帮助。
答案 1 :(得分:0)
我认为问题在于你开始两次活动:
startActivity(i);
Intent viewIntent = new Intent(context, SavedInfo.class);
startActivity(viewIntent);
只需删除最后2行即可。 祝你好运
答案 2 :(得分:0)
您已将意图调用两次相同的活动类。
i.putExtras(b);
startActivity(i);
Intent viewIntent = new Intent(context, SavedInfo.class);
startActivity(viewIntent);
所以删除viewIntent
的最后两行,代码的其他部分似乎没问题。
答案 3 :(得分:0)
除了上面的答案,还检查一下,SavedInfo应该不是一个简单的java类?如果它是一个类,则不能将其视为StartActivity参数的一部分。
需要将它从Activity类派生为一个活动,然后才能以它的使用方式使用。