单击按钮时应用程序关闭

时间:2017-01-11 20:38:59

标签: android

我已经创建了一个按钮,在互联网上打开一个链接,我有一个按钮可以交换到另一个活动,两个代码都没有错误,但是当我运行应用程序并点击它们时,应用程序崩溃。

要关联的按钮:

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

按钮到活动:

View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.fifth_layout, container, false);
    return myView;
}
public void sendMessage(View view) {
    Intent myintent = new Intent(FifthFragment.this.getActivity(), startGamee.class);
    startActivity(myintent);

}

这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.jannes.app, PID: 23465
                  java.lang.IllegalStateException: Could not find method startgame(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button11'
                      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)
I/Process: Sending signal. PID: 23465 SIG: 9
Application terminated.

2 个答案:

答案 0 :(得分:0)

您在按钮属性上的值 android:onClick="startGame" 应该与单击按钮

调用的函数名称相同
public void startGame(View view){}

答案 1 :(得分:0)

请确保fourth_layout中有此

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttona1" 
    android:id="@+id/button11"/>

请参阅Android unable to find my onClick method

或者这个

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.fifth_layout, container, false);
    Button btn1 = (Button) myView.findViewById(R.id.button11);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               buttona1(); // call buttona1 method
            }
        });
        return myView;
    }

然后

public void buttona1(){
    Intent browserIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.nl"));
    startActivity(browserIntent);
}