0

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

User inputs 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

  1. 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);
    
  2. 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.

Quick learner
  • 6,975
  • 2
  • 30
  • 44
DaxHR
  • 643
  • 8
  • 27

4 Answers4

0

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?

Niza Siwale
  • 2,308
  • 1
  • 17
  • 20
  • No, it exists in another activity because I want it's value to be stored there – DaxHR Feb 26 '16 at 15:37
  • You can not reference a view which exists in another antivity which has not been inflated in yours. If you want to it's data, you have to save it from the layout it's been inflated from – Niza Siwale Feb 26 '16 at 15:40
  • You can not have two have two activities from the same process running at the same time. One of them will be killed. You have to create a class to handle the storage of the data regardless of the activity it is in – Niza Siwale Feb 26 '16 at 15:58
0

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)

barq
  • 3,546
  • 4
  • 24
  • 35
  • Yes but, I don't need it's data to be displayed there, I want it to be passed in main activity – DaxHR Feb 26 '16 at 15:38
  • Then you need to pass the data to the other Activity, for example by using an Extra. See my edited answer. – barq Feb 26 '16 at 15:43
0

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

Community
  • 1
  • 1
agonist_
  • 4,220
  • 3
  • 27
  • 47
0

When you call another activity and you want to transport data from called activity to caller activity you must implement onActivity Result() on caller...and call target activity using method startActivityForResult(). Then on called activity setResult and pass your extras on Intent.

How to manage `startActivityForResult` on Android?

Then onActivityResult in main activity you can set text values.