我不断收到此java.lang.illegatStateException

时间:2019-07-23 18:29:04

标签: java android xml

我使用onClick:Showpoup在xml中创建了一个按钮,但该按钮无法运行,但仍然出现此错误,请帮助我解决此代码。

谢谢你们喜欢坦桑尼亚。

package com.labawsrh.aws.introscreen;

import android.app.Dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AccountFragment extends Fragment {

    Dialog myDialog;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View v = inflater.inflate (R.layout.fragment_account,container,false);



       myDialog = new Dialog(getActivity ());
        return  v;


    }

    public  void ShowPopup(View v)
    {

        TextView txtclose = (TextView)v.findViewById (R.id.txtclose);
        myDialog.setContentView (R.layout.custompop);
        txtclose.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                myDialog.dismiss ();
            }
        });
         myDialog.getWindow ().setBackgroundDrawable (new ColorDrawable 
 (Color.TRANSPARENT));
        myDialog.show ();

    }

}

XML代码:

<Button
       android:onClick="ShowPopup"
       android:textAllCaps="false"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/university"
        android:layout_marginTop="190dp"
        android:layout_marginLeft="90dp"/>

这一直显示在我的日志中

2019-07-23 21:08:45.776 6437-6437/com.labawsrh.aws.introscreen E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.labawsrh.aws.introscreen, PID: 6437
    java.lang.IllegalStateException: Could not find method ShowPopup(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
        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:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

2 个答案:

答案 0 :(得分:0)

请尝试这一希望对您有帮助

您需要找到button的值

 button = v.findViewById(R.id.button);

,然后使用按钮应用 onClickListener 。在 OnClick 功能中,只需调用 ShowPopup()

方法
  private void ShowPopup() {

    final Dialog dialog = new Dialog(getContext());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.custompop);
    dialog.getWindow().setBackgroundDrawable(new 
    ColorDrawable(Color.TRANSPARENT));

    Button ok = dialog.findViewById(R.id.ok);
    Button cancel = dialog.findViewById(R.id.cancel);
    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();


        }
    });

    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();

        }
    });
    dialog.show();
}

答案 1 :(得分:0)

  

我使用onClick:Showpoup在xml中创建了一个按钮,但该按钮无法运行,但我一直收到此错误

这是因为在Fragment上使用onClick xml属性时有一个古怪的现象(我能说这是一个错误吗?)。您不能将onClick属性与Fragment一起使用,它仅适用于Activity。因此,您需要改用View.setOnClickListener()