HEre is my RegisterActivity:
public class RegisterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
public void sendRegistration(View view) {
TextView txtName = (TextView) findViewById(R.id.txtName);
TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
TextView txtPassword = (TextView) findViewById(R.id.txtPassword);
String strName = txtName.getText().toString();
String strEmail = txtEmail.getText().toString();
String strPassword = txtPassword.getText().toString();
if (strName.isEmpty() || strEmail.isEmpty() || strPassword.isEmpty()) {
// One or more fields are empty
Toast toast = Toast
.makeText(
getApplicationContext(),
"One or more fields have been left blank. Please fill them in.",
Toast.LENGTH_SHORT);
toast.show();
} else {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.******.**/register.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name", strName));
nameValuePairs.add(new BasicNameValuePair("email", strEmail));
nameValuePairs.add(new BasicNameValuePair("password", strPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
startActivityForResult(new Intent(view.getContext(),
RegFinishActivity.class), 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
What my goal is, is to validate that the email, name, password is not empty then send a request to my server, but when I run this code on my device, it says “Applciation has crashed”. I am running Android 4.0.3, and infact do have the permission for internet in the manifest. What else could cause this problem? Without the http request, this runs fine, and the next view is shown.
LogCat log from start to finish of application:
01-08 08:58:06.793: D/dalvikvm(20672): GC_CONCURRENT freed 60K, 3% free 9439K/9671K, paused 4ms+6ms
01-08 08:58:06.929: D/OpenGLRenderer(20672): Flushing caches (mode 0)
01-08 08:58:15.597: D/AndroidRuntime(20672): Shutting down VM
01-08 08:58:15.597: W/dalvikvm(20672): threadid=1: thread exiting with uncaught exception (group=0x40a2b1f8)
01-08 08:58:15.609: E/AndroidRuntime(20672): FATAL EXCEPTION: main
01-08 08:58:15.609: E/AndroidRuntime(20672): java.lang.IllegalStateException: Could not execute method of the activity
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View$1.onClick(View.java:3044)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View.performClick(View.java:3511)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View$PerformClick.run(View.java:14105)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.Handler.handleCallback(Handler.java:605)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.Looper.loop(Looper.java:137)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 08:58:15.609: E/AndroidRuntime(20672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-08 08:58:15.609: E/AndroidRuntime(20672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-08 08:58:15.609: E/AndroidRuntime(20672): at dalvik.system.NativeStart.main(Native Method)
01-08 08:58:15.609: E/AndroidRuntime(20672): Caused by: java.lang.reflect.InvocationTargetException
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.view.View$1.onClick(View.java:3039)
01-08 08:58:15.609: E/AndroidRuntime(20672): ... 11 more
01-08 08:58:15.609: E/AndroidRuntime(20672): Caused by: android.os.NetworkOnMainThreadException
01-08 08:58:15.609: E/AndroidRuntime(20672): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-08 08:58:15.609: E/AndroidRuntime(20672): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-08 08:58:15.609: E/AndroidRuntime(20672): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-08 08:58:15.609: E/AndroidRuntime(20672): at sms.web.RegisterActivity.sendRegistration(RegisterActivity.java:58)
01-08 08:58:15.609: E/AndroidRuntime(20672): ... 14 more
01-08 08:58:18.336: I/Process(20672): Sending signal. PID: 20672 SIG: 9
Have you requested internet permission in your manifest file?
Add
in your manifest file above the application tag
Please post your logcat log if you have done this and you still encounter the error.
As said in my comment, you should use an AsyncTask to perform this task outside of the main Thread. Something like this, but then with correct variable scopes. This class should be added inside you RegisterActivity class as an inner class. YOu can then launch the task by:
Some of the stuff in the doInBackground could be done in the postExecute or the preExecute like the activity launch.