2014-01-24 58 views
-1

有人可以解釋什麼是錯的?爲什麼按鈕不起作用?

我希望它能像這樣工作:

屏幕頂部的三個按鈕。當我按下按鈕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) 
+1

爲第一個按鈕設置'View.GONE'到所有的情況下,是不是正確的? (只注意,不解決:)),你不需要這個部分:'} switch(v.getId()){'爲按鈕2和按鈕3部分 - 所以,你可以使用代碼,如:'switch(v .getId()){case x:some_code;打破; case y:some_code;打破; }' – Atiris

+0

我不知道代碼有什麼問題,但爲什麼不解釋它有什麼問題呢?如在,發生了什麼?你看到了什麼?例如,如果唯一的問題是該按鈕不起作用,則應該檢查@Atiris的評論。如果還有更多,爲什麼不解釋? – Nanne

+0

按下按鈕時沒有任何反應,這就是問題所在。我正在嘗試他/她的解決方案。 – johgru

回答

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
I認爲這是錯誤的,然後修復View.GONEswitch命令.. 嘗試一些教程,然後再做一次。

+0

謝謝你,我已更新我的文章,但是當我使用你的代碼時,它強制關閉,即使有任何動作附加到按鈕。 – johgru

+0

你在logcat中看到了什麼錯誤? – Atiris

+0

我已經添加了,現在 – johgru

相關問題