0

这是应用程序代码,mainactivity() 包含在布局的 xml() 文件中注册的 buttonClick() onClick() 事件!,如果单击按钮并且在 run() 中没有任何内容,则什么也不会发生,但是一旦我放了一些东西是 run() 并单击按钮,应用程序就会死掉?

我正在尝试线程!

public void buttonClick(View view)
    {

            Runnable runnable = new Runnable() {
                public void run() {         
                    //Toast.makeText(getApplicationContext(), "hehe",Toast.LENGTH_LONG).show();
                    chikki();
                }
          };
    Thread mythread = new Thread(runnable);
        mythread.start();
    }

链接到 logcat >> http://winacro.com/AndroidLOGCAT/crasher.txt

这是活动代码:

package com.example.elecimp;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    private static String url = "http://api.androidhive.info/contacts/";
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    private static final String TAG_PHONE_HOME = "home";
    private static final String TAG_PHONE_OFFICE = "office";

    // JSON Node names
    private static EditText jsonView;
    private static Button but1;
    private static Thread th;
    // contacts JSONArray
    JSONArray contacts = null;
    private String strJSONValue = "{\"information\":[ {\"sub1_attr\":\"sub1_attr_value\" },{\"sub1_attr\":\"sub2_attr_value\" }]}}}";
    private JSONObject jsonObject;

    String strParsedValue = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        jsonView = (EditText) findViewById (R.id.editText1);
        }

    public void buttonClick(View view)
    {

            Runnable runnable = new Runnable() {
                public void run() {         
                    //Toast.makeText(getApplicationContext(), "hehe",Toast.LENGTH_LONG).show();
                    chikki();
                }
          };
    Thread mythread = new Thread(runnable);
        mythread.start();
    }

   public void chikki() {
        Toast.makeText(getApplicationContext(), "hehe",Toast.LENGTH_LONG).show();
   }

   public void parseJSON() {
    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        contacts = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for(int i = 0; i < contacts.length(); i++){
            JSONObject c = contacts.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String email = c.getString(TAG_EMAIL);
            String address = c.getString(TAG_ADDRESS);
            String gender = c.getString(TAG_GENDER);

            // Phone number is agin JSON Object
            JSONObject phone = c.getJSONObject(TAG_PHONE);
            String mobile = phone.getString(TAG_PHONE_MOBILE);
            String home = phone.getString(TAG_PHONE_HOME);
            String office = phone.getString(TAG_PHONE_OFFICE);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            map.put(TAG_EMAIL, email);
            map.put(TAG_PHONE_MOBILE, mobile);

            // adding HashList to ArrayList
            //contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }}



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
4

1 回答 1

0

您是否尝试在线程中使用 UI(例如显示 Toast)?如果是,那么您的应用程序将无法运行。如果你想做一些与 UI 相关的事情,你必须使用

runonuithread

在 UI 线程上运行指定的操作。如果当前线程是 UI 线程,则立即执行该操作。如果当前线程不是 UI 线程,则将动作发布到 UI 线程的事件队列中。

或者

异步任务

编辑

你需要改变这个

public void buttonClick(View view)
{

        Runnable runnable = new Runnable() {
            public void run() {         
                //Toast.makeText(getApplicationContext(), "hehe",Toast.LENGTH_LONG).show();
                chikki();
            }
      };
    Thread mythread = new Thread(runnable);
    mythread.start();
}

对此

public void buttonClick(View view)
{
  Thread mythread  = new Thread("Thread1") {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    chikki();
                }
            });

        }
    };
    mythread .start();
}
于 2013-05-07T16:51:32.420 回答