0

我在将活动的 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 传递给第二个活动。

4

4 回答 4

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");
...
...

希望它会帮助你。

于 2012-09-03T07:13:09.373 回答
0

我认为问题在于您要开始活动两次:

            startActivity(i);

            Intent viewIntent = new Intent(context, SavedInfo.class);
            startActivity(viewIntent);

只需删除最后两行,它应该适合你。祝你好运

于 2012-09-03T07:13:20.780 回答
0

您已经两次调用同一活动类的意图。

                i.putExtras(b);
                startActivity(i);

                Intent viewIntent = new Intent(context, SavedInfo.class);
                startActivity(viewIntent);

所以删除最后两行viewIntent代码的其他部分似乎没问题。

于 2012-09-03T07:13:28.683 回答
0

除了上面的答案,还要检查一下,SavedInfo 不应该是一个简单的 java 类吗?如果它是一个类,则不能将其作为 StartActivity 参数的一部分。

需要从 Activity 类派生它成为一个活动,然后可以按照它被使用的方式使用它。

于 2012-09-03T07:21:55.820 回答