猿问

如何调试 Android 代码以获得意外值?

我是 Android 新手,在 Android Studio 上做了一些基本活动。我有以下代码,它会在成功登录后从 LoginActivity 重定向到 Navigation 活动。

如果凭据正确,则loginPassed变量设置为 true,否则为 false。

如果我在上面的程序中删除if条件 if 将正确重定向到 NavigationActivity 但如果条件到位,它将简单地关闭应用程序。

不知道我哪里错了。


扬帆大鱼
浏览 188回答 2
2回答

慕桂英4014372

您声明了loginPassedvar,但永远不要在其中放置值。当您创建一个booleanvar 时,默认值是false,它们运行时永远不会输入if(loginPassed)

慕仙森

首先你必须定义一个全局变量 loginPassedBoolean loginPassed;然后你必须执行 AsyncTask 来检查这样的用户凭据 -new UserLoginTask().execute();现在在 AsyncTask 中,我们将检查用户的凭据并进行相应的操作class UserLoginTask extends AsyncTask<Void, Integer, String>{&nbsp; &nbsp;String TAG = getClass().getSimpleName();&nbsp; &nbsp;protected void onPreExecute (){&nbsp; &nbsp; &nbsp; super.onPreExecute();&nbsp; &nbsp; &nbsp; Log.d(TAG + " PreExceute","On pre Exceute......");&nbsp; &nbsp; }&nbsp; &nbsp;protected String doInBackground(Void...arg0) {&nbsp; &nbsp; Log.d(TAG + " DoINBackGround","On doInBackground...");&nbsp; &nbsp; if(validate(email, password))&nbsp; &nbsp; &nbsp; &nbsp; loginPassed = true;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; loginPassed = false;&nbsp; &nbsp; return "";&nbsp; &nbsp;}&nbsp; protected void onProgressUpdate(Integer...a){&nbsp; &nbsp; super.onProgressUpdate(a);&nbsp; &nbsp; Log.d(TAG + " onProgressUpdate", "You are in progress update ... " + a[0]);&nbsp; }&nbsp; protected void onPostExecute(String result) {&nbsp; &nbsp; super.onPostExecute(result);&nbsp; &nbsp; Log.d(TAG + " onPostExecute", "" + result);&nbsp; &nbsp; if(loginPassed){&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(this, NavigationActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_MESSAGE, "Jefry");&nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; // There was an error; don't attempt login and focus the first&nbsp; &nbsp; &nbsp; &nbsp; // form field with an error.&nbsp; &nbsp; &nbsp; &nbsp; focusView.requestFocus();&nbsp; &nbsp; &nbsp;}&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答