Android应用程序在模拟器中运行但不在Android设备中运行

时间:2013-03-20 18:47:23

标签: android android-emulator samsung-mobile

我已经尝试了一整天用相关研究解决这个问题。我能够在模拟器中运行我的应用程序,但不能在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;
                        }
        }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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...

答案 2 :(得分:0)

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

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />