Unexpected Error

Put your problem here if it does not fit any of the other categories.

Unexpected Error

Postby peek4y » Thu Sep 08, 2011 7:02 am

Hello,

I'm getting an unexpected error when i click on the button and the app is forced to close.

I've pasted the code @ pastebin

Heres the link: http://pastebin.com/nm6PgpFF

I'm not able to find out whats the problem.

Please Help.

Thank you.
peek4y
Freshman
Freshman
 
Posts: 4
Joined: Mon Sep 05, 2011 11:08 am

Top

Re: Unexpected Error

Postby Dockland » Thu Sep 08, 2011 8:06 am

Where is LogCat output? http://developer.android.com/guide/developing/debugging/ddms.html

A process != a thread. To give misleading names is bad habit.

Do not access gui objects from worker threads.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mProcess = new Thread(new Runnable()
  3.         {
  4.  
  5.                         @Override
  6.                         public void run() {
  7.                                 try
  8.                                 {
  9.                                        
  10.                                
  11.                                 text1 = et.getText().toString(); //. outch
  12.                                 httpclient = new DefaultHttpClient();
  13.                                 httpost = new HttpPost("http://androidtest.zxq.net/disp.php");
  14.                                 nameValuepair = new ArrayList<NameValuePair>(1);
  15.                                 nameValuepair.add(new BasicNameValuePair("input",text1.trim()));
  16.                                 httpost.setEntity(new UrlEncodedFormEntity(nameValuepair));
  17.                                 response = httpclient.execute(httpost);
  18.                                  ResponseHandler<String> responseHandler = new BasicResponseHandler();
  19.                                  res = httpclient.execute(httpost, responseHandler);
  20.                                  Message myMsg = mhandler.obtainMessage();
  21.                                  myMsg.obj = "Response is: ";
  22.                                  mhandler.sendMessage(myMsg);
  23.                                  
  24.                                  
  25.                                 }
  26.                                 catch(Exception e){
  27.                     tv.setText("Exception : " + e.getMessage()); // ouch
  28.                 }
  29.                                
  30.                         }
  31.        
  32.         });
  33.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


When "run" functions ends - thread ends. No need for mProcess.stop. Thread stop function is deprecated.

One thread could be started only once

public synchronized void start ()
Since: API Level 1

Starts the new Thread of execution. The run() method of the receiver will be called by the receiver Thread itself (and not the Thread calling start()).
ThrowsIllegalThreadStateException if the Thread has been started before

See Also
run()


Simple example as I understand.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package gpl.test.unlimited;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import org.apache.http.NameValuePair;
  7. import org.apache.http.client.ResponseHandler;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.impl.client.BasicResponseHandler;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.message.BasicNameValuePair;
  13.  
  14. import android.app.Activity;
  15. import android.os.Bundle;
  16. import android.os.Handler;
  17. import android.os.Message;
  18. import android.util.Log;
  19. import android.view.View;
  20. import android.widget.TextView;
  21.  
  22. public class TestThreadActivity extends Activity {
  23.        
  24.     private Handler mHandler = new Handler() {
  25.         @Override
  26.         public void handleMessage(Message msg) {
  27.                 TextView tv = (TextView) findViewById(R.id.tvOutput);
  28.                 tv.setText(msg.getData().getString("data"));
  29.         }
  30.     };
  31.    
  32.     private Thread m_downloadThread;
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.main);
  37.     }
  38.    
  39.     private class DownloadRun implements Runnable {
  40.         private String m_argument;
  41.         public void setArgument(String arg) {
  42.                 m_argument = arg;
  43.         }
  44.                 @Override
  45.                 public void run() {
  46.                         DefaultHttpClient httpClient = new DefaultHttpClient();
  47.                         HttpPost httpPost = new HttpPost("http://androidtest.zxq.net/disp.php");
  48.                         ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(1);
  49.                         nvp.add(new BasicNameValuePair("input", "test: " + new Random().nextDouble() + " "      + m_argument));
  50.                         //HttpResponse response;
  51.                         String res;
  52.                         try {
  53.                                 httpPost.setEntity(new UrlEncodedFormEntity(nvp));
  54.                                 //response = httpClient.execute(httpPost);
  55.                                 ResponseHandler<String> rh = new BasicResponseHandler();
  56.                                 res = httpClient.execute(httpPost, rh);
  57.                         } catch (Exception e) {
  58.                                 Log.d("log", e.getMessage());
  59.                                 return;
  60.                         }
  61.                        
  62.                         Message msg= mHandler.obtainMessage();
  63.                         Bundle data = new Bundle();
  64.                         data.putString("data", res);
  65.                         msg.setData(data);
  66.                         mHandler.sendMessage(msg);                     
  67.                 }
  68.     }
  69.    
  70.     public void on_btnTestThread_click(View sender) {
  71.         // if still downloading...
  72.         if (m_downloadThread != null && m_downloadThread.isAlive())
  73.                 return;
  74.         DownloadRun dr = new DownloadRun();
  75.         dr.setArgument("random number");
  76.         m_downloadThread = new Thread(dr);
  77.         m_downloadThread.start();
  78.     }
  79.    
  80. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Dockland
Developer
Developer
 
Posts: 37
Joined: Wed Jul 27, 2011 12:29 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 39 guests