我在登录和注册课程中遇到同样的问题,每当我点击登录按钮时,我都会收到一些LogCat错误。有人可以向我解释错误以及如何摆脱它
我的登录类
package com.Flawed.hearmeout;
import java.util.ArrayList;
import java.util.List;
import library.JSONParser;
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 LoginScreen extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//setup input fields
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
//setup buttons
mSubmit = (Button)findViewById(R.id.login);
mRegister = (Button)findViewById(R.id.register);
//register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, SignUp.class);
startActivity(i);
break;
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(LoginScreen.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
//upon successful login, save username:
// Async json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(LoginScreen.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(LoginScreen.this, ReadComments.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;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(LoginScreen.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
我的LogCat
12-04 21:18:31.051: D/dalvikvm(1598): GC_FOR_ALLOC freed 267K, 10% free 9406K/10448K, paused 11ms, total 13ms
12-04 21:18:31.061: D/dalvikvm(1598): GC_FOR_ALLOC freed 95K, 11% free 9409K/10548K, paused 10ms, total 10ms
12-04 21:18:31.066: I/dalvikvm-heap(1598): Grow heap (frag case) to 11.018MB for 1127536-byte allocation
12-04 21:18:31.076: D/dalvikvm(1598): GC_FOR_ALLOC freed 1K, 10% free 10509K/11652K, paused 10ms, total 10ms
12-04 21:18:31.096: D/request!(1598): starting
12-04 21:18:31.096: W/dalvikvm(1598): threadid=12: thread exiting with uncaught exception (group=0x41fa7c08)
12-04 21:18:31.096: E/AndroidRuntime(1598): FATAL EXCEPTION: AsyncTask #1
12-04 21:18:31.096: E/AndroidRuntime(1598): Process: com.Flawed.hearmeout, PID: 1598
12-04 21:18:31.096: E/AndroidRuntime(1598): java.lang.RuntimeException: An error occured while executing doInBackground()
12-04 21:18:31.096: E/AndroidRuntime(1598): at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-04 21:18:31.096: E/AndroidRuntime(1598): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.lang.Thread.run(Thread.java:841)
12-04 21:18:31.096: E/AndroidRuntime(1598): Caused by: java.lang.NullPointerException
12-04 21:18:31.096: E/AndroidRuntime(1598): at com.Flawed.hearmeout.SignUp$CreateUser.doInBackground(SignUp.java:112)
12-04 21:18:31.096: E/AndroidRuntime(1598): at com.Flawed.hearmeout.SignUp$CreateUser.doInBackground(SignUp.java:1)
12-04 21:18:31.096: E/AndroidRuntime(1598): at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-04 21:18:31.096: E/AndroidRuntime(1598): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-04 21:18:31.096: E/AndroidRuntime(1598): ... 4 more
12-04 21:18:31.141: D/ProgressBar(1598): updateDrawableBounds: left = 0
12-04 21:18:31.141: D/ProgressBar(1598): updateDrawableBounds: top = 0
12-04 21:18:31.141: D/ProgressBar(1598): updateDrawableBounds: right = 96
12-04 21:18:31.141: D/ProgressBar(1598): updateDrawableBounds: bottom = 96
12-04 21:18:31.746: E/WindowManager(1598): android.view.WindowLeaked: Activity com.Flawed.hearmeout.SignUp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{429c0ab8 V.E..... R......D 0,0-684,192} that was originally added here
12-04 21:18:31.746: E/WindowManager(1598): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:467)
12-04 21:18:31.746: E/WindowManager(1598): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
12-04 21:18:31.746: E/WindowManager(1598): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-04 21:18:31.746: E/WindowManager(1598): at android.app.Dialog.show(Dialog.java:289)
12-04 21:18:31.746: E/WindowManager(1598): at com.Flawed.hearmeout.SignUp$CreateUser.onPreExecute(SignUp.java:89)
12-04 21:18:31.746: E/WindowManager(1598): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
12-04 21:18:31.746: E/WindowManager(1598): at android.os.AsyncTask.execute(AsyncTask.java:535)
12-04 21:18:31.746: E/WindowManager(1598): at com.Flawed.hearmeout.SignUp.onClick(SignUp.java:71)
12-04 21:18:31.746: E/WindowManager(1598): at android.view.View.performClick(View.java:4654)
12-04 21:18:31.746: E/WindowManager(1598): at android.view.View$PerformClick.run(View.java:19438)
12-04 21:18:31.746: E/WindowManager(1598): at android.os.Handler.handleCallback(Handler.java:733)
12-04 21:18:31.746: E/WindowManager(1598): at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 21:18:31.746: E/WindowManager(1598): at android.os.Looper.loop(Looper.java:146)
12-04 21:18:31.746: E/WindowManager(1598): at android.app.ActivityThread.main(ActivityThread.java:5602)
12-04 21:18:31.746: E/WindowManager(1598): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 21:18:31.746: E/WindowManager(1598): at java.lang.reflect.Method.invoke(Method.java:515)
12-04 21:18:31.746: E/WindowManager(1598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-04 21:18:31.746: E/WindowManager(1598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-04 21:18:31.746: E/WindowManager(1598): at dalvik.system.NativeStart.main(Native Method)
12-04 21:23:31.116: I/Process(1598): Sending signal. PID: 1598 SIG: 9
12-04 21:23:52.541: D/dalvikvm(2571): GC_FOR_ALLOC freed 71K, 9% free 9379K/10228K, paused 10ms, total 11ms
12-04 21:23:52.541: I/dalvikvm-heap(2571): Grow heap (frag case) to 10.989MB for 1127536-byte allocation
12-04 21:23:52.556: D/dalvikvm(2571): GC_FOR_ALLOC freed 1K, 8% free 10479K/11332K, paused 16ms, total 16ms
12-04 21:23:52.571: D/request!(2571): starting
12-04 21:23:52.576: W/dalvikvm(2571): threadid=11: thread exiting with uncaught exception (group=0x41fa7c08)
12-04 21:23:52.576: E/AndroidRuntime(2571): FATAL EXCEPTION: AsyncTask #1
12-04 21:23:52.576: E/AndroidRuntime(2571): Process: com.Flawed.hearmeout, PID: 2571
12-04 21:23:52.576: E/AndroidRuntime(2571): java.lang.RuntimeException: An error occured while executing doInBackground()
12-04 21:23:52.576: E/AndroidRuntime(2571): at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-04 21:23:52.576: E/AndroidRuntime(2571): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.lang.Thread.run(Thread.java:841)
12-04 21:23:52.576: E/AndroidRuntime(2571): Caused by: java.lang.NullPointerException
12-04 21:23:52.576: E/AndroidRuntime(2571): at com.Flawed.hearmeout.LoginScreen$AttemptLogin.doInBackground(LoginScreen.java:130)
12-04 21:23:52.576: E/AndroidRuntime(2571): at com.Flawed.hearmeout.LoginScreen$AttemptLogin.doInBackground(LoginScreen.java:1)
12-04 21:23:52.576: E/AndroidRuntime(2571): at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-04 21:23:52.576: E/AndroidRuntime(2571): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-04 21:23:52.576: E/AndroidRuntime(2571): ... 4 more
12-04 21:23:52.606: D/ProgressBar(2571): updateDrawableBounds: left = 0
12-04 21:23:52.606: D/ProgressBar(2571): updateDrawableBounds: top = 0
12-04 21:23:52.606: D/ProgressBar(2571): updateDrawableBounds: right = 96
12-04 21:23:52.606: D/ProgressBar(2571): updateDrawableBounds: bottom = 96
12-04 21:23:52.981: E/WindowManager(2571): android.view.WindowLeaked: Activity com.Flawed.hearmeout.LoginScreen has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42897a78 V.E..... R....... 0,0-684,192} that was originally added here
12-04 21:23:52.981: E/WindowManager(2571): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:467)
12-04 21:23:52.981: E/WindowManager(2571): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
12-04 21:23:52.981: E/WindowManager(2571): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-04 21:23:52.981: E/WindowManager(2571): at android.app.Dialog.show(Dialog.java:289)
12-04 21:23:52.981: E/WindowManager(2571): at com.Flawed.hearmeout.LoginScreen$AttemptLogin.onPreExecute(LoginScreen.java:108)
12-04 21:23:52.981: E/WindowManager(2571): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
12-04 21:23:52.981: E/WindowManager(2571): at android.os.AsyncTask.execute(AsyncTask.java:535)
12-04 21:23:52.981: E/WindowManager(2571): at com.Flawed.hearmeout.LoginScreen.onClick(LoginScreen.java:82)
12-04 21:23:52.981: E/WindowManager(2571): at android.view.View.performClick(View.java:4654)
12-04 21:23:52.981: E/WindowManager(2571): at android.view.View$PerformClick.run(View.java:19438)
12-04 21:23:52.981: E/WindowManager(2571): at android.os.Handler.handleCallback(Handler.java:733)
12-04 21:23:52.981: E/WindowManager(2571): at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 21:23:52.981: E/WindowManager(2571): at android.os.Looper.loop(Looper.java:146)
12-04 21:23:52.981: E/WindowManager(2571): at android.app.ActivityThread.main(ActivityThread.java:5602)
12-04 21:23:52.981: E/WindowManager(2571): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 21:23:52.981: E/WindowManager(2571): at java.lang.reflect.Method.invoke(Method.java:515)
12-04 21:23:52.981: E/WindowManager(2571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-04 21:23:52.981: E/WindowManager(2571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-04 21:23:52.981: E/WindowManager(2571): at dalvik.system.NativeStart.main(Native Method)
12-04 21:23:54.551: I/Process(2571): Sending signal. PID: 2571 SIG: 9
答案 0 :(得分:0)
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginScreen.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
String username = user.getText().toString();
String password = pass.getText().toString();
}
像这样使用..在onPreExecute()本身上获取用户名和密码
答案 1 :(得分:0)
我在您的实现中看到了两个主要的问题。在onPostExecute
你有
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(LoginScreen.this, file_url, Toast.LENGTH_LONG).show();
}
}
并在startActivity
中致电doInBackground
后致电。由于您当前的活动正在完成
pDialog.dismiss();
会导致WindowLeaked
例外。第二个
Toast.makeText(LoginScreen.this,
上下文可能不再有效,导致NPE
答案 2 :(得分:0)
你无法从doInBackground启动活动因为UI线程用完所以总是尝试从onPreExecute执行UI操作:
public void login(final Context context,final String username,final String password){
new AsyncTask<Void,Void,JSONObject>(){
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(Void... params) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
return jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
}
@Override
protected void onPostExecute(JSONObject response) {
super.onPostExecute(response);
if(pDialog!=null){
pDialog.dismiss();
}
if(response!=null){
try {
int success = response.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", response.toString());
// save user data
SharedPreferences sp = context.getSharedPreferences(context.getString(R.string.app_name), MODE_PRIVATE);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(context, ReadComments.class);
context.startActivity(i);
((Activity)context).finish();
}else{
Toast.makeText(context, response.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
}
}catch (JSONException e){
e.printStackTrace();
}
}
}
}.execute();
}
如何拨打登录信息:
case R.id.login:
login(this,user.getText().toString(),pass.getText().toString());
break;