0

所以我从启用 API 的网站解析 JSON 数据,它需要通过 APIKEY 进行身份验证,我的问题是我试图解析数据然后将其插入 UI 上的 TextViews。我当前的代码以前使用 aHashMap和 aListView但是现在我试图将其更改为仅将数据填充到TextViews. 目前,我已经开始更改代码,现在我正在RunTimeExceptions使用我当前的代码。任何帮助将不胜感激。

问题解决了

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;

    }

 }

布局

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

当前错误

  • RuntimeException - NullPointerException
  • 泄露的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView

LogCat - 更新

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

2 回答 2

6

在这里 :

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) {
.....

findViewById在为当前 Activity 设置布局之前使用。因此,在为其设置布局之后,您需要在onCreateActivity 的方法内移动所有 Widget 初始化。将您的代码更改为:

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);
  ......
于 2013-05-01T16:35:56.990 回答
0

您在 AsyncTask 的 doInBackround 中遇到错误

我怀疑

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

返回空

把这条线改为可能会起作用

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

你不应该处理来自 doInBackground OnPostExecute 的 UI 元素(TextViews)

于 2013-05-01T16:58:36.657 回答