Let me first clarify my question.
I am doing this,parsing datas (Using DOM) and bind it on my listview. Everything works fine until I found out that it is taking to long to respond. To accomplish that, I come up with an idea that I should put it inside a Thread. So Ive added a class that extends thread.
My code on parsing
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();
}
}
And added this class
public class thread extends Thread {
public void run() {
SetFriendString();
//SetMyRequestString();
//SetMyFriendRequestString();
pDialog.dismiss();
}
}
Now I am calling this class on my 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();
Now my app forces to close. Using debugger I found out that the thread skips my methods. Why is that?
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
Issues I see here:
onCreate()Hasn’t finished yet, when you are showing a dialog.dismiss()on progressbar, from a thread other than UI thread.Buttonfrom a method , you run on a thread other than UI thread.Try using
HandlerorAsyncTaskfor this.The NPE is at
MyFriendsAdd.SetAllFacebookFriends, so you can post that code, to exactly pinpoint the particular cause.