Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8025619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:13:26+00:00 2026-06-04T23:13:26+00:00

here how i call the thread…i think error also from here private void retrieveDataFromServer(String

  • 0

here how i call the thread…i think error also from here

 private void retrieveDataFromServer(String term){

    nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("text1",term));
    Thread thread =  new Thread(null, ChkSuggestionListConn);
    thread.start();
}

 //check internet connection

        private boolean chkConnectionStatus(){

        ConnectivityManager connMgr = (ConnectivityManager)
        this.getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi =
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile =
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if( wifi.isAvailable() ){     
          return true;
         }
         else if( mobile.isAvailable() ){     
              return true;        
         }
         else
         { 
             //Toast.makeText(this, "Kindly check your internet" , Toast.LENGTH_LONG).show();
             startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
             return false;
         }
    }
    private Runnable ChkUserPassConn = new Runnable(){
        @Override
    public void run() {
        //Set flag so we cant load new items 2 at the same time
        //ChkItemsConn = true;
            if(chkConnectionStatus()==true){

                name_str = username_edt.getText().toString();
                pass_str = password_edt.getText().toString();
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("user",name_str));
                nameValuePairs.add(new BasicNameValuePair("pass",pass_str));
                try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http link");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost); 
                    HttpEntity entity = response.getEntity();
                    InputStream is = entity.getContent();


                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();

                    result_str=sb.toString();


                }
                catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
                    Toast.makeText(search_name.this, "Error : Internet Problem", Toast.LENGTH_LONG).show();
                }


            }else if(chkConnectionStatus()==false){

            }
        //Done! now continue on the UI thread
        runOnUiThread(returnRes0);
        }
    };  

    private Runnable returnRes0 = new Runnable() {
    @Override
    public void run() {
        if (result_str.equals("")==false){
            //Toast.makeText(ShaketimamaActivity.this,result,Toast.LENGTH_SHORT).show();
            //String userid2 = name;
            ProgressDialog progress = new ProgressDialog(search_name.this);
            progress.setMessage("Loading...please wait..");
            progress.setTitle("SmartShopaholic");
            new MyTask(progress).execute();
            Intent intent2 = new Intent(getApplicationContext(), mainpage.class);
            intent2.putExtra("userid",result_str.trim());
            startActivity(intent2);
        }else{
            Toast.makeText(search_name.this,"Failed login!",Toast.LENGTH_SHORT).show();
        }
        }
    };
    private Runnable ChkFavListConn = new Runnable(){
        @Override
    public void run() {
        //Set flag so we cant load new items 2 at the same time
        //ChkItemsConn = true;
            if(userid.equals("")){
                ProgressDialog progressA = new ProgressDialog(search_name.this);
                progressA.setMessage("Loading...please wait..");
                progressA.setTitle("SmartShopaholic");
                new MyTask(progressA).execute();
                Intent intent = new Intent(getApplicationContext(), register.class);
                startActivity(intent);
            }else{
                ArrayList<NameValuePair> passvalue = new ArrayList<NameValuePair>();
                passvalue.add(new BasicNameValuePair("userid",userid.trim()));
                try{
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost("http link");
                    post.setEntity(new UrlEncodedFormEntity(passvalue));
                    HttpResponse HRes = client.execute(post); 
                    HttpEntity HEn = HRes.getEntity();
                    InputStream stream = HEn.getContent();

                    BufferedReader BRead = new BufferedReader(new InputStreamReader(stream,"iso-8859-1"),8);
                    StringBuilder SBuilder = new StringBuilder();
                    String getline = null;
                    while ((getline = BRead.readLine()) != null) {
                        SBuilder.append(getline + "\n");
                    }
                    stream.close();
                    System.out.println("dsa");
                    answer2=SBuilder.toString().trim();             
                }
                catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
                }

            }
        //Done! now continue on the UI thread
        runOnUiThread(returnRes2);
        }
    };  

    private Runnable returnRes2 = new Runnable() {
        @Override
        public void run() {
            if(answer2.equals("yes")){
                ProgressDialog progressA = new ProgressDialog(search_name.this);
                progressA.setMessage("Loading...please wait..");
                progressA.setTitle("SmartShopaholic");
                new MyTask(progressA).execute();
                Intent intent1 = new Intent(getApplicationContext(), favoritelist.class);
                intent1.putExtra("userid",userid);
                startActivity(intent1); 
            }else{
                ProgressDialog progress = new ProgressDialog(search_name.this);
                progress.setMessage("Loading...please wait..");
                progress.setTitle("SmartShopaholic");
                new MyTask(progress).execute();
                Intent intent = new Intent(getApplicationContext(), noresult.class);                    
                startActivity(intent);
            }
            }
        };
        private Runnable ChkSearchListConn = new Runnable(){
            @Override
        public void run() {
            //Set flag so we cant load new items 2 at the same time
            //ChkItemsConn = true;
                if(chkConnectionStatus()==true){
                    name = getname.getText().toString();

                    if((name.equals(""))){

                        Toast.makeText(search_name.this, "Blank Field..Please Enter", Toast.LENGTH_LONG).show();
                    }else{
                        ArrayList<NameValuePair> passvalue = new ArrayList<NameValuePair>();
                         passvalue.add(new BasicNameValuePair("choose","pName"));
                         passvalue.add(new BasicNameValuePair("text1",name));
                            try{
                                HttpClient client = new DefaultHttpClient();
                                HttpPost post = new HttpPost("http link");
                                post.setEntity(new UrlEncodedFormEntity(passvalue));
                                HttpResponse HRes = client.execute(post); 
                                HttpEntity HEn = HRes.getEntity();
                                InputStream stream = HEn.getContent();


                                BufferedReader BRead = new BufferedReader(new InputStreamReader(stream,"iso-8859-1"),8);
                                StringBuilder SBuilder = new StringBuilder();
                                String getline = null;
                                while ((getline = BRead.readLine()) != null) {
                                    SBuilder.append(getline + "\n");
                                }
                                stream.close();
                                System.out.println("dsa");
                                answer=SBuilder.toString().trim();              
                            }
                            catch(Exception e){
                                Log.e("log_tag", "Error converting result "+e.toString());
                            }

                    }
                }else{

                }
            //Done! now continue on the UI thread
            runOnUiThread(returnRes3);
            }
        };  

        private Runnable returnRes3 = new Runnable() {
            @Override
            public void run() {
                if(answer.equals("yes")){
                    ProgressDialog progress = new ProgressDialog(search_name.this);
                    progress.setMessage("Loading...please wait..");
                    progress.setTitle("SmartShopaholic");
                    new MyTask(progress).execute();
                    Intent intent = new Intent(getApplicationContext(), name_result.class);
                    System.out.println("userid="+userid+",productname="+name);
                    intent.putExtra("productname",name);
                    intent.putExtra("userid", userid);

                    startActivity(intent);
                }else{
                    ProgressDialog progress = new ProgressDialog(search_name.this);
                    progress.setMessage("Loading...please wait..");
                    progress.setTitle("SmartShopaholic");
                    new MyTask(progress).execute();
                    Intent intent = new Intent(getApplicationContext(), noresult.class);                    
                    startActivity(intent);
                }
                }
            };
            private Runnable ChkSuggestionListConn = new Runnable(){
                @Override
            public void run() {
                //Set flag so we cant load new items 2 at the same time
                //ChkItemsConn = true;
                    //System.out.println(number);
                    //http post
                    try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http link");
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost); 
                        HttpEntity entity = response.getEntity();
                        InputStream is = entity.getContent();

                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        is.close();
                        result=sb.toString();
                    }
                    catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                       // Toast.makeText(SearchResult.this, "Error : Internet Problem", Toast.LENGTH_LONG).show();
                    }
                    //parse json data
                    try{
                        JSONArray jArray = new JSONArray(result);
                        myBooks = new ArrayList<HashMap<String,Object>>();
                            HashMap<String, Object> hm;
                          //System.out.println(result);  
                            //With the help of HashMap add Key, Values of Book, like name,price and icon path 
                            JSONObject json_data;
                        String[]slist=new String[jArray.length()];    
                        String nameproduct=null;
                        for(int i=0;i<jArray.length();i++){
                            json_data = jArray.getJSONObject(i);
                            nameproduct=json_data.getString("pName");
                            slist[i]=nameproduct;
                        }
                        Set<Object> uniqueSet = new HashSet<Object>(Arrays.asList(slist));
                        Object[] unique = uniqueSet.toArray();
                        for(int i=0;i<unique.length;i++){
                            hm = new HashMap<String, Object>();
                            hm.put(searchTerm, unique[i]);
                            hm.put(picpic, R.drawable.ic_search);
                            myBooks.add(hm);
                            json_data=null;
                        }  
                    }catch(Exception e){
                        listView.setVisibility(View.GONE);       
                    }
                //Done! now continue on the UI thread
                runOnUiThread(returnRes4);
                }
            };  

            private Runnable returnRes4 = new Runnable() {
                @Override
                public void run() {
                    SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), myBooks, R.layout.autocomplete_listbox, 
                            new String[]{picpic,searchTerm}, new int[]{R.id.image1,R.id.text1});    
                            listView.setAdapter(adapter);
                            listView.setVisibility(View.VISIBLE);
                            listView.setTextFilterEnabled(true);
                    listView.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                            String selection = listView.getItemAtPosition(position).toString();
                            //Example selection:{ companyName=ckwai1990, pname=dsa, pID=P000000005, price=RM 1230.00 } 
                            int begin=selection.indexOf("searchTerm=");  
                            int end=selection.length()-1;
                            String productID=selection.substring(begin+11,end);
                            getname.setText(productID);
                            listView.setVisibility(View.GONE);
                            }
                        });
                    }
                };

this is my error log

06-04 12:26:51.331: E/AndroidRuntime(12711): FATAL EXCEPTION: Thread-17
06-04 12:26:51.331: E/AndroidRuntime(12711): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.ViewRoot.checkThread(ViewRoot.java:3022)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.ViewRoot.requestLayout(ViewRoot.java:635)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.widget.TableLayout.requestLayout(TableLayout.java:226)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.requestLayout(View.java:8320)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.widget.AbsListView.requestLayout(AbsListView.java:1202)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.setFlags(View.java:4694)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at android.view.View.setVisibility(View.java:3169)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at com.skyhaw.search_name$7.run(search_name.java:789)
06-04 12:26:51.331: E/AndroidRuntime(12711):    at java.lang.Thread.run(Thread.java:1019)

the error above given because i doing a change with the ListView in the thread method as below is the fatal error part:

        private Runnable ChkSuggestionListConn = new Runnable(){
            @Override
        public void run() {
        try{
        ... here is get my http link connection
        }(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
        }
        try{
        ... here is get my http result and put it into array
        }(Exception e){
         listView.setVisibility(View.GONE);
         //so the problem at here
        }

here giving my solution which solved by a handle method

        public void handleMessage(Message msg) {
        super.handleMessage(msg);

        if(msg.what == EMPTY_RESULT){
            System.out.println("handleMsg EMPTY_RESULT");
            listView.setVisibility(View.GONE);
            }
          }
        };

        //note that handle method is do at the above OnCreate

        private Runnable ChkSuggestionListConn = new Runnable(){
            @Override
        public void run() {
        try{
        ... here is get my http link connection
        }(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
        }
        try{
        ... here is get my http result and put it into array
        }(Exception e){
         messageHandler.sendEmptyMessage(EMPTY_RESULT);

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:13:28+00:00Added an answer on June 4, 2026 at 11:13 pm

    I think your issue is here:

    getname.setText(productID);
    listView.setVisibility(View.GONE);
    

    You are trying to modify the IU thread from another thread. You should use a post() method to call the Runnable in the UI thread. Here is a similar question in SO, although this one uses an AsyncTask: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

    You can also see an example of how to use post() here:
    http://developer.android.com/resources/articles/painless-threading.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the call stack from a user's crash report : Thread 0 Crashed: Dispatch
class ... { onClick() { while(true) { //call the thread from here threadpool(request)//send request
What should I do to call thread's pause() method from showExitDialog() here ? Here's
I have heard that DateTime.Now is very expensive call (from here ) Is GETDATE()
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
I have some code here to call minizip(), a boilerplate dirty renamed main() of
I use EGORefreshTableHeaderView to update my tableview. Here I call my method to download
Using Fluent Interface design here if i call something like dog.Train(Running).Train(Eating).Do(Running).Do(Eating); what is the
Here is the ajax call: $.ajax({ url: /TheControllerName/MyMethod, type: GET, contentType: application/json, dataType: 'json',
Here is a template I call to generate a menu, and it kinda breaks.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.