2013-04-09 100 views
0

我無法讓它做任何事情。一旦輸入完成,它就會崩潰。在解析JSON數據到TextView時出現問題應用程序崩潰

註釋掉的部分是它以前如何顯示信息,但我只想顯示UPC和產品。有任何想法嗎?

如果有問題已經存在,請將其鏈接。

崩潰日誌:

04-08 21:12:52.416: W/dalvikvm(26817): threadid=12: thread exiting with uncaught exception (group=0x40ac4228) 
04-08 21:12:52.426: E/AndroidRuntime(26817): FATAL EXCEPTION: Thread-48634 
04-08 21:12:52.426: E/AndroidRuntime(26817): java.lang.NullPointerException 
04-08 21:12:52.426: E/AndroidRuntime(26817): at net.example.glutefree.Networking.getServerData(Networking.java:113) 
04-08 21:12:52.426: E/AndroidRuntime(26817): at net.example.glutefree.Networking.access$0(Networking.java:68) 
04-08 21:12:52.426: E/AndroidRuntime(26817): at net.example.glutefree.Networking$1.run(Networking.java:49) 
04-08 21:12:52.727: E/log_tag(26817): Result    [{"ID":"512320","UPCA":"310742023497","Company":"310742","Product":"OXY MAX DEEP PORE PADS","Gluten Free":null}] 
04-08 21:12:52.727: W/dalvikvm(26817): threadid=13: thread exiting with uncaught exception (group=0x40ac4228) 
04-08 21:12:52.737: E/AndroidRuntime_2_crash(26817): crash in the same process: Thread-48635 
04-08 21:12:52.737: E/AndroidRuntime_2_crash(26817): java.lang.NullPointerException 
04-08 21:12:52.737: E/AndroidRuntime_2_crash(26817): at net.example.glutefree.Networking.getServerData(Networking.java:113) 
04-08 21:12:52.737: E/AndroidRuntime_2_crash(26817): at net.example.glutefree.Networking.access$0(Networking.java:68) 
04-08 21:12:52.737: E/AndroidRuntime_2_crash(26817): at net.example.glutefree.Networking$1.run(Networking.java:49) 

package net.example.glutefree; 


import android.app.Activity; 
import android.os.Bundle; 
import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.util.Log; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 



public class Networking extends Activity{ 
TextView txt; 
int request_Code = 1; 
//called when activity is first created 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_networking); 
    txt = new TextView(getApplicationContext()); 

    // Set the text and call the connect function. 
    txt.setText("Connecting..."); 
    //call the method to run the data retreival 
    new Thread() { 
     public void run() { 
      final String data = getServerData(KEY_121); 
      if (data != null) 
       runOnUiThread(new Runnable() 
       { 
        public void run() 
        { 
         txt.setText(data); 

        } 
       }); 
     } 
    }.start(); 

} 



public static final String KEY_121 = "http://glutefree.com/application_query.php"; 

private String getServerData(String returnString) { 
    String UPC = getIntent().getStringExtra("UPCA"); 
    InputStream is = null; 
    String result = ""; 
    //the upc data to send 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    nameValuePairs.add(new BasicNameValuePair("UPCA",UPC)); 

    //http post 
    try{ 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(KEY_121); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

    }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 

    //convert response to string 
    try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
    }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
    } 
    //parse json data 
    try{ 
      JSONArray jArray = new JSONArray(result); 
      Log.e("log_tag", "Result "+result.toString()); 
      for(int i=0;i<jArray.length();i++){ 
        JSONObject json_data = jArray.getJSONObject(i); 
        String UPCA = json_data.getString("UPCA"); 
        String Product = json_data.getString("Product"); 
        TextView upca = (TextView)findViewById(R.id.textView1); 
        upca.setText("UPCA: " + UPCA); 
        TextView product = (TextView)findViewById(R.id.textView2); 
        product.setText("Product: " + Product); 
        /* Log.i("log_tag","UPCA: "+json_data.getString("UPCA")+ 
          ", Product: "+json_data.getString("Product"));*/ 
        //Get an output to the screen 
        returnString += "\n\t" + jArray.getJSONObject(i); 
      } 
    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 
    return returnString; 
} 

} 

XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Networking" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="37dp" 
    android:layout_marginTop="38dp" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="63dp" 
    android:text="TextView" /> 

</RelativeLayout> 

新的錯誤:

04-08 22:27:38.545: E/log_tag(3312): Result    [{"ID":"512320","UPCA":"310742023497","Company":"310742","Product":"OXY MAX DEEP PORE PADS","Gluten Free":null}] 
04-08 22:27:38.545: W/dalvikvm(3312): threadid=12: thread exiting with uncaught exception (group=0x40aa6228) 
04-08 22:27:38.565: E/AndroidRuntime(3312): FATAL EXCEPTION: Thread-475 
04-08 22:27:38.565: E/AndroidRuntime(3312): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4381) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:805) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.View.requestLayout(View.java:12887) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.View.requestLayout(View.java:12887) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.View.requestLayout(View.java:12887) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.View.requestLayout(View.java:12887) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.view.View.requestLayout(View.java:12887) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.widget.TextView.checkForRelayout(TextView.java:7207) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.widget.TextView.setText(TextView.java:3474) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.widget.TextView.setText(TextView.java:3324) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at android.widget.TextView.setText(TextView.java:3299) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at net.example.glutefree.Networking.getServerData(Networking.java:108) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at net.example.glutefree.Networking.access$0(Networking.java:63) 
04-08 22:27:38.565: E/AndroidRuntime(3312):  at net.example.glutefree.Networking$1.run(Networking.java:44) 
+0

您能否提供崩潰日誌? – Gix 2013-04-09 02:24:00

+0

它真的是頂層的JSON數組,還是JSON數組裏面的JSON對象?第二個似乎更可能。如果文件的第一個字符是{它的第二個字符(數組以[開始])。 – 2013-04-09 02:26:04

+0

崩潰日誌發佈 – rmushero 2013-04-09 02:28:42

回答

2

好了,從你的日誌,我可以看到你在網上一個NullPointerException 113:

TextView upca = (TextView)findViewById(R.id.textView1); //line 112 
upca.setText("UPCA: " + UPCA); //line 113 

這意味着你的upcaTextView不在行初始化112.你能檢查,如果你有一個textView1你上面設置的佈局?

更新:第二個錯誤你得到ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

這種情況發生時,你要更新從另一個ThreadUI Thread。您在代碼中所做的操作是從新的Thread調用getServerData(),而在您嘗試更改UI Thread中的TextViews的文本的方法中。

希望以上內容有意義嗎?

+0

我正在編輯包含xml的主要問題 – rmushero 2013-04-09 03:05:37

+0

是您在'activity_networking' layout.xml之上發佈的xml嗎? – Gix 2013-04-09 03:08:20

+0

哦對了,對不起,我應該澄清一點。 Als感謝您提供的所有幫助至今爲止 – rmushero 2013-04-09 03:09:08

相關問題