public class Main extends Activity {
float fm_value_pdty;
Button calc_fm_pdty, dt_pdty_calc, loader_pdty_calc;
TextView fm_value_pdty_txt;
EditText DT_count, Loader_count;
public EditText DT_Pdty;
public EditText Loader_Pdty;
public static float vdtpdty;
public static float vloaderpdty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm_value_pdty=(float) 0;
calc_fm_pdty = (Button) findViewById(R.id.calcpdty);
dt_pdty_calc = (Button) findViewById(R.id.calcpdty_dt);
loader_pdty_calc = (Button) findViewById(R.id.calcpdty_loader);
fm_value_pdty_txt = (TextView) findViewById(R.id.fm_pdty_value);
DT_Pdty = (EditText) findViewById(R.id.DTPdty_ET);
Loader_Pdty = (EditText) findViewById(R.id.Loader_CT_ET);
vdtpdty=(float) 0.0;
vloaderpdty=(float) 0.0;
calc_fm_pdty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//if (DT_CT.getText().toString() <>""){
if ((DT_Pdty.getText().toString()!=null) && (Loader_Pdty.getText().toString()!=null)) {
fm_value_pdty = ((float) Integer.parseInt(DT_Pdty.getText().toString()))/((float) Integer.parseInt(Loader_Pdty.getText().toString()));
fm_value_pdty_txt.setText("Fleet Matching value is " + fm_value_pdty);
}
else if ((DT_Pdty.getText().toString()==null) || (Loader_Pdty.getText().toString()==null)) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(getApplicationContext());
dlgAlert.setMessage("Please input the DT Productivty and Loader Productivty");
dlgAlert.setTitle("Data is empty");
dlgAlert.setPositiveButton("OK",null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
}
});
dt_pdty_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dt_pdty.vloaderpdty=vloaderpdty;
DT_Pdty.setText("");
Loader_Pdty.setText("");
Intent pdty = new Intent(Main.this, dt_pdty.class);
startActivity(pdty);
DT_Pdty.setText(Float.toString(vdtpdty));
Loader_Pdty.setText(Float.toString(vloaderpdty));
}
});
loader_pdty_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v2) {
ld_pdty.vdtpdty=vdtpdty;
DT_Pdty.setText("");
Loader_Pdty.setText("");
Intent pdty = new Intent(Main.this, ld_pdty.class);
startActivity(pdty);
DT_Pdty.setText(Float.toString(vdtpdty));
Loader_Pdty.setText(Float.toString(vloaderpdty));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
当我尝试使用按钮激活活动时,代码很简单,首先清空编辑文本并且活动开始,活动结束后,已清空的编辑文本将填充活动中的计算值。 我想制作一个有两个编辑文本和两个按钮的程序。第一个编辑文本将填充第一个按钮的结果,与其他编辑文本和按钮相同。我得到第一个按钮的结果的问题但是当我从第二个按钮运行第二个活动时我得到一个错误。从logcat开始,这是因为第一次编辑文本的视图。我已经用logcat更新了我的帖子。
08-20 19:52:47.924: D/AndroidRuntime(237): Shutting down VM
08-20 19:52:47.958: W/dalvikvm(237): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-20 19:52:47.958: E/AndroidRuntime(237): Uncaught handler: thread main exiting due to uncaught exception
08-20 19:52:48.028: E/AndroidRuntime(237): java.lang.NullPointerException
08-20 19:52:48.028: E/AndroidRuntime(237): at com.example.fleetmatcher.Main$2.onClick(Main.java:66)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.View.performClick(View.java:2364)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.View.onTouchEvent(View.java:4179)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.View.dispatchTouchEvent(View.java:3709)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-20 19:52:48.028: E/AndroidRuntime(237): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-20 19:52:48.028: E/AndroidRuntime(237): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-20 19:52:48.028: E/AndroidRuntime(237): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.os.Looper.loop(Looper.java:123)
08-20 19:52:48.028: E/AndroidRuntime(237): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-20 19:52:48.028: E/AndroidRuntime(237): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 19:52:48.028: E/AndroidRuntime(237): at java.lang.reflect.Method.invoke(Method.java:521)
08-20 19:52:48.028: E/AndroidRuntime(237): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-20 19:52:48.028: E/AndroidRuntime(237): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-20 19:52:48.028: E/AndroidRuntime(237): at dalvik.system.NativeStart.main(Native Method)
08-20 19:52:48.201: I/dalvikvm(237): threadid=7: reacting to signal 3
08-20 19:52:48.422: I/dalvikvm(237): Wrote stack trace to '/data/anr/traces.txt'
08-20 19:52:50.620: I/Process(237): Sending signal. PID: 237 SIG: 9
答案 0 :(得分:0)
你为什么这样做
EditText Loader_Pdty; why not EditText loader_Pdty;
并在将字符串添加到settext之前将float转换为字符串
fm_value_pdty = ((float) Integer.parseInt(DT_Pdty.getText().toString()))/((float) Integer.parseInt(Loader_Pdty.getText().toString()));
fm_value_pdty_txt.setText("Fleet Matching value is " + String.valueOf(fm_value_pdty));
现在的预防时间
dt_pdty_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dt_pdty.vloaderpdty=vloaderpdty; ///change the first letter
dT_Pdty.setText(""); // change the firstx letter..
Loader_Pdty.setText("");
Intent pdty = new Intent(Main.this, Dt_pdty.class); //change the first letter
startActivity(pdty);
DT_Pdty.setText(Float.toString(vdtpdty));
Loader_Pdty.setText(Float.toString(vloaderpdty));
}
});
loader_pdty_
尝试..我知道它是否有帮助