为什么按钮不起作用?

时间:2014-01-24 12:56:56

标签: android-layout android-button

有人可以解释什么是错的吗?

我希望它能像这样工作:

屏幕顶部有三个按钮。当我按下button1时,它应显示#1可见。当我按下按钮2时,它应该显示#2并隐藏#1和#3等等......我的每个内部都有不同的布局。

FRAGMENT CLASS:

 public class Fragment1 extends Fragment implements View.OnClickListener {

    public static Fragment newInstance(Context context) {
        Fragment1 f = new Fragment1();

        return f;
    }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_musikteori_symboler, null);

    final Button button1 = (Button)getActivity().findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getActivity().findViewById(R.id.symb_id1).setVisibility(View.VISIBLE);
            getActivity().findViewById(R.id.symb_id2).setVisibility(View.GONE);
            getActivity().findViewById(R.id.symb_id3).setVisibility(View.GONE);
        }
    });

    final Button button2 = (Button)getActivity().findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getActivity().findViewById(R.id.symb_id1).setVisibility(View.GONE);
            getActivity().findViewById(R.id.symb_id2).setVisibility(View.VISIBLE);
            getActivity().findViewById(R.id.symb_id3).setVisibility(View.GONE);
        }
    });

    final Button button3 = (Button)getActivity().findViewById(R.id.button3);
    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getActivity().findViewById(R.id.symb_id1).setVisibility(View.GONE);
            getActivity().findViewById(R.id.symb_id2).setVisibility(View.GONE);
            getActivity().findViewById(R.id.symb_id3).setVisibility(View.VISIBLE);
        }
    });

    return root;
}

布局:

 
<LinearLayout
android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
android:background="@color/white"
    xmlns:android="http://schemas.android.com/apk/res/android">



    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Dynamik"
            android:id="@+id/button1"
            android:layout_weight="1"/>


        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Toner"
            android:id="@+id/button2"
            android:layout_weight="1"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Takter"
            android:id="@+id/button3"
            android:layout_weight="1"/>

        </LinearLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <include
            android:id="@+id/symb_id1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            layout="@layout/fragment_musikteori_symb_dynamik" />


        <include
            android:id="@+id/symb_id2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            layout="@layout/fragment_musikteori_symb_toner" />

        <include
            android:id="@+id/symb_id3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            layout="@layout/fragment_musikteori_symb_takter" />




        </FrameLayout>
</ScrollView>

</LinearLayout>

...错误

01-24 14:38:51.341    5039-5039/com.myapp.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.okramuf.musikteori, PID: 5039
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.test/com.myapp.test.testapp_main}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.okramuf.musikteori.musikteori_main.onCreate(musikteori_main.java:51)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

我没有看到点击操作的按钮注册 还有一些错误:) 在这里查看教程:http://developer.android.com/reference/android/widget/Button.html

你需要这样的东西:

     final Button button = (Button) findViewById(R.id.button1);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });

或者,如果您在点击侦听器上实现,则可以使用

     final Button button = (Button) findViewById(R.id.button1);
     button.setOnClickListener(this);

您在XML图层中使用@+id/button1,然后在代码中使用R.id.symb_id1
我认为这是错误的,然后修复View.GONEswitch命令.. 尝试一些教程,然后再做一次。

答案 1 :(得分:0)

如果在这里找到解决方案:

Android Developers - Performing Fragment Transactions