0

so I try to pass the result calculated in my first activity to my second activity where it must be shown. I'm using the putExtra. The problem is when i try to set the text in the TextView "wyn". I also tried just to make seText("text") in the second activity and it don't work too. The program crash when it change activity :/ Thanks for help :)

The method to change activities

public void calculateClick(View view){
    if(view.getId()==R.id.oblicz){
        EditText wag = (EditText)findViewById(R.id.waga);
        EditText wzr = (EditText)findViewById(R.id.wzrost);
        RadioGroup group = (RadioGroup)findViewById(R.id.group);
        TextView res = (TextView)findViewById(R.id.result);
        if(wag ==null || wzr ==null || group == null){
            Toast.makeText(Page1Activity.this, "Podaj dane !",  Toast.LENGTH_SHORT).show();
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
        float fwaga = Float.parseFloat(wag.getText().toString());
        float fwzrost = Float.parseFloat(wzr.getText().toString());
        if( fwzrost <= 0){
            Toast.makeText(Page1Activity.this, "Coś za mały jesteś nie ?", Toast.LENGTH_SHORT).show();
            Intent intent = getIntent();
            finish();
            startActivity(intent);  
        }
        if(group.getCheckedRadioButtonId() == R.id.rb1){
            fwzrost = fwzrost /100;
        }

        float rez = (float) (fwaga / (fwzrost * fwzrost));
        Intent intent = new Intent(Page1Activity.this, Page1resActivity.class);
        intent.putExtra(EXTRA_MESSAGE, "BMI = " + rez);
        final int result = 1;
        startActivityForResult(intent, result);
    }
}

the second activity where i must sho the result

public class Page1resActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page1res);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    Intent intent = getIntent();
    String message = intent.getStringExtra(Page1Activity.EXTRA_MESSAGE);
    TextView res =  ((TextView) findViewById(R.id.wyn));
    res.setText(message); //ERROR HERE ?

}

XML of the secondActivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.labswm.Page1resActivity$PlaceholderFragment"
tools:ignore="HardcodedText,UselessParent" >

<LinearLayout
    android:id="@+id/lay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/opispagewyniki"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/opis_to_okno_pozwala_na_sprawdzenie_swojego_bmi_i_por_wnanie_go_" />
//THIS IS THE TEXTVIEW I USE TO PUT RESULT
    <TextView
        android:id="@+id/wyn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
//******************************************************// 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dla_os_b_doros_ych_wartosc_bmi_wskazuje_na_" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&lt; 15,0 - wyniszczenie"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_15_0_17_4_wychudzenie"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_17_5_19_4_niedowaga"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_19_5_24_9_waga_prawid_owa"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_25_0_29_9_nadwaga"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_30_0_34_9_i_stopie_oty_o_ci"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_35_0_39_9_ii_stopie_oty_o_ci"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_40_0_oty_o_kliniczna"
        android:textSize="15sp" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="dla leniwych !" />

</LinearLayout>

LOG

04-17 17:50:17.760: D/dalvikvm(16620): GC_CONCURRENT freed 132K, 46% free 3078K/5639K,  external 251K/1281K, paused 3ms+3ms
04-17 17:50:24.360: W/dalvikvm(16620): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-17 17:50:24.370: E/AndroidRuntime(16620): FATAL EXCEPTION: main
04-17 17:50:24.370: E/AndroidRuntime(16620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.labswm/com.example.labswm.Page1resActivity}: java.lang.NullPointerException
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at  android.os.Handler.dispatchMessage(Handler.java:99)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.os.Looper.loop(Looper.java:130)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at java.lang.reflect.Method.invoke(Method.java:507)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at dalvik.system.NativeStart.main(Native Method)
04-17 17:50:24.370: E/AndroidRuntime(16620): Caused by: java.lang.NullPointerException
04-17 17:50:24.370: E/AndroidRuntime(16620):    at com.example.labswm.Page1resActivity.onCreate(Page1resActivity.java:30)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
04-17 17:50:24.370: E/AndroidRuntime(16620):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-17 17:50:24.370: E/AndroidRuntime(16620):    ... 11 more
04-17 17:50:26.380: I/dalvikvm(16620): threadid=4: reacting to signal 3
04-17 17:50:26.380: I/dalvikvm(16620): Wrote stack traces to '/data/anr/traces.txt'
04-17 17:50:40.120: I/dalvikvm(16620): threadid=4: reacting to signal 3
04-17 17:50:40.130: I/dalvikvm(16620): Wrote stack traces to '/data/anr/traces.txt'
donfuxx
  • 11,277
  • 6
  • 44
  • 76
sincrow
  • 61
  • 1
  • 9
  • I think it is about time to learn how to debug NullPointerExceptions instead of posting your NPE's all the time. A good start would be: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception/218510 – donfuxx Apr 17 '14 at 16:05
  • I'm tring to post questions the less i can. I will read this, but the thing is that i tried to solve this by me during 4 hours and i don't know where the problem is because i followed a tutorial. I don't think it's a nullPointer, because when i do setText("text") in primary activity it works, but in the second something like setText("text") doesn't work. – sincrow Apr 17 '14 at 16:11
  • It matters what the stacktrace tells you: `Caused by: java.lang.NullPointerException 04-17 17:50:24.370: E/AndroidRuntime(16620): at com.example.labswm.Page1resActivity.onCreate(Page1resActivity.java:30)` ==> Take look at line 30 of Page1resActivity and identify what is causing the NPE – donfuxx Apr 17 '14 at 16:16
  • at line 30 there is : res.setText(message); it would mean that message is null no ? but the same error occurs when i change this line to res.setText("text") <- how this can be a null ? – sincrow Apr 17 '14 at 16:23
  • it means that `res == null` because NPE is thrown when you try to call a method on an object that is currently pointing to null, see previously posted link for more details ;-) – donfuxx Apr 17 '14 at 16:27
  • And I have a suspect why it's null. Do you have this textview's id in activity_page1res xml? – donfuxx Apr 17 '14 at 16:32
  • No the textview is in fragment_page1res.xml in activity_page1res.xml i have nothing. – sincrow Apr 17 '14 at 16:37

1 Answers1

1

You are setting the following layout as the contentView:

setContentView(R.layout.activity_page1res);

but your are searching for an id that is not contained in this layout (because the textview is in fragment_page1res.xml layout):

TextView res =  ((TextView) findViewById(R.id.wyn));
res.setText(message); //ERROR HERE ?

==> That is why findViewById returned null ==> res == null ==> NPE after trying to call setText on res.

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • Thank you, can you just tell my what is the difference between fragment_page1res.xml and activity_page1res.xml ? – sincrow Apr 17 '14 at 16:45
  • Seems like there has been created a layout for a fragment at some point and this fragment would use `fragment_page1res.xml`. – donfuxx Apr 17 '14 at 16:52