解决了抱歉问题!!!
忘了在对话框中指出编辑文字:
number1 = (EditText)dialogcalc.findViewById(R.id.number1);
number2 = (EditText)dialogcalc.findViewById(R.id.number2);
我正在尝试制作一个与用户相乘的计算器。 该数字可以放在对话框中的Edittext中。 在按下对话框上的按钮计数后,该数字必须乘以并显示在激活对话框的活动中。 当我点击计算按钮时出错,我做错了什么?
main.xml(对话框)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- number 1 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="@+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<!-- number 2 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="number:" />
<EditText
android:id="@+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
android:layout_marginLeft="20dp" />
</LinearLayout>
<Button
android:id="@+id/btncalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"/>
<Button
android:id="@+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
</LinearLayout>
java文件:
package com.tip.calc;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class OverviewpageActivity extends Activity {
private TextView multiplydisplay;
private TextView multiply2display;
private Button btntocalculator;
private Button btnpopup;
private EditText number1;
private EditText number2;
private double number1calc = 0;
private double number2calc = 0;
private double multiply = 0;
private double multiply2 = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
multiplydisplay = (TextView)findViewById(R.id.multiplydisplay);
multiply2display = (TextView)findViewById(R.id.multiplydisplay2);
btntocalculator = (Button)findViewById(R.id.btntocalculator);
btnpopup = (Button)findViewById(R.id.btnpopup);
btntocalculator.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
final Dialog dialogcalc = new Dialog(OverviewpageActivity.this);
dialogcalc.setContentView(R.layout.main);
dialogcalc.setTitle("calc");
dialogcalc.setCancelable(true);
Button buttoncalculate = (Button) dialogcalc.findViewById(R.id.btncalculate);
buttoncalculate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(number1.getText().toString().trim().length() < 1 ){number1calc=0;}
else{number1calc=Double.parseDouble(number1.getText().toString());}
if(number2.getText().toString().trim().length() < 1 ){number2calc=0;}
else{number2calc=Double.parseDouble(number2.getText().toString());}
//calculate
multiply=(number1calc*number2calc)*0.222;
multiply2=(number1calc*number2calc)*0.888;
//display
multiplydisplay.setText(Double.toString(multiply));
multiply2display.setText(Double.toString(multiply2));
dialogcalc.dismiss();
}
});
dialogcalc.show();
}
});
btnpopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//set up dialog
final Dialog dialog = new Dialog(OverviewpageActivity.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.about);
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.carbon);
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
答案 0 :(得分:12)
就像
一样简单AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
答案 1 :(得分:0)
我认为这是你的问题:
number1.getText().toString().trim().length()
你永远不会实例化number1。它是空的。我很确定这会给你造成一个NullPointer。
答案 2 :(得分:0)
修复了NumberFormatExceptions和一些NPE的OverviewpageActivity。 它现在有效。我没有使用你的布局。只是手工完成。
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class OverviewpageActivity extends Activity {
private TextView multiplydisplay;
private TextView multiply2display;
private Button btntocalculator;
private EditText number1;
private EditText number2;
private double number1calc = 0;
private double number2calc = 0;
private double multiply = 0;
private double multiply2 = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout rl = new LinearLayout (this);
rl.setOrientation(1);
multiplydisplay = new TextView(this);
multiply2display = new TextView(this);
btntocalculator = new Button(this);
btntocalculator.setText("calc");
rl.addView(multiplydisplay);
rl.addView(multiply2display);
rl.addView(btntocalculator);
btntocalculator.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
final Dialog dialogcalc = new Dialog(OverActivity.this);
//dialogcalc.setContentView(R.layout.main);
LinearLayout rl = new LinearLayout (OverActivity.this);
rl.setOrientation(1);
number1 = new EditText(OverActivity.this);
number2 = new EditText(OverActivity.this);
number1.setWidth(50);
number2.setWidth(50);
rl.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
rl.addView(number1);
rl.addView(number2);
dialogcalc.setTitle("calc");
dialogcalc.setCancelable(true);
Button buttoncalculate = new Button(OverActivity.this);
buttoncalculate.setText("do calc");
rl.addView(buttoncalculate);
dialogcalc.setContentView(rl);
buttoncalculate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try{
if(number1.getText().toString().trim().length() < 1 ){number1calc=0;}
else{number1calc=Double.parseDouble(number1.getText().toString());}
if(number2.getText().toString().trim().length() < 1 ){number2calc=0;}
else{number2calc=Double.parseDouble(number2.getText().toString());}
}catch (NumberFormatException nfe)
{
nfe.printStackTrace();
}
//calculate
multiply=(number1calc*number2calc)*0.222;
multiply2=(number1calc*number2calc)*0.888;
//display
multiplydisplay.setText(Double.toString(multiply));
multiply2display.setText(Double.toString(multiply2));
dialogcalc.dismiss();
}
});
dialogcalc.show();
}
});
setContentView(rl);
}
}