2013-05-07 38 views
0

這裏是應用程序代碼,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; 
    } 

} 
+0

發佈您的logcat輸出。 – Cornholio 2013-05-07 16:45:21

+0

你可以發佈logcat輸出嗎? – 2013-05-07 16:45:44

+0

'onClick'事件應該在您的佈局中,而不是清單文件中。這可能就是爲什麼這個按鈕沒有反應。 – 2013-05-07 16:47:27

回答

0

你們是不是做UI的東西(EG-顯示敬酒)在線程?如果是,那麼你的應用程序將無法工作。如果你想要做一些相關的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(); 
} 
+0

我做了一個新的線程,你的意思是即使新的線程是在同一個UI線程,所以在線程上的線程是碰巧運行? – Naaz 2013-05-07 16:55:10

+0

沒有得到你的朋友.. – bakriOnFire 2013-05-07 16:57:45

+3

根據定義,一個新的線程不能「在同一個UI線程上」。不管你在哪裏實例化新線程,它都是一個新的對象,因此與UI線程(OS爲你創建的)不同。除了UI線程之外,您無法觸摸UI內容。 – Krylez 2013-05-07 17:08:35