0

我一整天都在尝试通过相关研究来解决这个问题。我可以在模拟器中运行我的应用程序,但不能在 android 设备中运行,即三星 Galaxy ace 2。我在 logcat 中得到以下错误。非常感谢您的帮助。谢谢!

03-20 18:40:07.340: E/AndroidRuntime(15785): FATAL EXCEPTION: main
03-20 18:40:07.340: E/AndroidRuntime(15785): java.lang.NullPointerException
03-20 18:40:07.340: E/AndroidRuntime(15785):    at com.dorji.finalproject.LoginLayout$MyAsyncTask.onPostExecute(LoginLayout.java:87)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at com.dorji.finalproject.LoginLayout$MyAsyncTask.onPostExecute(LoginLayout.java:1)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at android.os.AsyncTask.finish(AsyncTask.java:417)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at android.os.Looper.loop(Looper.java:130)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at android.app.ActivityThread.main(ActivityThread.java:3691)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at java.lang.reflect.Method.invoke(Method.java:507)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
03-20 18:40:07.340: E/AndroidRuntime(15785):    at dalvik.system.NativeStart.main(Native Method)

主要错误代码:

     @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new newAsyncTask().execute();
        }
    });
}

private class newAsyncTask extends AsyncTask<Void, Void, Void>
{
        ProgressDialog Dialog;
        String user = username.getText().toString();
        String pass = password.getText().toString();



        @Override
        protected void onPreExecute() {
                Dialog = ProgressDialog.show(Login.this, "processing..", "data is Loading...");
        }


        @Override
                protected void onPostExecute(Void result) {
                    if(res.equals("1")){
                        if (Dialog.isShowing()) {
                        Dialog.dismiss();
                        }

                } else{
                        error.setText("Sorry, wrong username and password");
                        Dialog.dismiss();
                }
                }

        @Override
        protected Void doInBackground(Void... params) {


             ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
             postParameters.add(new BasicNameValuePair("username", user));
             postParameters.add(new BasicNameValuePair("password", pass));

                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8080/MainServer/LoginResponse", postParameters);
                    res=response.toString();
                    res= res.replaceAll("\\s+","");
                    Bundle bundle = new Bundle();
                    bundle.putString("param1", user);
                    bundle.putString("param2", pass);

                    Intent newIntent = new Intent(getApplicationContext(), MainActivity.class);
                    newIntent.putExtras(bundle);
                    startActivityForResult(newIntent, 0);

                        } catch (Exception e) {
                            System.out.println(e.toString());
                            }
                        return null;
                        }
        }
4

3 回答 3

0

您的设备无法访问“10.0.2.2:8080”,因此您进入异常并从 doInBackground 返回 null。在 onPostExecute 中,您尝试在 null 上运行方法 equals(),并获得空指针异常

于 2013-03-20T18:58:36.017 回答
0

我不是专家,但也许这可以帮助你:

1.- 添加处理程序:

private Handler myHndlr = null;

2.- 初始化它:

    public void onClick(View v) {
       myHndlr = new Handler();
       //...

3.- 在你的 AsyncTask 中使用它:

    @Override
    protected void onPostExecute(Void result)
    {
        if(res.equals("1"))
        {
            myHndlr.post(new Runnable(){
               @Override
               public void run()
               {
                  //Code that modify items on the screen and stuff
                  if(Dialog.isShowing())
                  {
                       Dialog.dismiss();
                  }
               }
           });
         }
         //The rest of the code here...
于 2013-03-20T19:17:55.867 回答
0

在您的 Android Manifest 中,更改与您设备的 android 版本对应的目标 sdk (targetSdkVersion)。它应该可以正常工作。

例如,Android 版本 4.2.2 的 API 版本是 API 级别 17。因此,在您的 Android Manifest 中更改以下内容

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
于 2013-11-29T11:59:15.313 回答