這裏是應用程序代碼,mainactivity()包含在layout的xml()文件中註冊的buttonClick()onClick()事件! ,如果單擊按鈕並且run()中沒有任何內容會發生任何事情,但只要我放了一些東西是run()並單擊該應用程序死亡的按鈕?app失敗buttonClick()
我想要線程!
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;
}
}
發佈您的logcat輸出。 – Cornholio 2013-05-07 16:45:21
你可以發佈logcat輸出嗎? – 2013-05-07 16:45:44
'onClick'事件應該在您的佈局中,而不是清單文件中。這可能就是爲什麼這個按鈕沒有反應。 – 2013-05-07 16:47:27