简体   繁体   中英

Passing values of EditText and Spinner from an Activity into TextView of another Activity

I'm having trouble with passing values of EditText & Spinner of activity to TextView which is from another activity. I can only link first activity to second activity. The values are not displayed in textview.


Plus, i got NullPointerException and it points to String name = b.getString("name"); which is from 2nd activity.

Below is the first activity code(passing the values to 2nd activity) - 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);

These the codes for getting the values from the first activity - 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);

Below is my 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

I need to pass the values of the EditText & Spinner to the 2nd activity as TextView.

You have started activity twice in your code for the same activity.

use this code :

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

and in your SavedInfo.java file :

get string n spinner value from the bundle using

Bundle extras = i.getExtras();
String date = extra.getString("date");
...
...

Hope it will help you.

I think that the problem is that you are starting activity twice:

            startActivity(i);

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

Just remove last 2 lines and it should work for you. Good luck

You have called the intent to same activity class twice.

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

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

So remove the last 2 lines of viewIntent and the other part of the code seems okay.

Apart from the above answers, also check, SavedInfo should not be a simple java class? If it's a class, one can not mention it as the part of StartActivity parameter.

one need to derive it from Activity class to be an activity and then can be used the way it is getting used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM