1

我的代码中出现 NullPointerException 错误,我已将其缩小为导致异常的这段代码。任何人都可以帮忙吗?

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.
4

3 回答 3

0

您正在从线程更新 UI。所以会发生错误。如果要从线程更新 ui,请使用处理程序。试试这个SO 线程以获取更多信息

于 2014-04-24T04:15:19.590 回答
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

于 2014-04-24T04:18:59.057 回答
0

您正在线程上更新 UI(TextView settext),这就是您收到错误的原因。

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

Android“只有创建视图层次结构的原始线程才能触摸它的视图。”

于 2014-04-24T04:19:18.807 回答