0

嘿,我正在使用通过 php 从 mysql db 获取的朋友列表。有时需要一段时间取决于连接强度。所以我想使用加载器直到它获取数据。但我收到错误。

请帮我。

谢谢你,

代码

public class Activity_YourFriendsWith_Class extends Activity {

public String PREFS_NAME = "MyPrefsFile";
public String PREFS_USERID = "prefsUserId";

private static final String TAG_Name = "fname";
private static final String TAG_LName = "lname";

String result = "";
public String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_yourfriendswith);

    SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

    userId = pref.getString(PREFS_USERID, "");

    Button back = (Button) findViewById(R.id.addback);
    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(getApplication(), Activity_MyProfile_Class.class);
            startActivity(intent);
            finish();
        }
    });

    Button addfrnds = (Button) findViewById(R.id.addfrnds);
    addfrnds.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent inti = new Intent();
            inti.setClass(Activity_YourFriendsWith_Class.this,
                    Activity_AddFreinds_Class.class);
            startActivity(inti);
        }
    });

    ProgressTask task = new ProgressTask();
    task.execute();

}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {

           private ProgressDialog pd;
        private Activity activity;



    @Override
    protected Boolean doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        InputStream is = null;

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("user_id", userId));
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://hopscriber.com/friendshopscriber.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        // parse json data
        try {

            ArrayList<HashMap<String, String>> placelist = new ArrayList<HashMap<String, String>>();
            JSONArray jArray = new JSONArray(result);
            if (jArray != null) {
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_Name, json_data.getString("fname"));
                    map.put(TAG_LName, json_data.getString("lname"));

                    placelist.add(map);
                }
            }
            ListView list = (ListView) findViewById(R.id.frndslist);

            ListAdapter adapter = new SimpleAdapter(activity, placelist,
                    R.layout.listrow_yourfriendswith, new String[] { TAG_Name,
                            TAG_LName }, new int[] {
                            R.id.fname, R.id.lname });
            list.setAdapter(adapter);

        } catch (JSONException e1) {
            ListView list = (ListView) findViewById(R.id.frndslist);
            list.setEmptyView(findViewById(R.id.fempty));
        }
        return null;
    }

    }



}

日志猫

08-16 18:38:17.910: E/AndroidRuntime(452): FATAL EXCEPTION: AsyncTask #1
08-16 18:38:17.910: E/AndroidRuntime(452): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 18:38:17.910: E/AndroidRuntime(452):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.lang.Thread.run(Thread.java:1019)
08-16 18:38:17.910: E/AndroidRuntime(452): Caused by: java.lang.NullPointerException
08-16 18:38:17.910: E/AndroidRuntime(452):  at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
08-16 18:38:17.910: E/AndroidRuntime(452):  at hopscriber.com.Activity_YourFriendsWith_Class$ProgressTask.doInBackground(Activity_YourFriendsWith_Class.java:142)
08-16 18:38:17.910: E/AndroidRuntime(452):  at hopscriber.com.Activity_YourFriendsWith_Class$ProgressTask.doInBackground(Activity_YourFriendsWith_Class.java:1)
08-16 18:38:17.910: E/AndroidRuntime(452):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-16 18:38:17.910: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-16 18:38:17.910: E/AndroidRuntime(452):  ... 4 more
4

3 回答 3

1
    private class ProgressTask extends AsyncTask<String, Void, Boolean> {

           private Activity activity;
           ......

           ListAdapter adapter = new SimpleAdapter(activity, ......);

    } 

未定义可变活动- 所以你得到 NPE

于 2012-08-16T13:19:17.817 回答
0

尝试将您的代码分成不同的状态,在 doInbackground() 中进行互联网连接,并在 onPostExecute() 中操作您的列表视图以确保 AsyncTask 已经完成了它的工作

private class ProgressTask extends AsyncTask<String, Void, Boolean> {


    @Override
    protected Boolean doInBackground(String... params) {
        //load your progressBar here
        //for instance
        ProgressBar bar = (ProgressBar) findViewById(R.id.yourProgressBar);
        bar.setVisibility(View.VISIBLE);

        //Load your content here

    }

    protected void onPostExecute() {
        //manipulate your list view here, after fill in some content

        //set your loading bar invisible
        bar.setVisibility(View.INVISIBLE);
    }


}
于 2012-08-16T14:05:04.883 回答
0

而且您也不能在方法 doInBackground 中使用您的 ListView 进行操作。您应该在 onPostExecute 方法中执行此操作

于 2012-08-16T13:22:30.203 回答