2016-03-26 11 views
-1

Ich habe Schaltflächen in meiner Navigationsleiste hinzugefügt. Wenn ich jedoch auf die Schaltflächen klicke. Die Anwendung muss anhalten. Es gibt keine Fehler in den Codes. Hier ist die Java-DateiNavigationsschublade funktioniert nicht

package com.android.pet.view; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 

import com.doepiccoding.navigationdrawer.R; 

public class Lesson111 extends Fragment { 
    private RadioGroup radioGroup; 


    public View onCreateView(LayoutInflater Inflater, ViewGroup container,Bundle savedInstanceState) { 
     View rootView = Inflater.inflate(R.layout.onepointthree, null); 

    /* Initialize Radio Group and attach click handler */ 
     radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup); 
     radioGroup.clearCheck(); 

    /* Attach CheckedChangeListener to radio group */ 
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       RadioButton rb = (RadioButton) group.findViewById(checkedId); 
       if(null!=rb && checkedId > -1){ 
        Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }); 
     return rootView; 
    } 

    public void onClear(View v) { 
    /* Clears all selected radio buttons to default */ 
     radioGroup.clearCheck(); 
    } 

    public void onSubmit(View v) { 
     RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()); 
     Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show(); 
    } 
} 

Auch hier ist der logcat. Hier

03-27 07:36:12.185 24720-24720/com.doepiccoding.navigationdrawer E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.doepiccoding.navigationdrawer, PID: 24720 
                        java.lang.IllegalStateException: Could not find a method onClear(View) in the activity class com.android.pet.view.NavigationActivity for onClick handler on view class android.widget.Button with id 'clearBtn' 
                         at android.view.View$1.onClick(View.java:3871) 
                         at android.view.View.performClick(View.java:4499) 
                         at android.view.View$PerformClick.run(View.java:18575) 
                         at android.os.Handler.handleCallback(Handler.java:733) 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                         at android.os.Looper.loop(Looper.java:136) 
                         at android.app.ActivityThread.main(ActivityThread.java:5021) 
                         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:827) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                         at dalvik.system.NativeStart.main(Native Method) 
                        Caused by: java.lang.NoSuchMethodException: onClear [class android.view.View] 
                         at java.lang.Class.getConstructorOrMethod(Class.java:472) 
                         at java.lang.Class.getMethod(Class.java:857) 
                         at android.view.View$1.onClick(View.java:3864) 
                         at android.view.View.performClick(View.java:4499)  
                         at android.view.View$PerformClick.run(View.java:18575)  
                         at android.os.Handler.handleCallback(Handler.java:733)  
                         at android.os.Handler.dispatchMessage(Handler.java:95)  
                         at android.os.Looper.loop(Looper.java:136)  
                         at android.app.ActivityThread.main(ActivityThread.java:5021)  
                         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:827)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)  
                         at dalvik.system.NativeStart.main(Native Method)  

ist der Code für die XML-Datei

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="20dp"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="What is ICT?" 
     android:textColor="@android:color/black" 
     android:textSize="20sp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     /> 

    <RadioGroup 
     android:id="@+id/radioGroup" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
      > 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Option 1" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
/> 

     <RadioButton 
      android:id="@+id/radioButton2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Option 2" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
/> 

     <RadioButton 
      android:id="@+id/radioButton3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Option 3" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
/> 
    </RadioGroup> 

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

     <Button 
      android:id="@+id/clearBtn" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:onClick="onClear" 
      android:text="Clear " /> 

     <Button 
      android:id="@+id/submitBtn" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:onClick="onSubmit" 
      android:text="Submit" /> 
    </LinearLayout> 
</LinearLayout> 
+1

Zeigen Sie das Layout für die Ansichten, auf die Sie klicken. –

+0

Die Methoden, die Sie im Layout-XML als 'onClick'-Attribute festlegen, müssen Mitglieder der 'Activity'-Klasse sein. Sie können nicht im "Fragment" sein. Es wäre wahrscheinlich vorzuziehen, den 'OnTouchListener' in diesem Fall dynamisch zu setzen. Das heißt, verwenden Sie die 'setOnClickListener()' Methode für Ihre 'Button' in der 'onCreateView()' Methode des 'Fragments, anstatt das XML Attribut zu setzen. –

+0

@DougStevenson Es tut mir leid, ich habe Ihre Antwort nicht bekommen. Willst du sagen, dass ich die XML-Datei oder ein Bild der App zeigen muss? – Dreamer

Antwort

1

Diese Schaltfläche in Ihrem Layout:

<Button 
     android:id="@+id/clearBtn" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:onClick="onClear" 
     android:text="Clear " /> 

Es ist zu sagen, dass, wenn es angeklickt, um ein Verfahren auszuführen genannt OnClear() in der Aktivität, die die Ansicht enthält.

Ihre Fehlermeldung besagt, dass in der Aktivität keine relevante Methode onClear() vorhanden ist. Sie haben ein onClear in Ihrem Fragment, aber das ist keine Aktivität. Es funktioniert nur mit Aktivitäten.

Anstatt mit android: onClick den Namen einer Methode anzugeben, ist es fast immer besser, ein OnClickListener-Objekt im Code zu registrieren.

+0

Wie kann ich den onclicklistener in meinem Code implementieren? – Dreamer

+0

Es gibt viele tolle Tutorials dazu. Suche und probiere es aus, wenn du ein spezifisches Problem hast, stelle eine andere Frage hier. –

+0

Ich bin gerade dabei. Vielen Dank – Dreamer

Verwandte Themen