Question

Hello I started making an application on android, it's a simple BMI Calculator. The application has 2 activities (Main with a menu, and BMICalculator with the widgets).

My problem is that when I try to make an OnClickListener my application crash why trying to pass from Main to BMIcalculator. I Also tried to do it by android:OnClick and with this method, I can go through activities, but the application crash when I click on the "oblicz" button (which is for calculating BMI).

I tried a lot of things, searched the web but didn't find any solution :/ Please help me :D Here are the codes:

/\OnClickListener Work if i don't do the getText()

MainActivity.java

package com.example.labswm;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}
public void gotoP1(View view){
    Intent intent = new Intent(this, Page1Activity.class);
    startActivity(intent);


}

}

mainXML

<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.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/opis"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/gotoP1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/opis"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="132dp"
    android:layout_marginLeft="40dp"
    android:onClick="gotoP1"
    android:text="@string/oblicz_ibm" />

    <requestFocus />
</EditText>

BMIACtivity(named Page1Activity)

package com.example.labswm;



public class Page1Activity extends ActionBarActivity {

public Button oblicz;
public EditText waga;
public EditText wzrost; 
public RadioGroup group;
public TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page1);

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



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.page1, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page1,
                container, false);
        return rootView;
    }
}
public void calculateClick(View view){
    if(view.getId()==R.id.oblicz){

//THE ERROR STARTS HERE !
        EditText wag = (EditText)findViewById(R.id.waga);
        EditText wzr = (EditText)findViewById(R.id.wzrost);
        TextView res = (TextView)findViewById(R.id.result);

        float fwaga = Float.parseFloat(wag.getText().toString());
        float fwzrost = Float.parseFloat(wzrost.getText().toString());
        float rez = (float) (fwaga * 4.88 / fwzrost * fwzrost);
        res.setText("BMI" + rez);

    }
}





}

Page1Activity.xml

<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"
android:launchMode = "singleInstance" 
tools:context="com.example.labswm.Page1Activity$PlaceholderFragment" >

<TextView
    android:id="@+id/opis"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/opis_to_okno_pozwla_na_obliczenie_swojego_ibm_" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/opis"
    android:layout_below="@+id/opis"
    android:layout_marginTop="32dp"
    android:text="@string/waga_" />

<EditText
    android:id="@+id/waga"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/waga"
    android:layout_below="@+id/waga"
    android:text="@string/wzrost_" />

<EditText
    android:id="@+id/wzrost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:ems="10"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/oblicz"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/group"
    android:layout_below="@+id/group"
    android:onClick="calculateClick"
    android:text="@string/oblicz_" />

<RadioGroup
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/wzrost"
    android:layout_below="@+id/wzrost"
    android:layout_marginTop="42dp" >

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/centymetry" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/metry"
        tools:ignore="ObsoleteLayoutParam" />
</RadioGroup>

<TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/oblicz"
    android:layout_marginTop="31dp"
    android:layout_toRightOf="@+id/textView2"
    android:textSize="20sp" />

LOG

04-16 21:31:31.063: D/dalvikvm(12362): GC_EXTERNAL_ALLOC freed 87K, 47% free 2855K/5379K, external 0K/0K, paused 77ms
04-16 21:31:31.503: D/CLIPBOARD(12362): Hide Clipboard dialog at Starting input: finished by someone else... !
04-16 21:31:33.153: D/dalvikvm(12362): GC_CONCURRENT freed 121K, 46% free 3076K/5639K, external 328K/1281K, paused 2ms+3ms
04-16 21:31:38.093: W/dalvikvm(12362): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-16 21:31:38.103: E/AndroidRuntime(12362): FATAL EXCEPTION: main
04-16 21:31:38.103: E/AndroidRuntime(12362): java.lang.IllegalStateException: Could not execute method of the activity
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.view.View$1.onClick(View.java:2185)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.view.View.performClick(View.java:2585)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.view.View$PerformClick.run(View.java:9299)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.os.Handler.handleCallback(Handler.java:587)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.os.Looper.loop(Looper.java:130)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at java.lang.reflect.Method.invoke(Method.java:507)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at dalvik.system.NativeStart.main(Native Method)
04-16 21:31:38.103: E/AndroidRuntime(12362): Caused by: java.lang.reflect.InvocationTargetException
04-16 21:31:38.103: E/AndroidRuntime(12362):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at java.lang.reflect.Method.invoke(Method.java:507)
04-16 21:31:38.103: E/AndroidRuntime(12362):    at android.view.View$1.onClick(View.java:2180)
04-16 21:31:38.103: E/AndroidRuntime(12362):    ... 11 more
04-16 21:31:38.103: E/AndroidRuntime(12362): Caused by: java.lang.NullPointerException
04-16 21:31:38.103: E/AndroidRuntime(12362):    at com.example.labswm.Page1Activity.calculateClick(Page1Activity.java:89)
04-16 21:31:38.103: E/AndroidRuntime(12362):    ... 14 more
04-16 21:31:40.113: I/dalvikvm(12362): threadid=4: reacting to signal 3
04-16 21:31:40.113: I/dalvikvm(12362): Wrote stack traces to '/data/anr/traces.txt'
Was it helpful?

Solution

Here, you declare your EditText variables:

public EditText waga; 
public EditText wzrost; 

but you never initialise them.

You need something like waga = (EditText)findViewById(R.id.waga);

The specific error you have is null pointer at line 89 which means that wzrost is null, therefore .getText() causes an exception.

OTHER TIPS

Look at this line:

 float fwzrost = Float.parseFloat(wzrost.getText().toString());

You are using wzrost but you did not initialize it. You just initialized wzr ==> NPE

You are using an uninitialized variable wzrost in float fwzrost = Float.parseFloat(wzrost.getText().toString());. I am guessing you meant that to be wzr.

Button b1=(Button)findViewById(R.id.gotoP1);
b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {}
        });

your xml is ok but in MainActivity you must define that button

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top