我的应用程序在虚拟设备中运行良好但是当我在实际设备上运行时,我得到“我的应用程序停止工作”错误..并且日志中出现此错误
06-07 12:20:09.022 28371-28371/my.amirul.psm E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException at my.amirul.psm.BackgroundTask.onPostExecute(BackgroundTask.java:113)
at my.amirul.psm.BackgroundTask.onPostExecute(BackgroundTask.java:22)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
这是我的后台任务代码......
public class BackgroundTask extends AsyncTask<String,Void,String>{
AlertDialog alertDialog;
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information......");
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://10.0.2.2/webapp/register.php";
String login_url = "http://10.0.2.2/webapp/login.php";
String method = params[0];
if(method.equals("register"))
{
String name = params[1];
String user_name = params[2];
String user_pass = params [3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8") + "=" + URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("user_name","UTF-8") + "=" + URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_pass","UTF-8") + "=" + URLEncoder.encode(user_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if (method.equals("login"))
{
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection =(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line ;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Registration Success...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} else if(result.contains("Hello")) {
Intent i = new Intent(ctx, FormActivity.class);
ctx.startActivity(i);
} else {
alertDialog.setMessage(result);
alertDialog.show();
}
}
}
请帮助我...
答案 0 :(得分:1)
实际上,网络不可用,并且在与网络连接时系统收到null。在模拟器上,您正在使用系统的网络。我希望它会对你有所帮助。
答案 1 :(得分:0)
将您的设备连接到网络,即WiFi /数据连接。然后
假设您在同一个无线网络上......
找到您当地的IP地址(应该是 192.168.1.x ) - 转到命令提示符并输入&#39; ipconfig&#39;得到这个。其中x是服务器计算机的已分配本地IP。 使用本地IP地址为您的Android设备连接到您的本地主机。
它对我有用。
注意:要在模拟器上访问localhost:10.0.2.2。但是,如果您拥有除Web服务器之外的其他内容(例如XMPP服务器),则可能并不总是有效。
这可能对你有所帮助。
答案 2 :(得分:0)
我是唯一一个看到
的人return null;
在doInBackground结束时的每个if / else语句中?
如果成功,那没关系,因为第一个if语句中有return "Registration Success...";
,但如果失败,则为null,因此空指针异常。