1

I have an app that has 2 buttons: one does a calculation, the other launches a website with a browser. When the user clicks on button1, the answers should display. When the user clicks on button2, the browser will direct them to a website. The trouble is: clicking on button1 causess a "Complete action using" dialog to pop up, prompting the user to pick one of 4 things (application, process, etc.). This didn't happen before and is quite odd. The only thing I can think of is maybe a problem with the onclicklistener and button1 doesn't have a new intent so I don't know how this dialog is coming up.

Here is the manifest:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.costaload"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

 </application>

 </manifest>

Here is my code:

package com.example.costaload;

import com.example.costloads.R;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
EditText mile, diesel;
Button button1, button2;
TextView tv, tv2, tv3;
private double x, y, z, costper, gallon, litres, ophours, stopdrive;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
NumberFormat format = NumberFormat.getCurrencyInstance();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mile = (EditText) findViewById(R.id.mile);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(button1onClickListener);
OnClickListener button2onClickListener = null;
button2.setOnClickListener(button2onClickListener);
tv = (TextView) findViewById(R.id.cost);
tv2 = (TextView) findViewById(R.id.cpm);
tv3 = (TextView) findViewById(R.id.gallons);
diesel = (EditText) findViewById(R.id.diesel);
}

private OnClickListener button1onClickListener = new OnClickListener() {
   public void onClick(final View v) {

    x = Double.parseDouble(mile.getText().toString());
    y = Double.parseDouble(diesel.getText().toString());
    if (checkBox2.isChecked()) {
        x = x * 2;
    }
    if (checkBox1.isChecked()) {
        x = x * 0.62137;
    }
    ophours = 0;
    ophours = (x / 55) + 2;
    if (ophours >= 11) {
        stopdrive = (ophours / 10) - 1;
        if (stopdrive > 1) { // This block isn't closed.
            ophours = ophours + (stopdrive * 10);
        }
        gallon = x / 5.5;
        if (checkBox4.isChecked()) {
            gallon = gallon + (ophours * 1.1);
        }
        if (checkBox3.isChecked()) {
            litres = gallon * 3.785;
            tv3.setText(new DecimalFormat("####.##").format(litres)
                    + "L");
        }
        z = (gallon * y) + (x * 0.655);
        costper = z / x;
        tv.setText(format.format(z));
        tv2.setText(format.format(costper) + "/mile");
        tv3.setText(new DecimalFormat("####.##").format(gallon)
                + "gal.");
    }
    ;
};

private OnClickListener button2OnClickListener = new OnClickListener() {
    public void onClick(final View v) {

        Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                Url.parse("http://www.ratenroll.com"));
        startActivity(browserIntent);
    }
  };
 };
};

Here is the activity 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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="# of miles"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="40dp"
    android:text="Price of diesel"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/diesel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/mile"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="numberDecimal" />

<EditText
    android:id="@+id/mile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/checkBox3"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Visit www.ratenroll.com" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/diesel"
    android:layout_marginTop="14dp"
    android:text="Kilometres (not miles)" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_alignParentRight="true"
    android:text="Round trip" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/checkBox1"
    android:text="Litres (not gallons)" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/checkBox3"
    android:layout_alignBottom="@+id/checkBox3"
    android:layout_alignParentRight="true"
    android:text="Reefer load" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox3"
    android:layout_marginTop="50dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="Compute" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="26dp"
    android:text="Total Cost"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:text="Cost per mile"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/cost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignParentRight="true"
    android:text=" "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/cpm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView4"
    android:layout_alignBottom="@+id/textView4"
    android:layout_alignParentRight="true"
    android:text="   "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:text="Fuel required"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/gallons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView5"
    android:layout_alignBottom="@+id/textView5"
    android:layout_alignParentRight="true"
    android:text="   "
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Thank you for looking.

4

2 回答 2

1

那个额外的支架在这里做什么?

tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
         break;}  // did you mean to do this?

break;在第二个 case 语句中将大括号移到 after 。

PS 请养成正确缩进代码和对齐大括号的习惯。还可以使用 IDE 的自动代码格式化功能来保持整洁。它会让你的事情变得更容易。

PPS 对于其他读者。为什么这甚至可以编译,因为第二个 case 语句现在在 switch 之外?

于 2012-11-06T21:43:30.563 回答
1

您将相同的 onClickListener 添加到两个按钮并使用 switch 语句来区分它们的方法容易出错。

package com.example.costaload;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
    EditText mile, diesel;
    Button button1, button2;
    TextView tv, tv2, tv3;
    private double x, y, z, costper, gallon, litres, ophours, stopdrive;
    CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
    NumberFormat format = NumberFormat.getCurrencyInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mile = (EditText) findViewById(R.id.mile);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(button1onClickListener);
        button2.setOnClickListener(button2OnClickListener);
        tv = (TextView) findViewById(R.id.cost);
        tv2 = (TextView) findViewById(R.id.cpm);
        tv3 = (TextView) findViewById(R.id.gallons);
        diesel = (EditText) findViewById(R.id.diesel);
    }

    private OnClickListener button1onClickListener = new OnClickListener() {
        public void onClick(final View v) {

            x = Double.parseDouble(mile.getText().toString());
            y = Double.parseDouble(diesel.getText().toString());
            if (checkBox2.isChecked()) {
                x = x * 2;
            }

            if (checkBox1.isChecked()) {
                x = x * 0.62137;
            }

            ophours = 0;
            ophours = (x / 55) + 2;

            if (ophours >= 11) {
                stopdrive = (ophours / 10) - 1;
            }
            if (stopdrive > 1) {
                ophours = ophours + (stopdrive * 10);
            }

            gallon = x / 5.5;

            if (checkBox4.isChecked()) {
                gallon = gallon + (ophours * 1.1);
            }

            if (checkBox3.isChecked()) {
                litres = gallon * 3.785;
                tv3.setText(new DecimalFormat("####.##").format(litres) + "L");
            }

            z = (gallon * y) + (x * 0.655);
            costper = z / x;
            tv.setText(format.format(z));
            tv2.setText(format.format(costper) + "/mile");
            tv3.setText(new DecimalFormat("####.##").format(gallon) + "gal.");
        }

    };

    private OnClickListener button2OnClickListener = new OnClickListener() {
        public void onClick(final View v) {

            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://www.ratenroll.com"));
            startActivity(browserIntent);
        }
    };
};

我没有更改您的 xml 文件。

祝你好运。

于 2012-11-06T22:37:39.270 回答