Reputation: 123
I'm a new programmer on Android. I'm trying create an application which has 2 buttons. I want to send a HTMLpost if you click first one. And for second one i want to create a new activity. But when i click "YES" on the dialog box for first one, application closes. The some thing occurs when i want to create new activity. I just click second one and application closes automatically.
I cant understand what is going on wrong. Can you help me? Thank you!
This is the MainActivity.java:
package com.onur.proje;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonAlert);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Do you want to run it?");
// set dialog message
alertDialogBuilder
.setMessage("Your Choice?")
.setCancelable(false)
.setPositiveButton("YES",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
HttpClient httpclient = new DefaultHttpClient();
// put the address to your server and receiver file here
HttpPost httppost = new HttpPost("http://yoursite/yourPHPScript.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// message is the parameter we are receiving, it has the value of 1 which is the value that will be sent from your server to your Arduino board
nameValuePairs.add(new BasicNameValuePair("message", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost); // send the parameter to the server
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
dialog.cancel();
}
})
.setNegativeButton("NO",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
button = (Button) findViewById(R.id.button);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
NewActivity.class);
startActivity(myIntent);
}
});
}
}
New Activity.java:
import android.os.Bundle;
import android.app.Activity;
public class NewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from new_activity.xml
setContentView(R.layout.new_activity);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.onur.proje.MainActivity" >
<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:text="RUN IT" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonAlert"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="Button" />
</RelativeLayout>
new_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Upvotes: 0
Views: 1483
Reputation: 13
You have wrote two button click listener for single Id ..... U have added button only not got button alert.. Add I'd for button alert and one click listener for button another for button alert.. I'd
public class MainActivity extends Activity { final Context context = this; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.buttonAlert); // add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context); // set title alertDialogBuilder.setTitle("Do you want to run it?"); // set dialog message alertDialogBuilder .setMessage("Your Choice?") .setCancelable(false) .setPositiveButton("YES",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { HttpClient httpclient = new DefaultHttpClient(); // put the address to your server and receiver file here HttpPost httppost = new HttpPost("http://yoursite/yourPHPScript.php"); try { List nameValuePairs = new ArrayList(2); // message is the parameter we are receiving, it has the value of 1 which is the value that will be sent from your server to your Arduino board nameValuePairs.add(new BasicNameValuePair("message", "1")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); // send the parameter to the server } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } dialog.cancel(); } }) .setNegativeButton("NO",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } }); button = (Button) findViewById(R.id.button); // putt another I'd for button alert here Capture button clicks button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // Start NewActivity.class Intent myIntent = new Intent(MainActivity.this, NewActivity.class); startActivity(myIntent); } }); } }
Upvotes: 0
Reputation: 371
You are executing a Network Request on the UI thread in the Dialog's onClick... this should produce an android.os.NetworkOnMainThreadException
, and causes your app to crash.
You should use an AsyncTask
or just create a new thread to execute your network request in the background.
EDIT: I just noticed, that you are using one variable for both of you buttons... so, you never enter in the logic for the network request, as you are setting a new reference to the button
variable, which is pointing to the second button, with a new onClickListener
...
Upvotes: 1