在togglebutton单击事件中执行asynctask时应用程序崩溃

时间:2015-02-10 13:59:50

标签: android android-asynctask

我在激活和停用我的安全摄像头时正在为个人用途编写应用程序。在我的应用程序启动期间,我将执行一个URL(使用asynctask)来检查激活的状态并更新文本字段,按钮文本和图像。 然后,我点击togglebutton以通过使用asynctask调用执行不同的URL来激活/停用它并且它崩溃了。 我无法弄清楚坠机原因是什么。 你能告诉我吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvKitchenStat= (TextView) findViewById(R.id.alarmstatuskitchen);

    //Set togglebutton click event
    setKitchenButtonClickListener();

    //Execute URL to get status - Kitchen
    mKeyWord="KitchenCheckStat";
    new HttpAsyncTask().execute("http://www.example.com");
}

//For Kitchen
private void setKitchenButtonClickListener(){
    Button toggleButtonKitchen = (Button)findViewById(R.id.toggleButtonKitchen);
    toggleButtonKitchen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                if (mKitchenIsActivated){
                    //Diarm it
                    mKeyWord="KitchenDisarm";
                    new HttpAsyncTask().execute("http://www.example.com");

                }
                else{
                    //Arm it
                    mKeyWord="KitchenArm";
                    new HttpAsyncTask().execute("http://www.example.com");
                }   
            } catch (Exception e) {
                Log.d("ButtonClick", e.getLocalizedMessage());
            }
        }
    });
}
 private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        return GET(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        UpdateKitchenStat();

   }
}

这是日志:

  

02-10 21:33:39.642:I / System.out(7199):AsyncTask#2调用detatch()   02-10 21:34:30.992:W / dalvikvm(7199):threadid = 14:线程退出   未捕获的异常(组= 0x417e7da0)02-10 21:34:31.032:   E / AndroidRuntime(7199):致命异常:AsyncTask#2 02-10   21:34:31.032:E / AndroidRuntime(7199):流程:   com.kcyeoh.android.cctvcontrol,PID:7199 02-10 21:34:31.032:   E / AndroidRuntime(7199):java.lang.RuntimeException:发生错误   执行doInBackground时()02-10 21:34:31.032:   E / AndroidRuntime(7199):at   android.os.AsyncTask $ 3.done(AsyncTask.java:300)02-10 21:34:31.032:   E / AndroidRuntime(7199):at   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   java.util.concurrent.FutureTask.setException(FutureTask.java:222)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   java.util.concurrent.FutureTask.run(FutureTask.java:242)02-10   21:34:31.032:E / AndroidRuntime(7199):at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)02-10   21:34:31.032:E / AndroidRuntime(7199):at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   java.lang.Thread.run(Thread.java:841)02-10 21:34:31.032:   E / AndroidRuntime(7199):引起:java.lang.NullPointerException:   println需要一条消息02-10 21:34:31.032:E / AndroidRuntime(7199):     在android.util.Log.println_native(Native Method)02-10 21:34:31.032:   E / AndroidRuntime(7199):在android.util.Log.d(Log.java:156)02-10   21:34:31.032:E / AndroidRuntime(7199):at   com.kcyeoh.android.cctvcontrol.MainActivity.GET(MainActivity.java:168)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   com.kcyeoh.android.cctvcontrol.MainActivity $ HttpAsyncTask.doInBackground(MainActivity.java:136)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   com.kcyeoh.android.cctvcontrol.MainActivity $ HttpAsyncTask.doInBackground(MainActivity.java:1)   02-10 21:34:31.032:E / AndroidRuntime(7199):at   android.os.AsyncTask $ 2.call(AsyncTask.java:288)02-10 21:34:31.032:   E / AndroidRuntime(7199):at   java.util.concurrent.FutureTask.run(FutureTask.java:237)02-10   21:34:31.032:E / AndroidRuntime(7199):... 4更多02-10 21:36:09.022:   I /处理(7199):发送信号。 PID:7199 SIG:9

这是GET的代码,

 public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

0 个答案:

没有答案