prince
prince

Reputation: 123

App stops working whenever I select one radio button

I have two radio buttons and if one is selected I want to change the value of Textview. I am getting an error. The app stops working when I click one of my radio buttons. It is giving this error on log.

 logged error 
>    /com.example.myapp.mttally E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.myapp.mttally, PID: 15125
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4025)
            at android.view.View.performClick(View.java:4785)
            at android.widget.CompoundButton.performClick(CompoundButton.java:120)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4020)
            at android.view.View.performClick(View.java:4785)
            at android.widget.CompoundButton.performClick(CompoundButton.java:120)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

..`            

          

My java code on main file is this

package com.example.myapp.mttally;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
RadioButton lend;
RadioButton borrow;
TextView toFrom;

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


    TextView toFrom = (TextView) findViewById(R.id.toFrom);

    RadioButton lend = (RadioButton) findViewById(R.id.lend);
    RadioButton borrow = (RadioButton) findViewById(R.id.borrow);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


public void onRadioButtonClicked(View view) {
    // Is the button now checked?

    Boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch (view.getId()) {
        case R.id.lend:
            if (checked)
                toFrom.setText("   To");
                break;
        case R.id.borrow:
            if (checked)
                toFrom.setText("   From");
            break;
    }
}
}

Here my xml file

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"  
 tools:context=".MainActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="8dp"
    android:id="@+id/linearLayout">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:clickable="false"
        >

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_button1"
                android:id="@+id/lend"
                android:textSize="16dp"
                android:onClick="onRadioButtonClicked"
                />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_button2"
                android:id="@+id/borrow"
                android:textSize="16dp"
                android:onClick="onRadioButtonClicked"
                />
        </RadioGroup>
    </LinearLayout>


</LinearLayout>

<TextView
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/toFrom"
    android:layout_below="@+id/linearLayout"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="78dp" />

</RelativeLayout>

Upvotes: 1

Views: 615

Answers (1)

Michiyo
Michiyo

Reputation: 1201

Every time you write something like TextView toFrom, RadioButton lend, or, more generally <type of variable> <variable name>, you're declaring a new variable. You declare variables called twoFrom, lend, and borrow in two places: once in the MainActivity class and once in the onCreate method. So, your code has two different variables that are both called toFrom, two variables called lend, and two variables called borrow.

Consider the lend variable. Each copy of the lend variable has its own "scope", or the area of code in which it exists. Since you declared the first lend variable in the class itself (not inside any of the methods), its scope is the class. You can (typically) use it in code anywhere in the class.

Your second lend variable, however, is what causes the problem. You've declared a second lend variable inside the onCreate method. That second lend is the one gets the TextView object...and then it ceases to exist once the onCreate method finishes executing.

So, to fix this, your onCreate should look like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toFrom = (TextView) findViewById(R.id.toFrom);
    lend = (RadioButton) findViewById(R.id.lend);
    borrow = (RadioButton) findViewById(R.id.borrow);
}

That way, when you get the reference to the TextView, you're holding it in the variable that will still exist when onRadioButtonClicked is called, not in a second identically-named variable that will cease to exist almost immediately.

Upvotes: 2

Related Questions