2013-08-30 106 views
0

我試圖運行我的應用程序,但我收到一條消息「不幸的應用程序已停止」當我在錯誤logcat中的過程出現錯誤致命異常Asynctask#1爲什麼?任何解決方案嗎?不幸的示例應用程序已停止android模擬器

這裏是一個完整的logcat

08-30 09:15:30.957: E/AndroidRuntime(580): FATAL EXCEPTION: AsyncTask #1 
08-30 09:15:30.957: E/AndroidRuntime(580): java.lang.RuntimeException: An error occured while executing doInBackground() 
08-30 09:15:30.957: E/AndroidRuntime(580): at android.os.AsyncTask$3.done(AsyncTask.java:278) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
08-30 09:15:30.957: E/AndroidRuntime(580): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.lang.Thread.run(Thread.java:856) 
08-30 09:15:30.957: E/AndroidRuntime(580): Caused by: java.lang.NullPointerException 
08-30 09:15:30.957: E/AndroidRuntime(580): at com.example.example.Login$AttemptLogin.doInBackground(Login.java:125) 
08-30 09:15:30.957: E/AndroidRuntime(580): at com.example.example.Login$AttemptLogin.doInBackground(Login.java:1) 
08-30 09:15:30.957: E/AndroidRuntime(580): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
08-30 09:15:30.957: E/AndroidRuntime(580): ... 5 more 

Login.java

package com.example.example; 

import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Login extends Activity implements OnClickListener { 

private EditText user, pass; 
private Button mSubmit, mRegister; 
private ProgressDialog pDialog; 
JSONParser jsonParser = new JSONParser(); 

private static final String LOGIN_URL = "http://10.0.2.2/android/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.login); 

user = (EditText) findViewById(R.id.username); 
pass = (EditText) findViewById(R.id.password); 

mSubmit = (Button) findViewById(R.id.login); 
mRegister = (Button) findViewById(R.id.register); 

mSubmit.setOnClickListener(this); 
mRegister.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 

switch (v.getId()) { 
case R.id.login: 
    new AttemptLogin().execute(); 
    break; 
case R.id.register: 
    Intent i = new Intent(this, Register.class); 
    startActivity(i); 
    break; 

default: 
    break; 
} 
} 

class AttemptLogin extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    pDialog = new ProgressDialog(Login.this); 
    pDialog.setMessage("Attempting login..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(true); 
    pDialog.show(); 
} 

@Override 
protected String doInBackground(String... v) { 

    int success; 
    String username = user.getText().toString(); 
    String password = pass.getText().toString(); 
    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("Login Successful!", json.toString()); 

      SharedPreferences sp = PreferenceManager 
        .getDefaultSharedPreferences(Login.this); 
      Editor edit = sp.edit(); 
      edit.putString("username", username); 
      edit.commit(); 

      Intent i = new Intent(Login.this, ReadComment.class); 
      finish(); 
      startActivity(i); 
      return json.getString(TAG_MESSAGE); 
     } else { 
      Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
      return json.getString(TAG_MESSAGE); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return null; 

} 

protected void onPostExecute(String file_url) { 

    pDialog.dismiss(); 
    if (file_url != null) { 
     Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show(); 
    } 

} 

} 

} 
+0

請添加代碼,並指向NPE發生的位置。 – flx

+0

我應該改變什麼?我畢竟不明白,因爲我是Android新手請問 –

+0

哪行是125行? – flx

回答

2

您在文件Login.java 125線得到了NullPointerException。轉到您的代碼並嘗試確定代碼中此行的值如何爲空。

我怎麼知道這個?

跟蹤包含以下代碼:

Caused by: java.lang.NullPointerException 
... at com.example.example.Login$AttemptLogin.doInBackground(Login.java:125) 
0

試試這個: -

class AttemptLogin extends AsyncTask<String, void, String> 

通過異步任務使用的三種類型如下:

  1. PARAMS,類型在執行時發送給任務的參數。
  2. 進度,後臺計算期間發佈的進度單位的類型。
  3. 結果,後臺計算結果的類型。
+0

仍然是一樣的沒有發生 –

+0

確保你收到一個非null的json .. 編輯這個「http://10.0.2.2/android/login.php」..在POST中更改POST以獲取該文件並回顯結果json 使用你的瀏覽器.. goto http://10.0.2.2/android/login.php/username=blah&password=blah 確保有返回的東西@LuhungHaryo –

相關問題