1

我在我的代碼中得到一個NullPointerException錯誤,我已經縮小到這段代碼導致異常。誰能幫忙?如何解決Android中的NullPointerException和ThreadException錯誤

url = new URL(Http.toString()); 

Iterator is a raw type. References to generic type Iterator<E> should be parameterized User_menu.java /RSBUYclient/src/com/example/rsbuyclient line 141 Java Problem 
04-24 11:34:29.756: W/System.err(32259): java.lang.NullPointerException 

而且我有第二個問題是:當我在這個按鈕我得到下面的異常點擊:「android.view.ViewRootImpl $ CalledFromWrongThreadException:只有創建視圖層次原線可以觸及它的觀點。「 我該如何解決?謝謝 。

 private OnClickListener btsearchitemListener = new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 

      final Handler handler = new Handler(); 
      handler.post(new Runnable() { 
      //User_menu.this.runOnUiThread(new Runnable() { 
      //User_menu.this.runOnUiThread(new Runnable(){ 
       public void run() { 
        // TODO Auto-generated method stub           
        editText1.setHint(""); 

        //URL url ; 
        //String Http_keyWord = "http://tw.search.bid.yahoo.com/search/auction/product?p=%s", Http, Keyword; 
        //String Http_keyWord = "https://tw.search.yahoo.com/search?p=u+zj6&fr=yfp&ei=utf-8&v=0", Http, Keyword;  

        AllDatainfos = new ArrayList<HashMap<String, Object>>(); 

        Keyword = editText1.getText().toString().trim(); 
        tvspottempp = (TextView)findViewById(R.id.tvspottempp); 
        tvspottempp.setText("Keyword:"+Keyword);             

        Http = String.format(Http_keyWord, UTF8EnCode(Keyword)); 
        tvspottempp1 = (TextView)findViewById(R.id.tvspottempp1); 
        tvspottempp1.setText("Http:"+Http); 
       } 
       }); 

      User_menu.this.runOnUiThread(new Runnable(){ 
        public void run() { 
         try {           
           url = new URL(Http.toString()); 
           tvspottempp2 = (TextView)findViewById(R.id.tvspottempp2); 
           tvspottempp2.setText("url:"+url); 

           Document doc;     
           doc = Jsoup.parse(url, 3000); 
           tvspottempp2 = (TextView)findViewById(R.id.tvspottempp3); 
           tvspottempp2.setText("doc:"+doc); 

           Elements paramDocuments = doc.select("#srp_result_list, #srp_gaze_list").select(".grid-type.yui3-g > *"); 

           Iterator localIterator = paramDocuments.select("#srp_result_list, #srp_gaze_list").select(".grid-type.yui3-g > *").iterator(); 

           while (true) 
           { 
           if (!localIterator.hasNext()) 
            return; 
           Element localElement1 = (Element)localIterator.next(); 
           try 
           { 
            SearchResultItem localSearchResultItem = new SearchResultItem(); 
            Element localElement2 = localElement1.select(".wrap").first(); 
            parsePhoto(localSearchResultItem, localElement2); 
            parsePrice(localSearchResultItem, localElement2); 
            parseTitle(localSearchResultItem, localElement2);      
           } 
           catch (Exception localException2) 
           { 
            localException2.printStackTrace(); 
           } 
           } 

         }catch (MalformedURLException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          catch (Exception localException1) 
          { 
           localException1.printStackTrace(); 
          } try { 
        Thread.sleep(5000) ; 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }     
        } 
       }); 


       } 
       }; 

04-24 12:25:31.231: W/System.err(9995): java.lang.NullPointerException 
04-24 12:25:31.236: W/System.err(9995):  at com.example.rsbuyclient.User_menu$1$2.run(User_menu.java:138) 
04-24 12:25:31.236: W/System.err(9995):  at android.app.Activity.runOnUiThread(Activity.java:4893) 
04-24 12:25:31.236: W/System.err(9995):  at com.example.rsbuyclient.User_menu$1.onClick(User_menu.java:135) 
04-24 12:25:31.236: W/System.err(9995):  at android.view.View.performClick(View.java:4475) 
04-24 12:25:31.236: W/System.err(9995):  at android.view.View$PerformClick.run(View.java:18786) 
04-24 12:25:31.236: W/System.err(9995):  at android.os.Handler.handleCallback(Handler.java:730) 
04-24 12:25:31.236: W/System.err(9995):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-24 12:25:31.236: W/System.err(9995):  at android.os.Looper.loop(Looper.java:137) 
04-24 12:25:31.236: W/System.err(9995):  at android.app.ActivityThread.main(ActivityThread.java:5419) 
04-24 12:25:31.236: W/System.err(9995):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-24 12:25:31.236: W/System.err(9995):  at java.lang.reflect.Method.invoke(Method.java:525) 
04-24 12:25:31.236: W/System.err(9995):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
04-24 12:25:31.236: W/System.err(9995):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
04-24 12:25:31.236: W/System.err(9995):  at dalvik.system.NativeStart.main(Native Method) 
04-24 12:25:36.256: W/Choreographer(9995): Already have a pending vsync event. There should only be one at a time. 
+3

的第二個問題是因爲你從一個線程 – Raghunandan

+0

更新UI界面使用 「runOnUiThread」? –

+0

是的,但更好地使用asynctask – Raghunandan

回答

0

你正在更新線程上的UI(TextView settext)這就是爲什麼你會得到錯誤。

     // ui stuff updating that's why showing error 
         tvspottempp2 = (TextView)findViewById(R.id.tvspottempp2); 
         tvspottempp2.setText("url:"+url); 

Android "Only the original thread that created a view hierarchy can touch its views."

+0

我嘗試更改兩個線程,但它有runOnUiThread錯誤 –

+0

at android.app.Activity.runOnUiThread(Activity.java:4893) –

+0

04-24 12:25:31.231:W/System.err (9995):java.lang.NullPointerException 04-24 12:25:31.236:W/System.err(9995):\t at com.example.rsbuyclient.User_menu $ 1 $ 2.run(User_menu.java:138) 04 -24 12:25:31.236:W/System.err(9995):\t at android.app.Activity.runOnUiThread(Activity.java:4893) –

0

要更新從線程的用戶界面。所以發生錯誤。如果你想從線程更新ui,然後使用處理程序。嘗試this SO線程更多信息

0

試試這個:

runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         url = new URL(Http.toString()); 
         tvspottempp2 = (TextView)findViewById(R.id.tvspottempp2); 
         tvspottempp2.setText("url:"+url); 

         Document doc;     
         doc = Jsoup.parse(url, 3000); 
         tvspottempp2 = (TextView)findViewById(R.id.tvspottempp3); 
         tvspottempp2.setText("doc:"+doc); 
        } 
       }); 

有關使用UI線程你可以採取的Link參考更多的信息。

相關問題