通过它找到一个按钮"标记"在android中

时间:2017-04-24 16:59:47

标签: android android-layout

我正在制作一个Android应用程序,最初用户获得了#34; GO"按钮。单击按钮时,可以看到一个新的相对布局,其中有几个选项按钮,即"添加"和"减法"。 然后,我尝试获取用户点击的按钮,并相应地显示该组问题。 但是,只要单击GO按钮,应用程序就会崩溃。 我的代码检查点击了哪个按钮---

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);
    }

XML代码

<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>

错误是

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

3 个答案:

答案 0 :(得分:2)

为什么不使用这些按钮已有的ID?

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);
    }

答案 1 :(得分:2)

- 编辑 -

我想我没理解你想要达到的目标。

1 - 在您的start()方法中,将其删除:

    int tag =  (Integer) view.getTag();
    if(tag==4)
        additionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
    if(tag==5)
      subtractionrelativelayout.setVisibility(RelativeLayout.VISIBLE);

2 - 创建两个新方法并将可见性线放入其中:

public void displayAddition(View view){
    additionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
}

public void displaySubtraction(View view){
    subtractionrelativelayout.setVisibility(RelativeLayout.VISIBLE);
}

3 - 在XML中,将它们添加到按钮:

addition按钮中添加以下行: android:onClick="displayAddition"

subtraction按钮中添加以下行: android:onClick="displaySubtraction"

答案 2 :(得分:0)

您正在使用view.getTag(),但问题是视图不是按钮变量

使用此:

Button button = (Button) view;
int Tag = button.getTag();
//Do what ever you wanna do with the tag from here on...

希望这会有所帮助