2

http サーバーからフィードProgressDialogを読み込んで表示しようとしています。とjsonを取得しNullPointerExceptionます。誤解していなければ、最終的にコードが実行されます! 私のせいはどこですか?これが私のアクティビティコードです:doInBackgroundonPostExecuteAsyncTaskonPostExecute

public class ResultInput extends Activity {

    int count = 0;
    String mainUrl;
    String[] product_pict_part;
    InputStream is = null;
    private ProgressDialog pd;
    String TAG = "meinDebug";
    ParseEan parse_code;
    String readProductNameEan;
    ImageView productPic;
    String productName;
    Drawable drawable;
    TextView product_name;
    TextView text_level;
    int productLevel;

    private Context context;

    // private MyProgressDialog dialog;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        new ProgressTask(ResultInput.this).execute();
    }

    private InputStream fetch(String urlString) throws MalformedURLException,
            IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        private Activity activity;

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

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

        /** application context. */

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

        @Override
        protected void onPostExecute(final Boolean success) {
            pd.dismiss();
            productPic.setImageDrawable(drawable);

            Log.i(TAG, "Picture URL " + mainUrl);
            product_name.setText(productName);

            if (productLevel == 1) {
                text_level.setText("helal");
            } else if (productLevel == 2) {
                text_level.setText("mittel");
            } else if (productLevel == 3) {
                text_level.setText("haram");
            } else if (productLevel == 4) {
                text_level.setText("in analyse");
            }

            if (dialog.isShowing()) {
                dialog.dismiss();
            }

            if (success) {
                Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
            }
        }

        protected Boolean doInBackground(final String... args) {
            try {
                mainUrl = "http://nur-efsan.de/websHalalCheck/public/image/index/dir/";
                product_name = (TextView) findViewById(R.id.produkt_name);
                text_level = (TextView) findViewById(R.id.text_level);
                productPic = (ImageView) findViewById(R.id.productpic);
                productName = parse_code.parseJSONPost("data", "name",
                        readProductNameEan);
                String productPicture = parse_code.parseJSONPost("data",
                        "image", readProductNameEan);
                productLevel = Integer.parseInt(parse_code.parseJSONPost(
                        "data", "levelId", readProductNameEan));

                product_pict_part = productPicture.split("/");
                mainUrl = mainUrl + product_pict_part[0] + "/name/"
                        + product_pict_part[1];

                try {
                    is = fetch(mainUrl);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                drawable = Drawable.createFromStream(is, "src");

                return true;
            } catch (Exception e) {
                Log.e("tag", "error", e);
                return false;
            }
        }

    }

}
4

2 に答える 2

1

OnPostExecuteメソッドで

 dialog.dismiss();

それ以外の

 pd.dismiss();

2番

doinbackgroundメソッドは非UIスレッドであるため、doInBackgroud(...)メソッドでTextViewまたはimageview(productname、testview ...)を参照しないでください。

So give refrence in onCreate method and then call asyncTaskおよびsetValueinonPostExecute method...

于 2012-05-26T11:06:45.697 に答える
0

返信ありがとうございます!あなたが私に説明したように、私は自分のコードを変更します! そして、コンテンツビューを設定するのを忘れました:)私の最終的なコード:

package com.halal.helalcheck;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultInput extends Activity {

    int count = 0;
    String mainUrl;
    String[] product_pict_part;
    InputStream is = null;
    String TAG = "meinDebug";
    ParseEan parse_code = new com.halal.helalcheck.ParseEan();
    String readProductNameEan;
    ImageView productPic;
    String productName;
    Drawable drawable;
    TextView product_name;
    TextView text_level;
    int productLevel;


    private Context context;

    // private MyProgressDialog dialog;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.resultwindow);

        mainUrl = "http://nur-efsan.de/websHalalCheck/public/image/index/dir/";
        product_name = (TextView) findViewById(R.id.produkt_name);
        text_level = (TextView) findViewById(R.id.text_level);
        productPic = (ImageView) findViewById(R.id.productpic);


        new ProgressTask(ResultInput.this).execute();


    }

    private InputStream fetch(String urlString) throws MalformedURLException,
            IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        private Activity activity;

        public ProgressTask(Activity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

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

        /** application context. */

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Checking Product");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            drawable = Drawable.createFromStream(is, "src");
            productPic.setImageDrawable(drawable);
            Log.i(TAG, "Picture URL " + mainUrl);
            product_name.setText(productName);

            if (productLevel == 1) {
                text_level.setText("helal");
            } else if (productLevel == 2) {
                text_level.setText("mittel");
            } else if (productLevel == 3) {
                text_level.setText("haram");
            } else if (productLevel == 4) {
                text_level.setText("in analyse");
            }

            if (dialog.isShowing()) {
                dialog.dismiss();
            }

        }

        @Override
        protected Boolean doInBackground(final String... args) {
            try {
                Bundle b = getIntent().getExtras();
                String ean_code = b.getString("barcodestring");


                String readProductNameEan = parse_code.readProductEan(ean_code);
                Log.i(TAG, readProductNameEan);
                productName = parse_code.parseJSONPost("data", "name", readProductNameEan);
                Log.i(TAG, productName);

                productLevel = Integer.parseInt(parse_code.parseJSONPost( "data", "levelId", readProductNameEan));

                String productPicture = parse_code.parseJSONPost("data", "image", readProductNameEan);
                product_pict_part = productPicture.split("/");
                mainUrl = mainUrl + product_pict_part[0] + "/name/" + product_pict_part[1];

                try {
                    is = fetch(mainUrl);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                return true;
            } catch (Exception e) {
                Log.e("tag", "error", e);
                return false;
            }
        }

    }

}
于 2012-05-31T07:25:33.807 に答える