2014-12-29 72 views
0

數據顯示在此logcat的,但沒有顯示在文本視圖..我能做些什麼數據顯示logcat的,但在文本視圖中不顯示

這是我的日誌

 
12-29 11:05:13.908: D/gralloc_goldfish(772): Emulator without GPU emulation detected. 
12-29 11:05:24.649: D/dalvikvm(772): GC_FOR_ALLOC freed 195K, 9% free 7439K/8135K, paused 35ms, total 38ms 
12-29 11:05:26.458: D/dalvikvm(772): GC_CONCURRENT freed 229K, 10% free 7628K/8455K, paused 26ms+99ms, total 185ms 
12-29 11:05:27.908: D/Create Response(772): {"message":"Login successfully.","success":1} 
12-29 11:05:28.598: I/Choreographer(772): Skipped 52 frames! The application may be doing too much work on its main thread. 
12-29 11:05:29.848: D/Create Response(772): {"3":"[email protected]","2":"myname","1":"msnmsn","0":"8527801400","mobile_number":"8527801400","hint":"myname","email_id":"[email protected]","password":"msnmsn"} 
12-29 11:05:29.848: I/JSON parse(772): myname 
12-29 11:05:29.878: W/System.err(772): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
12-29 11:05:29.878: W/System.err(772): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609) 
12-29 11:05:29.878: W/System.err(772): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835) 
12-29 11:05:29.878: W/System.err(772): at android.view.View.requestLayout(View.java:15129) 
12-29 11:05:29.878: W/System.err(772): at android.view.View.requestLayout(View.java:15129) 
12-29 11:05:29.878: W/System.err(772): at android.view.View.requestLayout(View.java:15129) 
12-29 11:05:29.888: W/System.err(772): at android.view.View.requestLayout(View.java:15129) 
12-29 11:05:29.888: W/System.err(772): at android.view.View.requestLayout(View.java:15129) 
12-29 11:05:29.888: W/System.err(772): at android.widget.TextView.checkForRelayout(TextView.java:6303) 
12-29 11:05:29.888: W/System.err(772): at android.widget.TextView.setText(TextView.java:3547) 
12-29 11:05:29.888: W/System.err(772): at android.widget.TextView.setText(TextView.java:3405) 
12-29 11:05:29.888: W/System.err(772): at android.widget.TextView.setText(TextView.java:3380) 
12-29 11:05:29.898: W/System.err(772): at com.example.phonebook.Welcome$loginAccess.doInBackground(Welcome.java:93) 
12-29 11:05:29.898: W/System.err(772): at com.example.phonebook.Welcome$loginAccess.doInBackground(Welcome.java:1) 
12-29 11:05:29.898: W/System.err(772): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
12-29 11:05:29.898: W/System.err(772): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
12-29 11:05:29.898: W/System.err(772): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
12-29 11:05:29.898: W/System.err(772): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
12-29 11:05:29.908: W/System.err(772): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
12-29 11:05:29.908: W/System.err(772): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
12-29 11:05:29.908: W/System.err(772): at java.lang.Thread.run(Thread.java:856) 

這是我的歡迎活動

 

    package com.example.phonebook; 

    import java.util.ArrayList; 
    import java.util.List; 

    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONObject; 



    import android.app.Activity; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.os.StrictMode; 
    import android.util.Log; 
    import android.widget.TextView; 

    public class Welcome extends Activity { 
    // private ProgressDialog pDialog; 

     TextView jsonParsed1,DataShow,hint_data; 

     JSONParser jsonParser = new JSONParser(); 
     JSONParser jobj = new JSONParser(); 
     private static String url = "http://192.168.1.10/android_connect/login_setup/show_data.php"; 

     //private static final String about = "about"; 
     // private static final String TAG_SUCCESS="success"; 
     // private static final String hint="hint"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectDiskReads().detectDiskWrites().detectNetwork() 
      .penaltyLog().build()); 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.welcome); 

      jsonParsed1=(TextView)findViewById(R.id.jsonParsed); 
      DataShow=(TextView)findViewById(R.id.show_all_data); 
      hint_data=(TextView)findViewById(R.id.hint_datafor_value); 

      String abt = getIntent().getStringExtra("mobile_number"); 
      //Intent i=getIntent(); 
      //String abt=i.getStringExtra("mobile_number"); 
      //System.out.println(abt); 
      //jsonParsed1.setText(abt); 

      new loginAccess().execute(); 
     } 

    class loginAccess extends AsyncTask { 

     TextView jsonParsed = (TextView) findViewById(R.id.jsonParsed); 

     private String Content; 
     String OutputData = ""; 
     JSONObject jsonResponse; 



     @Override 
     protected String doInBackground(String... arg0) { 

      String abt = getIntent().getStringExtra("mobile_number"); 

      List params = new ArrayList(); 
      params.add(new BasicNameValuePair("mobile_number", abt)); 
      JSONObject json = jsonParser.makeHttpRequest(url,"POST", params); 

       Content = json.toString(); 

       Log.d("Create Response", Content); 
    /****************** Start Parse Response JSON Data *************/ 




       try { 

        /****** Creates a new JSONObject with name/value mappings from the JSON string. ********/ 
        jsonResponse = new JSONObject(Content); 

        /******* Fetch node values **********/ 
        String hint = jsonResponse.optString("hint").toString(); 

        OutputData += "Node : \n\n  "+ hint +" "; 

        Log.i("JSON parse", hint); 

       /****************** End Parse Response JSON Data *************/  

        // jsonParsed.setText(OutputData); 

       } catch (Exception e) { 

        e.printStackTrace(); 
       } 


       return null; 
     } 

     protected void onPostExecute(Void unused) { 
      //  //JSONObject jsonResponse = null; 
      //  
      //  /******* Fetch node values **********/ 
      //  String hint = jsonResponse.optString("hint").toString(); 
      //   
      //  OutputData += "Node : \n\n  "+ hint +" "; 
      //   
      //  Log.i("JSON parse", hint); 
      //  
      //  /****************** End Parse Response JSON Data *************/  
      //   
      //  hint_data.setText(OutputData); 

       jsonParsed.setText(OutputData); 

      } 

     } 
    } 



+0

是否解決了? –

回答

1

修改您的異步任務是這樣的: 在非UI線程中執行asynctask的背景方法,因此我們不能修改doInbackgroud中的ui元素,所以我們必須返回結果,以便postExecute方法中的結果作爲參數那麼我們可以修改UI

package com.example.phonebook; 

    import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.widget.TextView; 

import com.afbb.animationtest.R; 

    public class Welcome extends Activity { 
     TextView jsonParsed1,DataShow,hint_data; 
     TextView jsonParsed ; 
     JSONParser jsonParser = new JSONParser(); 
     JSONParser jobj = new JSONParser(); 
     private static String url = "http://192.168.1.10/android_connect/login_setup/show_data.php"; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectDiskReads().detectDiskWrites().detectNetwork() 
      .penaltyLog().build()); 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.welcome); 
      jsonParsed1=(TextView)findViewById(R.id.jsonParsed); 
      DataShow=(TextView)findViewById(R.id.show_all_data); 
      hint_data=(TextView)findViewById(R.id.hint_datafor_value); 
      jsonParsed = (TextView) findViewById(R.id.jsonParsed); 
      String abt = getIntent().getStringExtra("mobile_number"); 
      new loginAccess().execute(); 
     } 

    class loginAccess extends AsyncTask<String,Void,JSONObject> { 
     private String Content; 
     @Override 
     protected JSONObject doInBackground(String... arg0) { 
      String abt = getIntent().getStringExtra("mobile_number"); 
      List params = new ArrayList(); 
      params.add(new BasicNameValuePair("mobile_number", abt)); 
      JSONObject json = jsonParser.makeHttpRequest(url,"POST", params); 
       Content = json.toString(); 
       Log.d("Create Response", Content); 
    /****************** Start Parse Response JSON Data *************/ 
       try { 
        JSONObject jsonResponse = new JSONObject(Content); 
        String hint = jsonResponse.optString("hint").toString(); 
        return jsonResponse; 
       } catch (Exception e) { 

        e.printStackTrace(); 
       } 
       return null; 
     } 
     protected void onPostExecute(JSONObject result) { 
      //Parse and update the UI 
       jsonParsed.setText(OutputData); 
      } 
     } 
    } 



i 
+0

問我清楚我沒有得到你。 –

+0

顯示您的修改代碼。 –

+0

我如何在歡迎活動中展示這些數據..... ?? ..不只顯示一個數據{「3」:「[email protected]」,「2」:「myname」,「1」 : 「msnmsn」, 「0」: 「8527801400」, 「MOBILE_NUMBER」: 「8527801400」, 「提示」: 「MYNAME」, 「EMAIL_ID」: 「[email protected]」, 「密碼」: 「msnmsn」 } –

1

jsonParsed.setText(OutputData);
你只要把上面一行在onPostExecute方法0123後線,你會得到完美的輸出。
並且還從doInBackground刪除jsonParsed.setText(OutputData);行的方法。

+0

我改變你說什麼,但數據不是完美的...你可以設置活動...深梅赫爵士 –

+0

編輯該問題就像你說的 –

+0

Log.i(「JSON解析」,提示);在jsonParsed.setText(OutputData)之前,將此行放在onPostExecute方法中;行,你會得到我認爲的解決方案。只需檢查將此日誌放在onPostExecute方法後,您將獲得logcat中的數據或不! –

0

低於線從doInBackground方法

jsonParsed.setText(OutputData); 

你不能碰從外部主線程視圖刪除。