自定义对话框在片段中有上下文错误

时间:2019-05-30 08:20:04

标签: android android-fragments android-webview android-dialog

我正在使用Instagram实现社交登录功能。

似乎我需要打开WebView,然后获取访问令牌。

这是Instagram document

我正在尝试显示包含WebView的对话框。然后,如果用户登录/授权,则该URL包含access-token。而且我想将其保存到我的应用中并保存。

这就是我实现的目标。

SignInFragment.kt

    override fun signInInstagram() {
        Timber.d("signInInstagram()")
        if (activity!!.isFinishing) {
            authDialog = AuthenticationDialog(activity!!, this)
            authDialog.setCancelable(true)
            authDialog.show()
        } else {
            Timber.d("No Activity")
            Toast.makeText(activity, "No Activity", Toast.LENGTH_SHORT).show()
        }
    }
class AuthenticationDialog(context: Context, var listener: AuthenticationListener) : Dialog(context) {
    private var webView: WebView? = null

    //    private final String url = "https://naver.com";

    private val url = "${Constants.INSTAGRAM_URL}oauth/authorize/" +
                    "?client_id=${Constants.CLIENT_ID}" +
                    "&redirect_uri=${Constants.REDIRECT_URI}" +
                    "&response_type=token" +
                    "&display=touch&scope=public_content"

    init {
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
            Timber.d("start")
        }
    }

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.dialog_insta_auth)
        Timber.d("url:${url}")

        initializeWebView()
    }

    private fun initializeWebView() {
        webView = findViewById(R.id.web_view)
        webView!!.loadUrl(url)
        webView!!.webViewClient = object : WebViewClient() {

            var authComplete = false
            var access_token: String? = null

            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
                super.onPageStarted(view, url, favicon)
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)

                if (url.contains("#access_token=") && !authComplete) {
                    val uri = Uri.parse(url)
                    access_token = uri.encodedFragment
                    Timber.d("access token: ${access_token}")
                    // get the whole token after the '=' sign
                    access_token = access_token!!.substring(access_token!!.lastIndexOf("=") + 1)
                    Log.i("", "CODE : " + access_token!!)
                    authComplete = true
                    listener.onCodeReceived(access_token!!)
                    dismiss()

                } else if (url.contains("?error")) {
                    Toast.makeText(context, "Error Occured", Toast.LENGTH_SHORT).show()
                    dismiss()
                }
            }
        }
    }
}

public class Constants {
    companion object {
        // Instagram
        const val INSTAGRAM_URL = "https://api.instagram.com/"
        const val CLIENT_ID = "xxxxxxxxxxxxxxxx"
        const val REDIRECT_URI = "https://www.instagram.com/"
    }
}

但是,问题是我得到了错误,因为上下文似乎不正确。 signInInstagram()SignInFragment中。 SignInFragment属于MainActivity。因此,我认为activity!!必须有效。但事实并非如此。我尝试使用applicationContextbaseContext,但它们都不起作用。

如果我删除activity!!.isFinishing。我收到此错误:

Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState

我该如何解决?

0 个答案:

没有答案