Android 应用程序在按钮上失败单击()

Android 应用程序在按钮上失败单击(),android,multithreading,buttonclick,Android,Multithreading,Buttonclick,下面是应用程序代码,mainactivity()由一个buttonClick()onClick()事件组成,该事件在布局的xml()文件中注册,如果单击按钮并且run()中没有任何内容,则不会发生任何事情,但只要我将某个内容放入run()并单击按钮,应用程序就会死机 我在试着穿线 public void buttonClick(View view) { Runnable runnable = new Runnable() { pu

下面是应用程序代码,mainactivity()由一个buttonClick()onClick()事件组成,该事件在布局的xml()文件中注册,如果单击按钮并且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>>

以下是活动代码:

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

}
package com.example.elecimp;
导入java.util.HashMap;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
专用静态字符串url=”http://api.androidhive.info/contacts/";
专用静态最终字符串标记_CONTACTS=“CONTACTS”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u EMAIL=“EMAIL”;
私有静态最终字符串标记_ADDRESS=“ADDRESS”;
私有静态最终字符串标记_GENDER=“GENDER”;
专用静态最终字符串标记_PHONE=“PHONE”;
专用静态最终字符串标记\u PHONE\u MOBILE=“MOBILE”;
专用静态最终字符串标记\u PHONE\u HOME=“HOME”;
专用静态最终字符串标记\u PHONE\u OFFICE=“OFFICE”;
//JSON节点名称
私有静态EditText jsonView;
专用静态按钮but1;
私有静态线程;
//联系JSONArray
JSONArray联系人=null;
私有字符串strJSONValue=“{\'information\”:[{\'sub1\u attr\':\'sub1\u attr\u value\',{\'sub1\u attr\':\'sub2\u attr\u value\'}}}”;
私有JSONObject JSONObject;
字符串strParsedValue=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
jsonView=(EditText)findViewById(R.id.editText1);
}
公共作废按钮单击(查看)
{
Runnable Runnable=新的Runnable(){
public void run(){
//Toast.makeText(getApplicationContext(),“hehe”,Toast.LENGTH_LONG.show();
chikki();
}
};
线程mythread=新线程(可运行);
mythread.start();
}
公共图书馆{
Toast.makeText(getApplicationContext(),“hehe”,Toast.LENGTH_LONG.show();
}
公共void parseJSON(){
//创建JSON解析器实例
JSONParser jParser=新的JSONParser();
//从URL获取JSON字符串
JSONObject json=jParser.getJSONFromUrl(url);
试一试{
//获取联系人数组
contacts=json.getJSONArray(TAG_contacts);
//通过所有触点循环
对于(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签名称、名称);
地图放置(标签、电子邮件、电子邮件);
地图放置(标签、手机、手机);
//将哈希列表添加到ArrayList
//联系人列表。添加(地图);
}
}捕获(JSONException e){
e、 printStackTrace();
}}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}

您是否正在尝试使用线程中的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();
}

发布您的logcat输出。您可以发布logcat输出吗?布局中应该有一个
onClick
事件,而不是清单文件。这可能就是按钮没有响应的原因。抱歉,我写错了,实际上它只在布局文件中。xml我创建了一个新线程。你的意思是,即使新线程在同一个UI线程上,线程对线程也会运行?根据定义,新线程不能“在同一个UI线程上”。无论您在何处实例化新线程,它都是一个新对象,因此与UI线程(操作系统为您创建的)不同。除了UI线程,你不能从任何地方接触UI内容。我已经添加了MyActivity代码:请在上面!请看一下,告诉我问题所在