Domanda

I am getting a NullPointerException error in my code, I have narrowed it down to be this piece of code that is causing the exception. Can anyone help?

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

And I have second problem is: When I click in this button I get the following exception: "android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views." How can I solve? thanks .

        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.
È stato utile?

Soluzione

You are updating UI (TextView settext) on thread thats why your getting error.

                      // 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."

Altri suggerimenti

You are updating the UI from threads. So the error occur. If you want to update ui from thread then use handlers. Try this SO Thread for more info

Try this:

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

For more information for using ui thread you can take reference of Link.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top