Parse JSON to TextViews

So im parsing JSON data from a API Enabled website, It requires authentiucation by a APIKEY, My issue is that im trying to parse the data then insert it into TextViews on the UI. My current code previously used to use a HashMap and a ListView However now im trying to change it to just populate the data into the TextViews. Currently, I've started changing the code over and now im getting RunTimeExceptions with my current code. Any Help would be greatly appreciated.

Issue Resolved
public class ParseMoreDetails extends Activity {
private Context context;
private static String url = "https://api.company.com/api/systems?";

private static final String TAG_SYSTEM = "systems";
private static final String TAG_SYSTEM_ID = "system_id";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_SYSTEM_NAME = "system_name";
private static final String TAG_SYSTEM_PUBLIC_NAME = "system_public_name";
private static final String TAG_STATUS = "status";

TextView textView;
TextView textView2;
TextView textView3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView2);
    textView2 = (TextView) findViewById(R.id.TextView01);
    textView3 = (TextView) findViewById(R.id.TextView03);

    new ProgressTask(ParseMoreDetails.this).execute();
}

private class ProgressTask extends AsyncTask<String, Void, ArrayList<String>> {
    private ProgressDialog dialog;
    ArrayList<String> arrfortextviews;
    private ParseMoreDetails activity;

    // private List<Message> messages;
    public ProgressTask(ParseMoreDetails parseMoreDetails) {
        this.activity = parseMoreDetails;
        context = parseMoreDetails;
        dialog = new ProgressDialog(context);
    }

    /** progress dialog to show user that the backup is processing. */

    /** application context. */
    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(ArrayList<String> success) {
        if(arrfortextviews.size() >0){
                textView.setText(success.get(0));
                textView2.setText(success.get(1));
                textView3.setText(success.get(2));
         }
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    protected ArrayList<String> doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();
         arrfortextviews=new ArrayList<String>();
        // Using APIKEY from strings.xml
        String apikey = getString(R.string.apikey);

        // getting JSON string from URL
        JSONArray json = jParser.getJSONFromUrl(url + "&key=" + apikey);

        for (int i = 0; i < json.length(); i++) {

            try {
                JSONObject c = json.getJSONObject(i);

                String vcolor = c.getString(TAG_SYSTEM_ID);
                String vfuel = c.getString(TAG_CITY);
                String vtread = c.getString(TAG_STATE);


                arrfortextviews.add(vcolor);
                arrfortextviews.add(vfuel);
                arrfortextviews.add(vtread);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return arrfortextviews;

    }

 }

Layout

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="System ID: " />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..." />
    </LinearLayout>
</FrameLayout>

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="City:" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..." />
    </LinearLayout>
</FrameLayout>

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/TextView04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="State:" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..." />
    </LinearLayout>
</FrameLayout>

</LinearLayout>
Current Errors
  • RuntimeException - NullPointerException
  • Leaked Window com.android.internal.policy.impl.PhoneWindow$DecorView
LogCat - UPDATED
 05-01 10:27:47.040: E/AndroidRuntime(28236): FATAL EXCEPTION: AsyncTask #1
 05-01 10:27:47.040: E/AndroidRuntime(28236): java.lang.RuntimeException: An error occured while executing doInBackground()
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at android.os.AsyncTask$3.done(AsyncTask.java:299)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.lang.Thread.run(Thread.java:856)
 05-01 10:27:47.040: E/AndroidRuntime(28236): Caused by: java.lang.NullPointerException
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.doInBackground(ParseMoreDetails.java:116)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.doInBackground(ParseMoreDetails.java:1)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 05-01 10:27:47.040: E/AndroidRuntime(28236):   ... 5 more
 05-01 10:27:55.035: I/Choreographer(28236): Skipped 464 frames!  The application may be doing too much work on its main thread.
 05-01 10:27:55.210: E/WindowManager(28236): Activity com.jitesh.androidjsonparser.ParseMoreDetails has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4285c818 that was originally added here
 05-01 10:27:55.210: E/WindowManager(28236): android.view.WindowLeaked: Activity com.jitesh.androidjsonparser.ParseMoreDetails has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4285c818 that was originally added here
 05-01 10:27:55.210: E/WindowManager(28236):    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.view.Window$LocalWindowManager.addView(Window.java:554)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.Dialog.show(Dialog.java:277)
 05-01 10:27:55.210: E/WindowManager(28236):    at com.jitesh.androidjsonparser.ParseMoreDetails$ProgressTask.onPreExecute(ParseMoreDetails.java:86)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.os.AsyncTask.execute(AsyncTask.java:534)
 05-01 10:27:55.210: E/WindowManager(28236):    at com.jitesh.androidjsonparser.ParseMoreDetails.onCreate(ParseMoreDetails.java:63)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.Activity.performCreate(Activity.java:5206)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.os.Handler.dispatchMessage(Handler.java:99)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.os.Looper.loop(Looper.java:137)
 05-01 10:27:55.210: E/WindowManager(28236):    at android.app.ActivityThread.main(ActivityThread.java:4898)
 05-01 10:27:55.210: E/WindowManager(28236):    at java.lang.reflect.Method.invokeNative(Native Method)
 05-01 10:27:55.210: E/WindowManager(28236):    at java.lang.reflect.Method.invoke(Method.java:511)
 05-01 10:27:55.210: E/WindowManager(28236):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
 05-01 10:27:55.210: E/WindowManager(28236):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
 05-01 10:27:55.210: E/WindowManager(28236):    at dalvik.system.NativeStart.main(Native Method)

Answers


In Here :

TextView textView = (TextView) findViewById(R.id.textView2);  //<<< here
TextView textView2 = (TextView) findViewById(R.id.TextView01);//<<< here
TextView textView3 = (TextView) findViewById(R.id.TextView03);//<<< here

@Override
protected void onCreate(Bundle savedInstanceState) {
.....

you are using findViewById before setting layout for current Activity .so you will need to move all Widgets initialize inside onCreate method of Activity after setting layout for it. Change your code as:

TextView textView,textView2,textView3;  //<<<declare here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  //initialize all textViews here
  textView = (TextView) findViewById(R.id.textView2);
  textView2 = (TextView) findViewById(R.id.TextView01);
  textView3 = (TextView) findViewById(R.id.TextView03);
  ......

Need Your Help

Load and Reuse Django Template Filters

python django templatetags

Is it possible to load a django template tag/filter to use as a function in one of my template tags?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.