有人可以解释什么是错的?为什么按钮不起作用?
我希望它能像这样工作:
屏幕顶部的三个按钮。当我按下按钮1时,它应该显示使#1可见。当我按下按钮2时,它应该显示#2并隐藏#1和#3,等等......我在每个内部都有不同的布局。
片段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)
为第一个按钮设置'View.GONE'到所有的情况下,是不是正确的? (只注意,不解决:)),你不需要这个部分:'} switch(v.getId()){'为按钮2和按钮3部分 - 所以,你可以使用代码,如:'switch(v .getId()){case x:some_code;打破; case y:some_code;打破; }' – Atiris
我不知道代码有什么问题,但为什么不解释它有什么问题呢?如在,发生了什么?你看到了什么?例如,如果唯一的问题是该按钮不起作用,则应该检查@Atiris的评论。如果还有更多,为什么不解释? – Nanne
按下按钮时没有任何反应,这就是问题所在。我正在尝试他/她的解决方案。 – johgru