0

ねえ、私はphp経由でmysqlデータベースから取得する友達のリストを使用していました。接続強度によっては時間がかかる場合があります。だから私はそれがデータを取得するまでローダーを使用したかった。しかし、エラーが発生します。

私を助けてください。

ありがとう、

コード

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

    }



}

Logcat

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内のListViewで操作することはできません。onPostExecuteメソッドでこれを行う必要があります

于 2012-08-16T13:22:30.203 に答える
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 に答える