简体   繁体   中英

unfortunately has stopped when I want to update ListView

My logic is as follows. First, I get some data from the server. Then, I manipulate the data. Next, I put the data into a ListView . If the user scrolls to the bottom of this view, I want to refresh the view. I copy the properties from my Async object to a new object. At this point I see "unfortunately has stopped".

Here my code of activity class

    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {


        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH,monthOfYear);

            this.Syear  = calendar.get(Calendar.YEAR);
            this.Smonth = calendar.get(Calendar.MONTH);
            Smonth = Smonth + 1;
            InboxActivity i = new InboxActivity();

            String user_id = getIntent().getExtras().getString(LoginActivity.SESSION_ID);           

            final FetchTask fetch = new FetchTask();
            fetch.Selectedmonth = this.Smonth;
            fetch.Selectedyear = this.Syear;
            fetch.page = 0;
            fetch.sess_id = user_id;
            ListView ll = (ListView)findViewById(R.id.mailList);
            fetch.ll = ll;
            fetch.execute();

            ll.setOnScrollListener(new OnScrollListener(){



                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) 
                {
                    if(firstVisibleItem+visibleItemCount==totalItemCount)
                    {

                        //System.out.println("HERE WILL BE MY SESSION ID!!!!w");
                        //System.out.println(fetch.sess_id);

                        FetchTask RefreshFetch = new FetchTask();

                        fetch.page++;
                        RefreshFetch.page           =  fetch.page++;
                        RefreshFetch.Selectedmonth  =  fetch.Selectedmonth;
                        RefreshFetch.Selectedyear   =  fetch.Selectedyear;
                        RefreshFetch.sess_id        =  fetch.sess_id;
                        RefreshFetch.ll             =  fetch.ll;

                        RefreshFetch.execute();

                    }                       

                }


            });

        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login, menu);
        return true;    
    }



public class FetchTask extends AsyncTask<Void, Void, JSONArray> {

        public JSONArray result_arr;
        public String result_str,email,password,test;
        public int Selectedyear;
        public int Selectedmonth;
        public int page;
        public String sess_id;
        public ListView ll;
        public ProgressDialog pd;
        public ArrayAdapter<String> adapter;


        @Override

        protected JSONArray doInBackground(Void... params) {
            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("MY SITE URL ....");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);



                nameValuePairs.add(new BasicNameValuePair("qw", "das"));
                nameValuePairs.add(new BasicNameValuePair("debug", "1"));
                nameValuePairs.add(new BasicNameValuePair("t", "0"));
                nameValuePairs.add(new BasicNameValuePair("m", Integer.toString(this.Selectedmonth)));
                nameValuePairs.add(new BasicNameValuePair("y", Integer.toString(this.Selectedyear)));
                nameValuePairs.add(new BasicNameValuePair("st", Integer.toString(this.page)));

                nameValuePairs.add(new BasicNameValuePair("sess_id", this.sess_id));


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                sb.append(reader.readLine());
                String line = "0";
                while ((line = reader.readLine()) != null) 
                {
                    sb.append(line);
                }
                reader.close();
                String result11 = sb.toString();

                this.result_str = result11;
                // parsing data
                JSONArray arr = new JSONArray(result11);





                return new JSONArray(result11);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }



          @Override
          protected void onPreExecute() {

          } 

My logs

03-05 13:24:43.239: D/dalvikvm(2052): GC_FOR_ALLOC freed 49K, 7% free 3326K/3540K, paused 33ms, total 35ms
03-05 13:24:43.629: D/gralloc_goldfish(2052): Emulator without GPU emulation detected.
03-05 13:24:50.349: D/dalvikvm(2052): GC_FOR_ALLOC freed 151K, 8% free 3690K/4004K, paused 28ms, total 36ms
03-05 13:24:50.349: D/InputEventConsistencyVerifier(2052): KeyEvent: ACTION_UP but key was not down.
03-05 13:24:50.349: D/InputEventConsistencyVerifier(2052):   in android.widget.EditText{b1e53a20 VFED..CL .F....I. 179,489-589,548 #7f090004 app:id/password}
03-05 13:24:50.349: D/InputEventConsistencyVerifier(2052):   0: sent at 11916532000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=11916532, downTime=11916440, deviceId=0, source=0x101 }
03-05 13:24:55.809: D/dalvikvm(2052): GC_FOR_ALLOC freed 417K, 14% free 3788K/4368K, paused 31ms, total 31ms
03-05 13:24:56.509: D/dalvikvm(2052): GC_FOR_ALLOC freed 89K, 12% free 3877K/4368K, paused 39ms, total 42ms
03-05 13:24:56.549: D/dalvikvm(2052): GC_FOR_ALLOC freed 28K, 12% free 3947K/4468K, paused 39ms, total 40ms
03-05 13:24:56.599: I/dalvikvm-heap(2052): Grow heap (frag case) to 5.087MB for 1127536-byte allocation
03-05 13:24:56.649: D/dalvikvm(2052): GC_FOR_ALLOC freed <1K, 10% free 5048K/5572K, paused 48ms, total 48ms
03-05 13:25:14.239: D/dalvikvm(2052): GC_FOR_ALLOC freed 504K, 12% free 5273K/5940K, paused 56ms, total 58ms
03-05 13:25:15.319: I/Choreographer(2052): Skipped 125 frames!  The application may be doing too much work on its main thread.
03-05 13:25:15.809: I/Choreographer(2052): Skipped 49 frames!  The application may be doing too much work on its main thread.
03-05 13:25:16.209: I/Choreographer(2052): Skipped 34 frames!  The application may be doing too much work on its main thread.
03-05 13:25:17.249: I/Choreographer(2052): Skipped 87 frames!  The application may be doing too much work on its main thread.
03-05 13:25:17.599: I/Choreographer(2052): Skipped 35 frames!  The application may be doing too much work on its main thread.
03-05 13:25:22.239: I/Choreographer(2052): Skipped 37 frames!  The application may be doing too much work on its main thread.
03-05 13:25:22.659: I/Choreographer(2052): Skipped 42 frames!  The application may be doing too much work on its main thread.
03-05 13:25:23.629: I/Choreographer(2052): Skipped 98 frames!  The application may be doing too much work on its main thread.
03-05 13:25:24.819: D/AndroidRuntime(2052): Shutting down VM
03-05 13:25:24.819: W/dalvikvm(2052): threadid=1: thread exiting with uncaught exception (group=0xb1aefba8)
03-05 13:25:24.919: E/AndroidRuntime(2052): FATAL EXCEPTION: main
03-05 13:25:24.919: E/AndroidRuntime(2052): Process: com.example.earchive, PID: 2052
03-05 13:25:24.919: E/AndroidRuntime(2052): java.lang.NullPointerException
03-05 13:25:24.919: E/AndroidRuntime(2052):     at com.example.earchive.InboxActivity$FetchTask.onPostExecute(InboxActivity.java:291)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at com.example.earchive.InboxActivity$FetchTask.onPostExecute(InboxActivity.java:1)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at android.os.Looper.loop(Looper.java:136)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at java.lang.reflect.Method.invoke(Method.java:515)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-05 13:25:24.919: E/AndroidRuntime(2052):     at dalvik.system.NativeStart.main(Native Method)
03-05 13:25:26.259: D/dalvikvm(2052): GC_FOR_ALLOC freed 385K, 9% free 5692K/6240K, paused 145ms, total 145ms

On post execute method

protected void onPostExecute(JSONArray result)
{
    if (result != null) 
    {
        List<String> subjects = new ArrayList<String>();
        List<String> emails = new ArrayList<String>();

        for(int i = 0; i < result.length(); i++)
        {
            try 
            {
                JSONObject json_data = result.getJSONObject(i);
                emails.add(json_data.getString("mittente"));
                subjects.add(json_data.getString("oggetto"));
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }

        }
        if(this.page == 0)
        {
            this.adapter = new ArrayAdapter<String>(
                    InboxActivity.this,
                    R.layout.da_item,
                    emails
                );
            this.ll.setAdapter(this.adapter);               
        }
        else
        {
            for(int i = 0; i < result.length(); i++)
            { 
                JSONObject json_data;
                try 
                {
                    json_data = result.getJSONObject(i);
                    this.adapter.add(json_data.getString("mittente"));  
                } 
                catch (JSONException e) 
                {
                    e.printStackTrace();
                }

            }
        }
    } 
    else 
    {
        System.out.println("Messages not found");

    }    
    this.pd.dismiss();
}

}

Your this.adapter.add(json_data.getString("mittente")); is throwing a NullPointerException .

If page = 1 , this.adapter is not initialized ( page = 0 ) and is therefore NULL. If page 0 is guaranteed to be called before page 1, this would work, but I'm guessing it is not. Here's a fix if I understand your code correctly.

    if( this.adapter == null ) {
            this.adapter = new ArrayAdapter<String>(
                InboxActivity.this,
                R.layout.da_item,
                emails
            );
        this.ll.setAdapter(this.adapter);
    }
    if(this.page != 0)
    {
        for(int i = 0; i < result.length(); i++)
        { 
            JSONObject json_data;
            try 
            {
                json_data = result.getJSONObject(i);
                this.adapter.add(json_data.getString("mittente"));  
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM