有人可以解釋什麼是錯的?爲什麼按鈕不起作用?
我希望它能像這樣工作:
屏幕頂部的三個按鈕。當我按下按鈕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