0

I cannot get it to do anything. Once the input is completed it just crashes.

The commented out section is how it previously displayed the information but I want just the UPC and Product to be displayed. Any ideas?

If there is a question that is already out there, please link it.

Crash logs:

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 file:

<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>

New Errors:

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)
Gligor
  • 2,862
  • 2
  • 33
  • 40
rmushero
  • 39
  • 9
  • can you please provide the crash logs? – Gligor Apr 09 '13 at 02:24
  • Is it really a JSON array at the top level, or is it a JSON object with a JSON array inside? The second seems more likely. If the first character of the file is { its the second (arrays start with [ ). – Gabe Sechan Apr 09 '13 at 02:26
  • 1
    @GluteFree Your answer is right in front of you: `android.view.ViewRootImpl$CalledFromWrongThreadException`. StackOverflow has a lot of threads on this - here is [the first hit on Google](http://stackoverflow.com/a/11204107/1270789). – Ken Y-N Apr 09 '13 at 03:37
  • I'm sorry. I am really new to Stackoverflow. I need to learn to look and be quiet. Thank you for pointing the threads out! – rmushero Apr 09 '13 at 03:51

1 Answers1

2

Ok, so from your logs I can see you have a NullPointerException on line 113:

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

This means that your upca TextView is not initialised in line 112. Can you check if you have a textView1 in the layout you have set above?

Update: The second error you are getting ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

This happens when you are trying to update the UI Thread from another Thread. What you are doing in the code is calling getServerData() from a new Thread, while within the method you are trying to change the text of the TextViews which are in the UI Thread.

Hope the above makes sense?

Gligor
  • 2,862
  • 2
  • 33
  • 40
  • I am Editing the main question to contain the xml as well – rmushero Apr 09 '13 at 03:05
  • is the xml you posted above your `activity_networking` layout.xml? – Gligor Apr 09 '13 at 03:08
  • Oh yes sorry i should have clarified that. Als thanks for all the help you have supplied so far – rmushero Apr 09 '13 at 03:09
  • ok, so did the above fix your issue and you are having a new issue now? can you post what the new issue is? – Gligor Apr 09 '13 at 03:25
  • Okay, what you told me prevented the initial crash. Now what is happening is that the window pops up and says "text view" "text view" one directly above the other and then after a few seconds it crashes. Adding log files to initial issue and editing code – rmushero Apr 09 '13 at 03:28
  • Ok, the new error I will explain as an update in the above answer. – Gligor Apr 09 '13 at 03:34
  • I'm not entirely sure what that means. Does that mean I need to put the : TextView upca = (TextView)findViewById(R.id.textView1); upca.setText("UPCA: " + UPCA); TextView product = (TextView)findViewById(R.id.textView2); product.setText("Product: " + Product); Elements elsewhere? – rmushero Apr 09 '13 at 03:47
  • 1
    Yes. Basically you cannot access the UI from a background thread. Everything on the UI thread happens in the main thread so what you can do is have the `getServerData()` method return some sort of `Product` object with `UPCA and Product` properties and then once returned you can update the `TextView` texts in the main/UI thread. – Gligor Apr 09 '13 at 04:08