我正在研究一個具有user login
屏幕的應用程序。每當我點擊登錄按鈕,該應用程序崩潰。至於,我是新的Android應用程序開發組成的登錄屏幕類型的應用程序;因此無法修復錯誤。請看看錯誤並幫我修復它。附着LoginActivity.java
文件:登錄時應用程序崩潰
public class LoginActivity1 extends Activity implements OnClickListener{
private EditText user, pass;
private Button bLogin;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://13.228.8.241/phonegap/api/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_login1);
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
bLogin = (Button)findViewById(R.id.signin);
bLogin.setOnClickListener(this);
}
@Override public void onClick(View v) {
String username= user.getText().toString();
String password=pass.getText().toString();
switch (v.getId()) {
case R.id.signin:
new AttemptLogin().execute(username,password);
case.default: break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/** * Before starting background thread Show Progress Dialog * */
boolean failure = false;
@Override protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity1.this);
pDialog.setMessage("Attempting for login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override protected String doInBackground(String... args) {
int success;
String username = args[0];
String password = args[1];
try { List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());
Intent i = new Intent(LoginActivity1.this,ContentSecond.class);
startActivity(i);
finish();
return json.getString(TAG_MESSAGE);
}
else{
return json.getString(TAG_MESSAGE);
}
}
catch (JSONException e) {
e.printStackTrace();
} return null;
}
protected void onPostExecute(String message) {
pDialog.dismiss();
if (message != null){
Toast.makeText(LoginActivity1.this, message, Toast.LENGTH_LONG).show();
}
}
}
}
的logcat的是:
致命異常:的AsyncTask#1 工藝:com.adani.animon.adani,PID:8678 了java.lang.RuntimeException:執行時發生錯誤 doInBackground() at android.os.AsyncTask $ 3.done(AsyncTask.java:309) at java.util.con current.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run (ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) 引起:java.lang.NullPointerException:試圖調用虛擬 method'java.l在 com.adani.animon.adani.LoginActivity1 $ AttemptLogin.doInBackground(LoginActivity1.java:76) ang.String org.json.JSONObject.toString()」上的空 對象引用 在 com.adani.animon .adani.LoginActivity1 $ AttemptLogin.doInBackground(LoginActivity1.java:53) at android.os.AsyncTask $ 2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:588) 在java.lang.Thread.run(Thread.java:818) 08-17 14:58:23.089 8678-8842/COM .adani.animon.adani d/libEGL:eglTerminate EGLDisplay = 0xe0362834 08-17 14:58:23.089 8678-8678/com.adani.animon.adani d/ViewRootImpl:#3 MVIEW = NULL 08-17 14時58分:23.099 8678-8678/com.adani.animon.adani E/WindowManager:android.view.WindowLeaked:Activity com.adani.animon.adani.LoginActivity1已泄露窗口 com.android.internal.policy.PhoneWindow $ DecorView { c367582 VE ..... R ...... D 0,0-668,232}原來在這裏加入 at android.view.ViewRootImpl。(ViewRootImpl.java:599) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) 在android.app.Dialog.show(Dialog.java:505) 在 com.adani.animon.adani.LoginActivity1 $ AttemptLogin。onPreExecute(LoginActivity1.java:62) 在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604) 在android.os.AsyncTask.execute(AsyncTask.java:551) 在 com.adani.animon.adani .LoginActivity1.onClick在android.view.View(LoginActivity1.java:45) 在android.view.View.performClick(View.java:5714) 在android.widget.TextView.performClick(TextView.java:10926) $ PerformClick.run(View.java:22589) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at androi d.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com。 android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
doInBackground()方法有一些錯誤。應用程序由於空對象而崩潰。嘗試用斷點調試代碼。 –
感謝您的關注兄弟,但我並不擅長調試。我已經嘗試了很多代碼來解決這個問題但失敗了。 –
您必須進行調試才能知道錯誤在哪裏。搜索SO或谷歌有關調試 –