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="< 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'
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.