0

我不能让它做任何事情。一旦输入完成,它就会崩溃。

注释掉的部分是它以前显示信息的方式,但我只想显示 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)
4

1 回答 1

2

好的,所以从您的日志中我可以看到您在第 113 行有一个 NullPointerException:

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

这意味着你upca TextView没有在第 112 行初始化。你能检查一下你textView1在上面设置的布局中是否有 a 吗?

更新:你得到的第二个错误ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

当您尝试UI Thread从另一个Thread. 您在代码中所做的是getServerData()从新调用,而在方法中Thread您试图更改.TextViewsUI Thread

希望以上内容有意义吗?

于 2013-04-09T02:33:15.703 回答