无法在AsyncTask中的onProgressUpdate中的Arraylist中添加String

时间:2015-07-03 13:09:11

标签: android-asynctask

尽管在onProgressupdate中添加了项目,我仍然获得null arraylist " rev_arr" 。 我在publishprogress方法中传递了String。 但它没有用。 为了方便起见,我没有在这里写入import语句。

这是我的代码 -

public class det_movFragment extends Fragment {
    public String revmovieJsonStr;
    public ArrayList<String > rev_arr = new ArrayList<String>();

    public det_movFragment() {
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootview = inflater.inflate(R.layout.fragment_det_mov, container, false);
        Intent intent = getActivity().getIntent();
        final Bundle extras = intent.getExtras();
        if (intent != null ) {
            String put_ext_tit = extras.getString("title");
            String put_ext_rel_date = extras.getString("rel_date");
            String url_pos = extras.getString("url_pos");
            int posit = extras.getInt("posit");
            String over = extras.getString("over_ex");
           final String id = extras.getString("id_ex");

           if(put_ext_tit!=null && id!=null) {


                       ((TextView) rootview.findViewById(R.id.title_mov))
                        .setText(put_ext_tit);
               ((TextView) rootview.findViewById(R.id.rel_date_mov))
                       .setText(put_ext_rel_date);
             ImageView im2= ((ImageView) rootview.findViewById(R.id.pos_im));

         Picasso.with(getActivity()).load(url_pos)

                       .centerCrop()
                       .resize(400, 400)
                       .into(im2);
               ((TextView) rootview.findViewById(R.id.over))
                       .setText(over);

               Button b = (Button) rootview.findViewById(R.id.rev);
               b.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       new getrev().execute(id);
                       String[]  revi_arr = rev_arr.toArray(new String[rev_arr.size()]);
                       Log.e("mayank1",revi_arr[1]);
                       Intent intent_rev = new Intent(getActivity(),det_mov_rev.class);
                       Bundle ex_rev = new Bundle();
                       ex_rev.putStringArrayList("rev_list",rev_arr);
                       intent_rev.putExtras(ex_rev);
                       startActivity(intent_rev);

                   }
               });

            }
            else {((TextView) rootview.findViewById(R.id.title_mov)).setText("mayank");}
        }


        return rootview;
    }

    public class getrev extends AsyncTask<String, String, Void> {
        private final String LOG_TAG = getrev.class.getSimpleName();
        public int count;
        String[] sin_rev;
        String[] aut_rev;
        @Override
        protected Void doInBackground(String... params) {
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;
            try {
                final String movie_rev_url_str = "http://api.themoviedb.org/3/movie/"+ params[0] +"/reviews?api_key=8d7a48043ba1d3348181e2b6615cedc7";
                Log.v("url",movie_rev_url_str);
                URL movie_rev_url = new URL(movie_rev_url_str);
                urlConnection = (HttpURLConnection) movie_rev_url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }
                if (buffer.length() == 0) {
                    return null;
                }
                revmovieJsonStr = buffer.toString();

                JSONObject revmovieJson = new JSONObject(revmovieJsonStr);
                JSONArray revmovieJsonarray = revmovieJson.getJSONArray("results");
                count = revmovieJsonarray.length();
                aut_rev = new String[count];
                sin_rev = new String[count];
                for(int i=0;i<count;i++){
                    JSONObject sin_rev_json = revmovieJsonarray.getJSONObject(i);
                    sin_rev[i] = sin_rev_json.getString("content");
                    Log.e("mayank",sin_rev[i]);

                    publishProgress((String) sin_rev[i]);

                }
            } catch (IOException | JSONException e) {
                Log.e(LOG_TAG, "Error ", e);
                return null;
            }
            finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "Error closing stream", e);
                    }
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {

            rev_arr.add(values[0]);

        }

    }
}

这是我的logcat -

07-03 22:34:54.370  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc V/Zygote﹕ Switching descriptor 34 to /dev/null
07-03 22:34:54.370  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc V/Zygote﹕ Switching descriptor 9 to /dev/null
07-03 22:34:54.370  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ Late-enabling CheckJNI
07-03 22:34:54.450  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/ActivityThread﹕ handleBindApplication:jindal5.mayank.popular_movies_14ce10032_gsc
07-03 22:34:54.450  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/ActivityThread﹕ setTargetHeapUtilization:0.75
07-03 22:34:54.450  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/ActivityThread﹕ setTargetHeapMinFree:4194304
07-03 22:34:54.480  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/os/PersistableBundle;)
07-03 22:34:54.480  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.support.v7.app.ActionBarActivity.onSaveInstanceState, referenced from method jindal5.mayank.popular_movies_14ce10032_gsc.MainActivity.onSaveInstanceState
07-03 22:34:54.480  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 8240: Landroid/support/v7/app/ActionBarActivity;.onSaveInstanceState (Landroid/os/Bundle;Landroid/os/PersistableBundle;)V
07-03 22:34:54.480  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 12275: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 12281: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 9846: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
07-03 22:34:54.510  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 407: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 429: Landroid/content/res/TypedArray;.getType (I)I
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 370: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ VFY: unable to resolve virtual method 372: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-03 22:34:54.520  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-03 22:34:54.590  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/Settings﹕ Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
07-03 22:34:54.610  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
    OpenGL ES Shader Compiler Version: E031.24.02.07
    Build Date: 11/03/14 Mon
    Local Branch:
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
07-03 22:34:54.630  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/qdutils﹕ PartialUpdate status: Disabled
07-03 22:34:54.630  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/qdutils﹕ Width Align: 0
07-03 22:34:54.630  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/qdutils﹕ Top Align: 0
07-03 22:34:54.630  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/qdutils﹕ Height Align: 0
07-03 22:34:54.630  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/qdutils﹕ Min ROI Width: 0
07-03 22:34:54.630  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/qdutils﹕ Min ROI Height: 0
07-03 22:34:54.640  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/OpenGLRenderer﹕ Enabling debug mode 0
07-03 22:34:54.780  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41c1b028 time:41024935
07-03 22:34:55.810  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ method Landroid/support/v7/internal/widget/ListViewCompat;.lookForSelectablePosition incorrectly overrides package-private method with same name in Landroid/widget/ListView;
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 135397
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 87101
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 76341
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 254128
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 214756
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 76757
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 198184
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 157336
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 99861
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 118340
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 177572
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 207703
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 122917
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 205596
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 158852
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 245891
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 150689
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 150540
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 241554
07-03 22:34:58.370  21092-21217/jindal5.mayank.popular_movies_14ce10032_gsc V/getimage﹕ Forecast string: 131631
07-03 22:35:01.230  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc V/mayank﹕ San Andreas
07-03 22:35:01.230  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/Timeline﹕ Timeline: Activity_launch_request id:jindal5.mayank.popular_movies_14ce10032_gsc time:41031383
07-03 22:35:01.500  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41f68a28 time:41031650
07-03 22:35:02.760  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc D/AndroidRuntime﹕ Shutting down VM
07-03 22:35:02.760  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41629db8)
07-03 22:35:02.760  21092-21645/jindal5.mayank.popular_movies_14ce10032_gsc V/url﹕ http://api.themoviedb.org/3/movie/135397/reviews?api_key=8d7a48043ba1d3348181e2b6615cedc7
07-03 22:35:02.770  21092-21092/jindal5.mayank.popular_movies_14ce10032_gsc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: jindal5.mayank.popular_movies_14ce10032_gsc, PID: 21092
    java.lang.ArrayIndexOutOfBoundsException: length=0; index=1
            at jindal5.mayank.popular_movies_14ce10032_gsc.det_movFragment$1.onClick(det_movFragment.java:78)
            at android.view.View.performClick(View.java:4445)
            at android.view.View$PerformClick.run(View.java:18446)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)
07-03 22:35:03.050  21092-21645/jindal5.mayank.popular_movies_14ce10032_gsc E/mayank﹕ I was a huge fan of the original 3 movies, they were out when I was younger, and I grew up loving dinosaurs because of them. This movie was awesome, and I think it can stand as a testimonial piece towards the capabilities that Christopher Pratt has. He nailed it. The graphics were awesome, the supporting cast did great and the t rex saved the child in me. 10\5 stars, four thumbs up, and I hope that star wars episode VII doesn't disappoint,
07-03 22:35:03.050  21092-21645/jindal5.mayank.popular_movies_14ce10032_gsc E/mayank﹕ Overall action packed movie... But there should be more puzzles in the climax... But I really love the movie.... Excellent...

1 个答案:

答案 0 :(得分:1)

试试这个,

 b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        getrev task=new getrev(){
                            @Override
                            protected void onPostExecute(Void aVoid) {
                                super.onPostExecute(aVoid);
                                String[] revi_arr = rev_arr.toArray(new String[rev_arr.size()]);
                                Log.e("mayank1", revi_arr[1]);
                                Intent intent_rev = new Intent(getActivity(), det_mov_rev.class);
                                Bundle ex_rev = new Bundle();
                                ex_rev.putStringArrayList("rev_list", rev_arr);
                                intent_rev.putExtras(ex_rev);
                                startActivity(intent_rev);

                            }
                        };
                        task.execute(id);

                    }
                });