首先让我澄清一下我的问题。
我这样做,解析数据(使用DOM)并将其绑定在我的listview上。一切正常,直到我发现它需要很长时间才能做出回应。为了实现这一点,我提出了一个想法,我应该把它放在一个线程中。所以我添加了一个扩展线程的类。
我的解析代码
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
答案 0 :(得分:1)
我在这里看到的问题:
onCreate()
当您显示对话框时尚未完成。dismiss()
。Button
,运行UI线程以外的线程。尝试使用Handler
或AsyncTask
。
NPE位于MyFriendsAdd.SetAllFacebookFriends
,因此您可以发布该代码,以准确查明特定原因。