单击时自动关闭应用程序 - Android

Closing Application Automatically When I Click - Android

我是 Android 的新程序员。 我正在尝试创建一个有 2 个按钮的应用程序。 如果您点击第一个,我想发送一个 HTMLpost。对于第二个,我想创建一个新的 activity。 但是当我在第一个对话框中单击 "YES" 时,应用程序关闭。 当我想创建新的 activity 时发生了一些事情。我只需单击第二个,应用程序就会自动关闭。

我不明白这是怎么回事。你能帮助我吗? 谢谢!

这是MainActivity.java:

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

新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>

您正在对话框的 onClick 中的 UI 线程上执行网络请求...这应该会产生一个 android.os.NetworkOnMainThreadException,并导致您的应用程序崩溃。

您应该使用 AsyncTask 或创建一个新线程在后台执行您的网络请求。

编辑:我刚刚注意到,您正在为两个按钮使用一个变量...因此,您永远不会输入网络请求的逻辑,因为您正在设置对 button 变量,它指向第二个按钮,带有新的 onClickListener...

您已经为单个 ID 编写了两个按钮点击侦听器.....您添加了按钮但没有按钮警报.. 添加了按钮警报和一个按钮单击侦听器另一个按钮警报..我会

public class MainActivity extends Activity { final Context context = this;私人按钮按钮; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);按钮 = (按钮) findViewById(R.id.buttonAlert); // 添加按钮监听器 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder alertDialogBu​​ilder = new AlertDialog.Builder( context); // 设置title alertDialogBuilder.setTitle("Do you want to run it?"); // 设置对话框消息 alertDialogBu​​ilder .setMessage("Your Choice?") .setCancelable(false) .setPositiveButton("YES",new DialogInterface.OnClickListener( ) { public void onClick(DialogInterface dialog,int id) { HttpClient httpclient = new DefaultHttpClient(); // 把你的服务器地址和接收器文件放在这里 HttpPost httppost = new HttpPost("http://yoursite/yourPHPScript.php" ); try { List nameValuePairs = new ArrayList(2); // message 是我们接收的参数,它的值为 1,这是将从您的服务器发送到 Arduino 开发板的值 nameValuePairs.add( new BasicNameValuePair("message", "1")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); // 向服务器发送参数 } catch (ClientProtocolException e ) { // TODO 自动生成的 catch 块 } catch (IOException e) { // TODO 自动生成的 catch 块 } dialog.cancel(); } }) .setNeg ativeButton("NO",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // 如果单击此按钮,则关闭 // 对话框,什么也不做dialog.cancel(); } }); // 创建警告对话框 AlertDialog alertDialog = alertDialogBuilder.create(); // 显示 alertDialog.show(); } });按钮 = (按钮) findViewById(R.id.button); // putt another I'd for button alert here Capture button clicks button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // 开始 NewActivity.class Intent myIntent = new Intent (MainActivity.this, NewActivity.class); 开始Activity(myIntent); } }); } }