0

让我首先澄清我的问题。

我正在这样做,解析数据(使用 DOM)并将其绑定到我的列表视图上。一切正常,直到我发现需要很长时间才能做出回应。为了做到这一点,我想出了一个想法,我应该把它放在一个线程中。所以我添加了一个扩展线程的类。

我的解析代码

  public  void SetFriendString()
{
    XMLParser parser2 = new XMLParser(); 
    parser2.getXmlFromUrl(URL_HERE); 

      //HTTP POST
      String url_Getmembermob= URL_FBFRIEND ;
      //String xml_getMembermob=null;
      try {
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost(url_Getmembermob);

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); 
      nameValuePairs.add(new BasicNameValuePair("oAuth", "Test123"));
      nameValuePairs.add(new BasicNameValuePair("""", modGen...));
      nameValuePairs.add(new BasicNameValuePair("", modGen....));
      nameValuePairs.add(new BasicNameValuePair("", "here"));

      //Log.i("nameValuePairs", "nameValuePairs=" + nameValuePairs);

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse httpResponse = httpclient.execute(httppost);

        HttpEntity httpEntity = httpResponse.getEntity();

        FBFRIENDS = EntityUtils.toString(httpEntity);    

        Button GetFriends =(Button) findViewById(R.id.btnAllFriends);
        GetFriends.setBackgroundDrawable(getResources().getDrawable(R.drawable.leftcornerclicked));
        KEY_FRIENDSDATA = FBFRIENDS;

        Log.i("xml-return",""+ FBFRIENDS);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace(); 
    }catch (ClientProtocolException e) { 
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

并添加了这个类

        public class thread extends Thread {
        public void run() {
            SetFriendString();
            //SetMyRequestString();
            //SetMyFriendRequestString();
            pDialog.dismiss();
            }
        }

现在我在我的 OnCreate 上调用这个类

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_friends);
    mContext = this;
    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading All Merchants...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.show();
    //Set All Required data
    thread t = new thread();
    t.start();

现在我的应用程序强制关闭。使用调试器我发现线程跳过了我的方法。这是为什么?

10-29 11:13:29.411: E/AndroidRuntime(2463): FATAL EXCEPTION: main
10-29 11:13:29.411: E/AndroidRuntime(2463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fb.connect/com.fb.connect.MyFriendsAdd}: java.lang.NullPointerException
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.os.Looper.loop(Looper.java:130)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at java.lang.reflect.Method.invoke(Method.java:507)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at dalvik.system.NativeStart.main(Native Method)
10-29 11:13:29.411: E/AndroidRuntime(2463): Caused by: java.lang.NullPointerException
10-29 11:13:29.411: E/AndroidRuntime(2463):     at com.fb.connect.MyFriendsAdd.SetAllFacebookFriends(MyFriendsAdd.java:193)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at com.fb.connect.MyFriendsAdd.onCreate(MyFriendsAdd.java:145)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-29 11:13:29.411: E/AndroidRuntime(2463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-29 11:13:29.411: E/AndroidRuntime(2463):     ... 11 more
4

1 回答 1

1

我在这里看到的问题:

  1. onCreate()尚未完成,当您显示对话框时。
  2. dismiss()从 UI 线程以外的线程调用进度条。
  3. 从方法更新Button,您在 UI 线程以外的线程上运行。

尝试使用Handleror AsyncTask

NPE 位于MyFriendsAdd.SetAllFacebookFriends,因此您可以发布该代码,以准确查明特定原因。

于 2012-10-29T03:29:53.620 回答