app失败按钮Click()

时间:2013-05-07 16:42:00

标签: android multithreading buttonclick

这是应用程序代码,mainactivity()包含一个在布局的xml()文件中注册的buttonClick()onClick()事件!如果单击按钮并且run()中没有任何内容,则没有任何反应,但是只要我放了一些东西就运行()并单击应用程序死的按钮?

我正在尝试使用Thread!

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;
    }

}

1 个答案:

答案 0 :(得分:0)

您是否尝试在线程中使用UI(例如,显示Toast)?如果是,那么您的应用程序将无效。如果你想做与UI相关的事情,你将不得不使用

runonuithread

  

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

OR

AsyncTask

修改

您需要更改此

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();
}