0

そのため、API 対応の Web サイトから JSON データを解析しています。APIKEY による認証が必要です。私の問題は、データを解析してから UI の TextViews に挿入しようとしているということです。HashMap私の現在のコードは以前は aと aを使用していましたが、ListView現在はデータを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現在のアクティビティのレイアウトを設定する前に使用しているonCreateため、レイアウトを設定した後、アクティビティのメソッド内ですべてのウィジェットの初期化を移動する必要があります。コードを次のように変更します。

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

null を返します

代わりにこの行を入れてください

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

doInBackground から UI 要素 (TextViews) を処理するべきではありません OnPostExecute はそのために作られています

于 2013-05-01T16:58:36.657 に答える