2014-12-29 144 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); 

你不能碰从外部主线程视图删除。