Saving data to TextView on different activity

On home screen of my app I want to have various TextViews that inititially hold only "..." for data, until user inputes his data.

User inputes data in different activity that opens when CardView that holds all "..." values is clicked.

I constantly get NullPointerException

02-26 16:14:33.106 26275-26275/hr.app.liftme.liftmehr E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: hr.app.liftme.liftmehr, PID: 26275


java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                        at hr.app.liftme.liftmehr.StatistikeInputMain$1.onClick(StatistikeInputMain.java:54)
                                                                        at android.view.View.performClick(View.java:4856)
                                                                        at android.view.View$PerformClick.run(View.java:19956)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:211)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

Here's my java file

public class StatistikeInputMain extends AppCompatActivity {
    RadioGroup radioGrupa;
    RadioButton imperial, metric;
    EditText visinaCM, visinaFT, visinaINC, tezina, bodyfat, tdee;
    Spinner kolikoDugoTrenirate, cilj;
    TextView rezultatTDEE;
    Button spremiRezultat;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistike_input_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        radioGrupa = (RadioGroup) findViewById(R.id.radioGrupaStatistike);
        imperial = (RadioButton) findViewById(R.id.radioStatistikeImperial);
        metric = (RadioButton) findViewById(R.id.radioStatistikeMetric);
        visinaCM = (EditText) findViewById(R.id.editTextStatistikeVisinaCM);
        visinaFT = (EditText) findViewById(R.id.editTextStatistikeVisinaFEET);
        visinaINC = (EditText) findViewById(R.id.editTextStatistikeVisinaINCH);
        tezina = (EditText) findViewById(R.id.editTextStatistikeTezina);
        bodyfat = (EditText) findViewById(R.id.editTextStatistikeBF);
        tdee = (EditText) findViewById(R.id.editTextStatistikeTDEE);
        kolikoDugoTrenirate = (Spinner) findViewById(R.id.spinnerStatistikeKolikoDugoTrenirate);
        cilj = (Spinner) findViewById(R.id.spinnerStatistikeCilj);
        spremiRezultat = (Button) findViewById(R.id.buttonStatistikeSpremi);
        rezultatTDEE = (TextView) findViewById(R.id.textViewStatistikeRezultatTDEE);


        spremiRezultat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double unosTDEE = Double.parseDouble(tdee.getText().toString());


                double rezultatTDEEInput = 0;
                rezultatTDEE.setText(Double.toString(rezultatTDEEInput));
            }
        });

    }



}

Note that error occurs on this line

  rezultatTDEE.setText(Double.toString(rezultatTDEEInput));

I want data to be saved on this Text View

 <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="..."
                        android:id="@+id/textViewStatistikeRezultatTDEE"
                        android:textSize="14sp" />

That is initiallized and casted in java file..

Another note - data is saved when the button is pressed!

What am I missing here?

EDIT

  • activity

    spremiRezultat.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class); intent.putExtra("tdeeInput", tdee.getText().toString()); startActivity(intent);

  • activity

    Intent intent = getIntent(); String tdee = intent.getStringExtra("tdeeInput");

  • I did it this way, only way that it doesn't give error, button works fine but it still doesn't display results


    The rezultatTDEE is null thats why that error happens. Does it exist in the activity_statistike_input_main layout with the id R.id.textViewStatistikeRezultatTDEE?


    if textViewStatistikeRezultatTDEE is not inside your activity_statistike_input_main layout, then it's normal that you get a NullPointerException. Because findViewById try to find the view inside the current layout. So if your TextView is in a different activity you have to pass the data through an Intent

    How ? Simple : Follow this

    This is the very basic of Android development. You really have to know this. It's in all book/tuto about Android programming


    rezultatTDEE is null, because you have no R.id.textViewStatistikeRezultatTDEE in R.layout.activity_statistike_input_main

    You mentioned in the comment that you want to set the text in the other Activity. This means you need to use an Extra to pass the String to the new Activity. For example:

    Intent intent = new Intent(this, OtherActivity.class);
    intent .putExtra("string", rezultatTDEEInput);
    startActivity(intent); 
    

    And then in OtherActivity do:

    Intent intent = getIntent();
    String string = intent.getExtras().getString("string");
    

    And then you can do textView.setText(string)

    链接地址: http://www.djcxy.com/p/67910.html

    上一篇: 当mesg框退出时,它返回到prevoius激活状态

    下一篇: 将数据保存到不同的活动上