Reputation: 20078
I have created a new class called Get_WebPage1 and init I have extends that class to use AsyncTask... as show below but still I get the same error:
public class Get_Webpage1 extends AsyncTask<String, Void, String> {
public String parsing_url = "";
public Get_Webpage1(String url_2_get){
parsing_url = url_2_get;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(parsing_url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
String html = "";
InputStream in = null;
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
....
....
return html;
}
}
Calling the class:
//Looper.myLooper().prepare();
String url = "http://ofertaweb.ro/android/sleepandlovemusic/list_files.php";
Get_Webpage1 obj = new Get_Webpage1(url);
directory_listings = obj.doInBackground("test");//get_webpage_source(); songs_array = directory_listings.split(":::");
end update
I am trying to read the .htm file through website and it was working without any issues but suddenly I started getting this error message: and spend time googling but no avail..
AndroidRuntime(5715): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Here is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_orders = new ArrayList<Order>();
this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
setListAdapter(this.m_adapter);
selelctedFile = (TextView)findViewById(R.id.selectedfile);
seekbar = (SeekBar)findViewById(R.id.seekbar);
playButton = (ImageButton)findViewById(R.id.play);
prevButton = (ImageButton)findViewById(R.id.prev);
nextButton = (ImageButton)findViewById(R.id.next);
player = new MediaPlayer();
playButton.setOnClickListener(onButtonClick);
nextButton.setOnClickListener(onButtonClick);
prevButton.setOnClickListener(onButtonClick);
viewOrders = new Runnable(){
public void run() {
loadList();
getOrders();
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(this, "Please wait...", "Retrieving data ...", true);
}
//here is what I am going to internet and reading the .htm page:
private void loadList(){
try
{
//Looper.myLooper().prepare();
String url = "http://ofertaweb.ro/android/sleepandlovemusic/list_files.php";
Get_Webpage obj = new Get_Webpage(url);
directory_listings = obj.get_webpage_source(); //
songs_array = directory_listings.split(":::");
}
catch (Exception e) {
Toast.makeText(
this, "You have to be connected to the internet for this application to work" + e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}
}
error stack:
02-24 00:02:25.507: E/AndroidRuntime(5715): FATAL EXCEPTION: MagentoBackground
02-24 00:02:25.507: E/AndroidRuntime(5715): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-24 00:02:25.507: E/AndroidRuntime(5715): at android.os.Handler.<init>(Handler.java:121)
02-24 00:02:25.507: E/AndroidRuntime(5715): at android.widget.Toast.<init>(Toast.java:68)
02-24 00:02:25.507: E/AndroidRuntime(5715): at android.widget.Toast.makeText(Toast.java:231)
02-24 00:02:25.507: E/AndroidRuntime(5715): at com.softberries.sf.SoftwarePassionView.loadList(SoftwarePassionView.java:262)
02-24 00:02:25.507: E/AndroidRuntime(5715): at com.softberries.sf.SoftwarePassionView.access$11(SoftwarePassionView.java:239)
02-24 00:02:25.507: E/AndroidRuntime(5715): at com.softberries.sf.SoftwarePassionView$4.run(SoftwarePassionView.java:87)
02-24 00:02:25.507: E/AndroidRuntime(5715): at java.lang.Thread.run(Thread.java:1019)
02-24 00:02:26.517: E/WindowManager(5715): Activity com.softberries.sf.SoftwarePassionView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40526408 that was originally added here
02-24 00:02:26.517: E/WindowManager(5715): android.view.WindowLeaked: Activity com.softberries.sf.SoftwarePassionView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40526408 that was originally added here
02-24 00:02:26.517: E/WindowManager(5715): at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-24 00:02:26.517: E/WindowManager(5715): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-24 00:02:26.517: E/WindowManager(5715): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-24 00:02:26.517: E/WindowManager(5715): at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.Dialog.show(Dialog.java:241)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ProgressDialog.show(ProgressDialog.java:107)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ProgressDialog.show(ProgressDialog.java:90)
02-24 00:02:26.517: E/WindowManager(5715): at com.softberries.sf.SoftwarePassionView.onCreate(SoftwarePassionView.java:94)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-24 00:02:26.517: E/WindowManager(5715): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 00:02:26.517: E/WindowManager(5715): at android.os.Looper.loop(Looper.java:123)
02-24 00:02:26.517: E/WindowManager(5715): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-24 00:02:26.517: E/WindowManager(5715): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 00:02:26.517: E/WindowManager(5715): at java.lang.reflect.Method.invoke(Method.java:507)
02-24 00:02:26.517: E/WindowManager(5715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-24 00:02:26.517: E/WindowManager(5715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
Upvotes: 1
Views: 4749
Reputation: 1165
I suggest you to use AsyncTask for this. Do the loadList in doInBackground
and upload UI in onPostExecute
.
This problem usually because you do something with UI in the thread. E.g Displaying a Toast
Upvotes: 2
Reputation: 13501
Toast.makeText(
this, "You have to be connected to the internet for this application to work" + e.getMessage(), Toast.LENGTH_LONG).show();
Toast can be shown only in UI Thread not in threads you create.. Right now you are doing the same...
And adding to that use AsyncTask, which is better for background process..
Upvotes: 0