从string.xml获取字符串时发生NullPointerException

时间:2018-10-25 20:00:42

标签: android kotlin nullpointerexception

我有问题。为什么我有NullPointerException?

这是我的调试器日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: bjd.adrian.e_shop, PID: 5301
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
    at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
    at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
    at android.content.Context.getString(Context.java:476)
    at bjd.adrian.e_shop.LoginView.loggedIn(LoginView.kt:33)
    at bjd.adrian.e_shop.LoginPresenter.endLoginisLogged(LoginPresenter.kt:25)
    at bjd.adrian.e_shop.DbManager$login$stringRequest$2.onResponse(DbManager.kt:48)
    at bjd.adrian.e_shop.DbManager$login$stringRequest$2.onResponse(DbManager.kt:35)
    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
    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:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

这是我的代码:

class LoginView : AppCompatActivity()
{

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login_view)


        loginBTT.setOnClickListener {
            val loginPresenter = LoginPresenter()
            var userName = userNameET.text.toString()
            var password = passwordET.text.toString()

            loginPresenter.login(userName, password, this@LoginView)

        }
    }


    fun loggedIn()
 {
        val logged = this@LoginView.getString(R.string.logged)
        Toast.makeText(this@LoginView, logged, Toast.LENGTH_LONG).show()
}

    fun unLogged()
    {
        val invalidUserOrPass = this@LoginView.getString(R.string.invalidUSorPASS)
        Toast.makeText(this@LoginView, invalidUserOrPass, Toast.LENGTH_LONG).show()
}

我有两种语言的 strings.xml (英语为主要语言和波兰语)。

这是DbManager:

package bjd.adrian.e_shop

class DbManager
{
    val TAG: String = "DbManager"


    var invalidUSorPass = "invalidUserNameOrPassword"
    var signedIn = "signedIn"


    fun login(username: String, password: String, context: Context)
    {
        val loginPresenter = LoginPresenter()
        val requsetQueue = Volley.newRequestQueue(context)

        val url = "https://shoppingcentreeshop.000webhostapp.com/androidAppScripts/hashedLogin.php"


        var stringRequest = object : StringRequest(Method.POST, url,
            Response.Listener<String>{ response ->
                if(response.equals("ERROR"))
                {
                    Log.v(TAG, invalidUSorPass)
                    loginPresenter.endLoginisntLogin()
                }
                else
                {
                    Log.v(TAG, signedIn)

                    val gson = GsonBuilder().create()
                    var loginJSONdata = gson.fromJson<LoginJSONdata>(response, LoginJSONdata::class.java)
                    loginPresenter.endLoginisLogged()

                }

            }, object : Response.ErrorListener
            {
                override fun onErrorResponse(error: VolleyError?)
                {
                    Log.e(TAG, error.toString())
                }
            }){
            override fun getParams(): MutableMap<String, String>
            {
                var params = HashMap<String, String>()

                params.put("user_name", username)
                params.put("user_pass", password)

                return params
            }

        }


    requsetQueue.add(stringRequest)

}
}

2 个答案:

答案 0 :(得分:0)

尝试用这种方式。

val logged: String? = this@LoginView.getString(R.string.logged)

val invalidUserOrPass: String? = this@LoginView.getString(R.string.invalidUSorPASS)

这未经测试,但值得一试

答案 1 :(得分:0)

在堆栈跟踪中,您拥有

at bjd.adrian.e_shop.LoginPresenter.endLoginisLogged(LoginPresenter.kt:25)

这意味着在LoginPresenter类中,您已经制作了new LoginView()loginView.loggedIn(),这是您不应该做的。

您可以首先尝试在入门者的Volley响应方法中直接Toast,以便删除所有这些复杂的Model-View-Presenter内容。