Android Studio:无法解析方法isShowing()

时间:2018-02-02 20:44:18

标签: java android

我目前正在做一个DialogFragments教程,但我遇到了一些奇怪的事情。这是代码:

package com.ja.testofallthing;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import static android.app.AlertDialog.*;

public class MyAlertDialogFragment extends DialogFragment{

public MyAlertDialogFragment(){

}

public static MyAlertDialogFragment newInstance(String title){
    MyAlertDialogFragment frag = new MyAlertDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    String title = getArguments().getString("title");
    Builder builder = new Builder(getActivity());
    builder.setTitle(title);
    builder.setMessage("Are you sure?");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which){

        }
    } );
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       @Override
        public void onClick(DialogInterface dialog, int which){
           if(dialog != null && dialog.isShowing())
               dialog.dismiss();
       }
    });

    return builder.create();
}
}

此代码与教程中的代码完全相同,但我仍然收到错误:

error_message

看起来导入有问题,但我不知道是什么。我已经尝试将import android.app.Dialog;更改为import android.support.app.v4.Dialog;,因为教程建议使用其他导入,但是没有这样的库。提前谢谢。

编辑:这是我的android mainfest依赖项文件,因为这里可能有错误:     申请插件:' com.android.application'

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.ja.testofallthing"
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

2 个答案:

答案 0 :(得分:1)

您需要将std::byte对象转换为dialog类型,并且您的代码应该有效:

android.app.Dialog

您应首先检查((Dialog) dialog).isShowing()对象是否为dialog类型。

答案 1 :(得分:1)

DialogInterface只是一个回调界面,只提供两种方法:cancel& dismiss

在您的情况下,您不必检查对话框是否仍然可见,此检查已在dismiss方法内实施:

public class Dialog implements DialogInterface... {

    [...]

    @Override
    public void dismiss() {
        if (Looper.myLooper() == mHandler.getLooper()) {
            dismissDialog();
        } else {
            mHandler.post(mDismissAction); // calls dismissDialog from UI-Thread
        }
    }

    void dismissDialog() {
        if (mDecor == null || !mShowing) {
            return;
        }
        [...]
    }
}