Search code examples
androidandroid-layout

Finding a button by it's "tag" in android


I am making an android app where initially the user get the "GO" button. On clicking the button a new relative layout is made visible which has a few option buttons namely - "addition" and "subtraction". I then try to get which button the user has clicked and accordingly present that set of questions. However, as soon the the GO button is clicked the app crashes.. My code to check which button in clicked is ---

public void start(View view)
    {
        startbutton.setVisibility(View.INVISIBLE);
        Log.i("YO:", "Start button ok!");
        optionRelativeLayout.setVisibility(RelativeLayout.VISIBLE);
        int tag =  (Integer) view.getTag();
        if(tag==4)
            additionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
        if(tag==5)
          subtractionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
    }

The XML code is

<Button
        android:text="Go!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/start_button"
        android:textSize="80sp"
        android:paddingBottom="50dp"
        android:paddingEnd="50dp"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingStart="50dp"
        android:paddingTop="50dp"
        android:background="@android:color/holo_green_light"
        android:onClick="start"
        android:visibility="visible" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/optionrelativlayout"
        android:visibility="visible">

        <Button
            android:text="Addition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="4  "
            android:layout_centerHorizontal="true"
            android:layout_marginTop="45dp"
            android:id="@+id/addition" />

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="5"
            android:layout_centerHorizontal="true"
            android:id="@+id/subtraction"
            android:layout_marginTop="150dp" />

    </RelativeLayout>

The error is

04-24 22:21:39.525 10853-10853/com.example.rohit_136.brain_trainer I/YO:: Start button ok!
04-24 22:21:39.527 10853-10853/com.example.rohit_136.brain_trainer E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.rohit_136.brain_trainer, PID: 10853
                                                                                     java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                         at android.view.View.performClick(View.java:5226)
                                                                                         at android.view.View$PerformClick.run(View.java:21350)
                                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5582)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                         at android.view.View.performClick(View.java:5226) 
                                                                                         at android.view.View$PerformClick.run(View.java:21350) 
                                                                                         at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5582) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
                                                                                         at com.example.rohit_136.brain_trainer.MainActivity.start(MainActivity.java:177)
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                         at android.view.View.performClick(View.java:5226) 
                                                                                         at android.view.View$PerformClick.run(View.java:21350) 
                                                                                         at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5582) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-24 22:21:39.535 10853-10865/com.example.rohit_136.brain_trainer I/ActivityThreadEui: schedulePauseActivity com.example.rohit_136.brain_trainer.MainActivity finished=true userLeaving=false configChanges=0 dontReport=false
04-24 22:21:41.098 10853-10853/com.example.rohit_136.brain_trainer I/Process: Sending signal. PID: 10853 SIG: 9

Solution

  • Why not to use ids that these buttons already have?

    public void start(View view)
        {
            startbutton.setVisibility(View.INVISIBLE);
            Log.i("YO:", "Start button ok!");
            optionRelativeLayout.setVisibility(RelativeLayout.VISIBLE);
            int id =  view.getId();
            if(id==R.id.addition)
                additionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
            if(id==R.id.subtraction)
              subtractionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
        }