【问题标题】:Closing Application Automatically When I Click - Android单击时自动关闭应用程序 - Android
【发布时间】:2015-04-23 20:54:01
【问题描述】:

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

我无法理解发生了什么问题。你能帮助我吗? 谢谢!

这是 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>

【问题讨论】:

  • 您是否在清单中添加了新活动? ?
  • 如果您有错误,请发布 logcat
  • 哦,我忘了添加清单。现在它起作用了。谢谢哈利

标签: android button android-activity


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您已经为单个 Id 编写了两个按钮单击侦听器.....您添加了按钮,但没有收到按钮警报。添加 Id 用于按钮警报,一个单击侦听器用于按钮警报,另一个用于按钮警报。我

    public class MainActivity extends Activity { final Context context = this;私人按钮按钮; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.buttonAlert); // 添加按钮监听 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder alertDialogBu​​ilder = new AlertDialog.Builder( context); // 设置标题 alertDialogBu​​ilder.setTitle("你想运行它?"); // 设置对话框消息 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); // 消息是我们收到的参数,它的值为 1,这是将从您的服务器发送到您的 Arduino 板的值 nameValuePairs.add(new BasicNameValuePair("message", "1")); httppost.setEntity(new UrlEncodedFormEntity( nameValuePairs)); httpclient.execute(httppost); // 发送参数给服务端r } catch (ClientProtocolException e) { // TODO 自动生成的 catch 块 } catch (IOException e) { // TODO 自动生成的 catch 块 } dialog.cancel(); } }) .setNegativeButton("NO",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // 如果这个按钮被点击,就关闭 // 对话框,什么也不做 dialog.cancel( ); } }); // 创建警报对话框 AlertDialog alertDialog = alertDialogBu​​ilder.create(); // 显示它 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) { // 启动 NewActivity.class Intent myIntent = new Intent(MainActivity.this, NewActivity.class ); startActivity(myIntent); } }); } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多