单击按钮时应用程序崩溃

时间:2017-01-12 09:26:48

标签: android exception button

错误

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);
}

3 个答案:

答案 0 :(得分:1)

你可以在onCreate中写这样的东西。

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

答案 1 :(得分: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中删除android:onClick属性。 如果您遵循此方法,则不需要buttona1方法。

答案 2 :(得分:0)

  

@SuppressWarnings(&#34;未使用&#34;)//实际使用,只是由Butter Knife注入

     

黄油刀

对于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);
}