2012-02-24 82 views
1

更新:AndroidRuntime:了java.lang.RuntimeException:無法內螺紋已不叫Looper.prepare創建處理程序()

好了,所以我創建了一個名爲Get_WebPage1新類和init我擴展了類使用的AsyncTask ...,如下所示,但我仍然得到同樣的錯誤:

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

調用類:

//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(":::"); 

端更新

我在我發佈我的問題之前,我做了搜索,我沒有發現任何與我正在做什麼有關的事情,請裸露在我身邊。

我想讀通過網站.htm文件和它的工作沒有任何問題,但突然間,我開始收到此錯誤信息:花時間使用Google,但無濟於事..

AndroidRuntime(5715): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

這裏是我的代碼:

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

//這裏是什麼,我要上網和閱讀.htm頁:

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

錯誤堆棧:

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 [email protected] 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 [email protected] 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) 

回答

2

我建議你使用的AsyncTask這一點。在doInBackground中執行loadList並在onPostExecute上載UI。
這個問題通常是因爲你在線程中使用了UI。例如,顯示敬酒

Android AsyncTask

+0

你能重定向我的網頁,做你正在試圖建議我嗎? – 2012-02-24 05:32:24

+0

據我瞭解你的問題,你正試圖下載一個網頁,並在用戶界面上顯示進度對話框。所以,在'doInBackground'中,你只需要完成那些繁重的任務。 'onPreExecute',你顯示一個progressdialog和'onPostExecute'只是'解僱':) – 2012-02-24 05:38:00

+0

我已經更新了我的問題,請看看。 – 2012-02-24 06:06:12

0
Toast.makeText(
    this, "You have to be connected to the internet for this application to work" + e.getMessage(), Toast.LENGTH_LONG).show(); 

吐司僅能顯示在UI線程沒有創建線程..現在你正在做同樣的...

並加入到使用AsyncTask,這是更好的後臺進程..

+1

@AbuHamzah ..我可以回答,但已經有100多個了..谷歌如何使用AsyncTask ..你會得到很多... – ngesh 2012-02-24 05:31:21

相關問題