关闭Android中的VM和致命异常

时间:2014-07-16 07:19:31

标签: java android

我每次看到一只日志猫时都尝试过有消息致命异常并关闭虚拟机,因为有一些关于此错误的解决方案我无法找到它请帮助我解决它我是新的在android中所以可能是我有一个小错误所以给我解决方案

以下是我的代码

main.java

package com.login.demo;
import impl.dao.InspectionVO;
import impl.dao.Inspection_damages;
import impl.dbOperation.Operation;
import impl.internetcheck.CkeckNetConnection;
import impl.urlConnection.CommonUtill;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
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;

import com.google.gson.Gson;

public class main extends Activity implements OnClickListener
{

    EditText unm, pwd;
    String webResponse1 = "NA";
    String uname, password;
    Operation op = new Operation(this);
    SharedPreferences pref;
    static String jsonstr;
    public static String msg = "nothing";


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        unm = (EditText) findViewById(R.id.editText1);
        pwd = (EditText) findViewById(R.id.editText2);
        Button lgn = (Button) findViewById(R.id.button1);

        op.getContainerid();


        //****Check for Connection at First time********
        lgn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        uname = unm.getText().toString().trim();
        password=  pwd.getText().toString().trim();

        if (v.getId() == R.id.button1)
        {
            String status = CkeckNetConnection.getConnectivityStatusString(main.this);
            if (status.equals("Mobile"))
            {
                MyAsynTask obj = new MyAsynTask();
                //Testing?param1=admin&param2=admin
                String login = "firstLogin?param1=" + uname + "&param2=" + password;

                //String login = "Testing?param1="+uname+"&param2="+password;
                //  String SubURL ="insertInsp?param1="+str1;
                Log.i("check 1", "Info 1");
                try
                {
                    msg = "Login..";
                    obj.execute(login).get();
                    Log.i("Login object", "Text set : " + jsonstr);
                    if (jsonstr.equals("true"))
                    {
                        Log.i("ketan", "Inside condition");
                        String inspid = op.getContainerid();
                        Log.i("id", inspid);
                        int ids=Integer.parseInt(inspid);
                        int id=1;
                        if(ids>0)
                        {
                            //Log.i("Test1","Here");
                            while(id<=ids)
                            {
                                InspectionVO insp=op.getInspection(id);
                                Gson gson=new Gson();
                                //String jsonStr=gson.toJson(insp);

                                StringBuilder tmp = new StringBuilder();
                                tmp.append(gson.toJson(insp));
                                Log.i("Stringssss", tmp.toString());



                                StringBuilder sb = new StringBuilder();
                                sb.append("insertInsp?param1="+java.net.URLEncoder.encode(tmp.toString()));

                                MyAsynTask inspection=new MyAsynTask();
                                inspection.execute(sb.toString()).get();


                                ArrayList<Inspection_damages> ar=op.getImages(id);
                                for(Integer k=0;k<ar.size();k++)
                                {
                                    ar.get(k).setInsp_id(jsonstr);
                                    StringBuilder tmp1 = new StringBuilder();
                                    tmp1.append(gson.toJson(ar.get(k)));
                                    Log.i("Stringssss", ar.get(k).getD_code());
                                    Log.i("Stringssss", tmp1.toString());

                                    StringBuilder sb1 = new StringBuilder();
                                    sb1.append("insertInspDamage?param1="+java.net.URLEncoder.encode(tmp1.toString()));
                                    Log.i("left", sb1.toString());
                                    MyAsynTask obj1=new MyAsynTask();
                                    obj1.execute(sb1.toString()).get();

                                }

                                id++;


                            }

                        }
                        /*    msg="recieving data..";
                        String uid = op.getusersid();
                        Log.i("Response","check it"+uid);
                        MyAsynTask user = new MyAsynTask();
                        String users = "getUsers?param1="+uid;
                        user.execute(users).get();
                        Log.i("Response", jsonstr);
                        Gson gson = new Gson();

                        Type type = new TypeToken<ArrayList<UserVO>>() {}.getType();
                        ArrayList<UserVO> ar = gson.fromJson(jsonstr,type);
                        if(!(ar==null))
                        {
                            op.InsertUsers(ar);
                        }
                        /*    MyAsynTask cont=new MyAsynTask();
                        String conts="getCT?";
                        cont.execute(conts).get();
                        Log.i("Response", jsonstr);


                        Type types = new TypeToken<ArrayList<InspectionVO>>() {}.getType();
                        ArrayList<InspectionVO> cnar= gson.fromJson(jsonstr,types);
                        op.InsertCT(cnar);*/

                        Intent i=new Intent(getApplicationContext(),mainbutton.class);
                        startActivity(i);
                        Toast.makeText(getApplicationContext(), "successfully login",10).show();
                    }
                    else
                    {
                        Log.i("Login status", "fail");
                        Toast.makeText(main.this, "Wrong username or password", Toast.LENGTH_LONG).show();
                        unm.setText("");
                        pwd.setText("");
                    }

                    /*String users="getUsers?";
                    obj.execute(users).get();
                    Log.i("Response", jsonstr);
                    Gson gson = new Gson();

                    Type type = new TypeToken<ArrayList<UserVO>>() {}.getType();
                    ArrayList<UserVO> ar = gson.fromJson(jsonstr,type);
                    */ 
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                } catch (ExecutionException e)
                {
                    e.printStackTrace();
                }
            }
            else if(status.equals("Not"))
            {
                Intent i=new Intent(getApplicationContext(),mainbutton.class);
                startActivity(i);
                Toast.makeText(getApplicationContext(), "successfully login",10).show();
                /*    Toast.makeText(main.this, "No Net Connection", Toast.LENGTH_LONG).show();
                unm.setText("");
                pwd.setText("");*/
            }
        }
    }

    private class MyAsynTask extends AsyncTask<String, Void, String>
    {
        ProgressDialog Asycdialog = new ProgressDialog(main.this);

        @Override
        protected void onPreExecute() {
            //set message of the dialog
            Asycdialog.setMessage(msg);
            //show dialog
            Asycdialog.show();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params)
        {
            //String webResponse= CommonUtill.getOutputFromUrl(CommonUtill.URL+"checkLogin?param1=admin&param2=admin");
            jsonstr=CommonUtill.getOutputFromUrl(CommonUtill.URL+params[0]);


            // TODO Auto-generated method stub
            return null;
        }





        protected void onPostExecute(String result)
        {
            //hide the dialog
            Asycdialog.dismiss();

            super.onPostExecute(result);
        }
    }
}

Logcat

07-16 02:08:18.065: D/AndroidRuntime(2160): Shutting down VM
07-16 02:08:18.065: W/dalvikvm(2160): threadid=1: thread exiting with uncaught exception (group=0xb3eae648)
07-16 02:08:18.095: E/AndroidRuntime(2160): FATAL EXCEPTION: main
07-16 02:08:18.095: E/AndroidRuntime(2160): java.lang.NullPointerException: println needs a message
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.util.Log.println_native(Native Method)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.util.Log.i(Log.java:159)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at com.login.demo.main.onClick(main.java:87)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.view.View.performClick(View.java:4240)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.view.View$PerformClick.run(View.java:17721)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.os.Handler.handleCallback(Handler.java:730)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.os.Looper.loop(Looper.java:137)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at java.lang.reflect.Method.invoke(Method.java:525)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-16 02:08:18.095: E/AndroidRuntime(2160):     at dalvik.system.NativeStart.main(Native Method)
07-16 02:08:29.686: I/Process(2160): Sending signal. PID: 2160 SIG: 9

3 个答案:

答案 0 :(得分:0)

尝试替换所有日志,例如

 Log.i("Stringssss", tmp.toString());

 Log.i("Stringssss","" +tmp.toString());

答案 1 :(得分:0)

您正在将一个空对象发送到某个地方的Log.i中。 您可以通过调用String.valueOf保护您的对象日志,该值为null安全并返回"null" 使用

Log.i(TAG, String.valueOf(myObject));

Log.i(TAG, "MyObject="+myObject);

而不是

Log.i(TAG, myObject);

如果您不想打扰测试对象的无效性。

答案 2 :(得分:0)

原因

此异常即将发生,因为您将空值传递给Log ,即您正在尝试记录对象或现在指向null的实例的未初始化引用,如以下行所示logcat

  

07-16 02:08:18.095:E / AndroidRuntime(2160):java.lang.NullPointerException:println需要一条消息

解决方案

  • 在记录之前始终使用空检查

      

      if(insipid!= null)
          Log.i(&#34; id&#34;,inspid);

  • 追加&#34;&#34;对于记录的值,这将确保日志记录的最终值永远不为null。 然而,这是不好的方法,理想情况下应该检查并记录空对象,如上所示。

      

          Log.i(&#34; id&#34;,&#34;&#34; + inspid);

代码

中的可能位置

以下两个代码块看起来是异常的理想候选者。

位置1:

Log.i("id", inspid); <<--inspid may be null

位置2:

ar.get(k).setInsp_id(jsonstr);
StringBuilder tmp1 = new StringBuilder();
tmp1.append(gson.toJson(ar.get(k)));
Log.i("Stringssss", ar.get(k).getD_code()); <<--ar.get(k).getD_code() may be null
Log.i("Stringssss", tmp1.toString()); <<--tmp1 may be null