当我按下按钮时,应用程序没有工作,没有错误发送

时间:2015-08-09 07:05:25

标签: android

我必须编写一个可以ping给定URL的应用程序。 我只是写一个带有类和布局的简单应用程序。但它没有用。 当我写网址并按下按钮时,应用程序崩溃

我的班级     package com.example.administrator.saraping;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class MainActivity extends Activity {

private EditText editText;
private String uRL;
private BufferedReader bufferedReader;
private InputStreamReader inputStreamReader;
private Process process;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText editTextt = (EditText) findViewById(R.id.editText);
     uRL = editText.getText().toString();
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            ping(uRL);
        }
    };
}

public void ping(String url) {
   String str = "";
    try {
        Process process = Runtime.getRuntime().exec("/system/bin/ping -c 8 " + url);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int i;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((i = bufferedReader.read(buffer)) > 0)
            output.append(buffer, 0, i);
        bufferedReader.close();

        str = output.toString();

    } catch (IOException e) {

        e.printStackTrace();
    }

    TextView ping = (TextView) findViewById(R.id.ping);
    ping.setText(str);
}
}

我的布局

<?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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">

        <TextView android:text="@string/Insert"
            android:textSize="34dp"
            android:textColor="#800000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="316dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_weight="0.16" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/submit"
            android:textColor="#800000"
            android:id=@+id/button" />

        <TextView
            android:textSize="34dp"
            android:textColor="#800000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ping" />


    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我让你的代码正常工作,但它有很多小问题。

主要问题:

  • 活动需要公开
  • 网络操作需要在后台线程中,我使用了AsyncTask。
  • 目前尚不清楚如何设置按钮点击事件,我只是在Java代码中进行了设置。
  • 单击按钮时未捕获URL EditText中的文本,因此uRL字符串始终为空。

以下代码对我有用:

public class MainActivity extends Activity {

    private EditText editText;
    private String uRL;
    //private BufferedReader bufferedReader;
    //private InputStreamReader inputStreamReader;
    //private Process process;
    private Button button;
    private TextView ping; //added

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);

        ping = (TextView) findViewById(R.id.ping); //added
        button = (Button) findViewById(R.id.button); //added

        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                uRL = editText.getText().toString();

                new PingAsync().execute(uRL);
                //ping(uRL);
            }
        });
    }

    class PingAsync extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String result = ping(params[0]);
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null){
                ping.setText(result);
            }
        }
    }

    //String return value
    public String ping(String url) {
        String str = "";
        try {
            Process process = Runtime.getRuntime().exec("/system/bin/ping -c 8 " + url);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            Log.d("ping", "do ping: " + url);
            int i;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((i = bufferedReader.read(buffer)) > 0)
                output.append(buffer, 0, i);
            bufferedReader.close();

            str = output.toString();
            Log.d("ping", "ping result:: " + str);

        } catch (IOException e) {

            e.printStackTrace();
        }

       return str; //added
    }
}

此外还有一个改进的布局,其中一个嵌套的LinearLayout用于顶行,左边距和更好的字体大小(你也应该使用sp作为字体大小)。

<?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
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:weightSum="1">

        <TextView android:text="insert"
            android:textSize="24sp"
            android:textColor="#800000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="146dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_weight="0.16" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="submit"
            android:textColor="#800000"
            android:id="@+id/button" />
    </LinearLayout>

        <TextView
            android:textSize="14sp"
            android:layout_marginLeft="10dp"
            android:scrollbars = "vertical"
            android:textColor="#800000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ping" />


</LinearLayout>

结果:

enter image description here