2017-01-12 5 views
-4

エラーは、Appは、ボタンを

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.jannes.app, PID: 16569 
        java.lang.IllegalStateException: Could not find method buttona1(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2' 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
         at android.view.View.performClick(View.java:5697) 
         at android.widget.TextView.performClick(TextView.java:10814) 
         at android.view.View$PerformClick.run(View.java:22526) 
         at android.os.Handler.handleCallback(Handler.java:739) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:158) 
         at android.app.ActivityThread.main(ActivityThread.java:7229) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

をクリックすると、このアプリを実行しているときに私が取得エラーですクラッシュ、私は複数のボタンを作り、中に別のアクティビティに(インターネットサイトへのリンクボタンとボタンが動作しませんアプリ)。うまくいけば誰でも私を助けることができる。私は全く新しいプログラミングですので、幼稚な言葉を使ってください。

レイアウト

<Button 
    android:text="Magister" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="buttona1" 
    android:id="@+id/button2" 
    android:visibility="visible" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

Javaファイル

public class FourthFragment extends Fragment{ 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fourth_layout, container, false); 
} 

@SuppressWarnings("unused") // it's actually used, just injected by Butter Knife 
public void buttona1(View view){ 
    Intent browserIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.nl")); 
    getActivity().startActivity(browserIntent); 
} 
+1

が問題にあまりにもあなたのコードを貼り付けてください忘れないでください。 –

+0

@ Jannes van den Bogert、おそらくあなたは、間違ったボタンid –

+0

でbuttona1()メソッドを参照しています。 – surya

答えて

1

あなたのonCreateでこのような何かを書くことができます。

button= (Button) findViewById(R.id.button2); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     //your code 
     } 
    }); 
1

、これであなたのonCreateView方法を交換し

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fourth_layout, container, false); 
    final Button button2 = (Button) view.findViewById(R.id.button2); 
    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent browserIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.nl")); 
      startActivity(browserIntent); 
     } 
    }); 
    return view; 
} 

とXMLから属性を削除します。 これに従うとbuttona1メソッドは必要ありません。

0

@SuppressWarnings( "未使用")//それは実際に使われている、ちょうどバターナイフによって注入

バターナイフ

ButterKnifeためには、単に次のように使用することができます

@OnClick(R.id.button2) 
public void onClick(){ 

} 

これを削除する

android:onClick="buttona1" 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    ButterKnife.bind(this, view); 
} 
関連する問題