onClick按钮崩溃

时间:2017-02-20 00:57:02

标签: java android

我刚刚开始使用Android Studio,并且我已经按照教程将我的按钮链接到网页,然而,按照它确定我的应用程序崩溃并且我收到了一堆错误,我&#39我试过在网上看,但我似乎找到了我做错了什么。我错过了什么吗?

这些是错误:

/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.josh.test2, PID: 2264
              java.lang.IllegalStateException: Could not find method browser1(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
                  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:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我遵循的代码:

Frag1.java

package com.example.josh.test2;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

import java.net.URI;
import java.util.zip.Inflater;



public class Frag1 extends Fragment {

View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.layout1, container, false);
    return myView;


    public View onCreateView(Bundle savedInstance){

        View rootView = Inflater.from(getContext()).inflate(R.layout.fragmet_view);

        Button browse = (Button) rootView.findViewById(R.id.button);
        browse.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent browserIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
                startActivity(browserIntent);
            }
        });

        return rootView;
    }
}

layout.xml

<Button
    android:text="Test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="18sp"
    android:id="@+id/button"/>

2 个答案:

答案 0 :(得分:0)

你不能在片段中制作方法,系统会在片段中的活动中搜索,所以你必须在不在片段中的活动中定义方法。

更好的解决方案是动态制作onClick方法:

 public View onCreateView(Bundle savedInstance){

 View rootView = Inflater.from(getContext()).inflate(R.layout.fragmet_view);

 Button browse = (Button) rootView.findViewById(R.id.button);
 browse.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v){
           Intent browserIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
           startActivity(browserIntent);
      }
 });

 return rootView;
 }

答案 1 :(得分:0)

首先,从任何布局XML中删除android:onClick="browser1"

然后,您可以从Java代码中添加按钮逻辑

public class YourFragment extends android.support.v4.app.Fragment {

    // TODO: Implement onCreateView

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 // Handle click
            }
        });
}