当我点击提交时,我的应用程序崩溃了Button
,看看 qf.xml 和 Math.java ,因为那是它崩溃的地方,下面的文本来自logcat
(从应用程序的初始化直到它崩溃):
05-20 12:08:11.671: D/libEGL(22022): 加载 /system/lib/egl/libGLES_android.so 05-20 12:08:11.686: D/libEGL(22022): 加载 /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 05-20 12:08:11.694: D/libEGL(22022): 加载 /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 05-20 12:08:11.694: D/libEGL(22022): 加载 /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 05-20 12:08:11.866: D/OpenGLRenderer(22022): 启用调试模式 0 05-20 12:08:16.514:D/OpenGLRenderer(22022):刷新缓存(模式 0) 05-20 12:08:22.335: D/AndroidRuntime(22022): 关闭虚拟机 05-20 12:08:22.335: W/dalvikvm(22022): threadid=1: 线程以未捕获的异常退出 (group=0x40a421f8) 05-20 12:08:22.342: E/AndroidRuntime(22022): 致命异常: main 05-20 12:08:22.342: E/AndroidRuntime(22022): java.lang.NullPointerException 05-20 12:08:22.342: E/AndroidRuntime(22022): at com.wael.test.Math$1$1.onClick(Math.java:85) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 android.view.View.performClick(View.java:3511) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 android.view.View$PerformClick.run(View.java:14105) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 android.os.Handler.handleCallback(Handler.java:605) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 android.os.Handler.dispatchMessage(Handler.java:92) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 android.os.Looper.loop(Looper.java:137) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 android.app.ActivityThread.main(ActivityThread.java:4424) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 java.lang.reflect.Method.invoke(Method.java:511) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-20 12:08:22.342: E/AndroidRuntime(22022): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-20 12:08:22.342: E/AndroidRuntime(22022): at dalvik.system.NativeStart.main(Native Method) 05-20 12:08:23.913: I/Process(22022): 发送信号。PID:22022 SIG:9
这是 Math.java 的代码:
package com.wael.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.wael.first.app.R;
public class Math extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.math);
final Button button1 = (Button) findViewById(R.id.lf);
final Button button2 = (Button) findViewById(R.id.qf);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
setContentView(R.layout.qf);
final Button submit1 = (Button) findViewById(R.id.qfButton);
final EditText ix = (EditText) findViewById(R.id.lfET1);
final EditText ia = (EditText) findViewById(R.id.lfET2);
final EditText ib = (EditText) findViewById(R.id.lfET3);
final TextView result = (TextView) findViewById(R.id.tvResultQf);
// setting EditText input type
ix.setInputType(InputType.TYPE_CLASS_NUMBER);
ia.setInputType(InputType.TYPE_CLASS_NUMBER);
ib.setInputType(InputType.TYPE_CLASS_NUMBER);
submit1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean TF = true;
String sx = String.valueOf(ix);
String sa = String.valueOf(ia);
String sb = String.valueOf(ib);
if (sx.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in x";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (sa.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in a";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (sb.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in b";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (TF == true) {
double x = Double.parseDouble(ix.getText()
.toString());
double a = Double.parseDouble(ia.getText()
.toString());
double b = Double.parseDouble(ib.getText()
.toString());
double y = a * x + b;
String sy = String.valueOf(y);
result.setText(sy);
}
}
});
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
setContentView(R.layout.qf);
final Button submit2 = (Button) findViewById(R.id.qfButton);
final EditText ix = (EditText) findViewById(R.id.qfET1);
final EditText ia = (EditText) findViewById(R.id.qfET2);
final EditText ib = (EditText) findViewById(R.id.qfET3);
final EditText ic = (EditText) findViewById(R.id.qfET4);
final TextView result = (TextView) findViewById(R.id.tvResultLf);
// setting EditText input type
ix.setInputType(InputType.TYPE_CLASS_NUMBER);
ia.setInputType(InputType.TYPE_CLASS_NUMBER);
ib.setInputType(InputType.TYPE_CLASS_NUMBER);
ic.setInputType(InputType.TYPE_CLASS_NUMBER);
submit2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean TF = true;
String sx = String.valueOf(ix);
String sa = String.valueOf(ia);
String sb = String.valueOf(ib);
String sc = String.valueOf(ic);
if (sx.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in x";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (sa.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in a";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (sb.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in b";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (sc.matches("")) {
TF = false;
Context context = getApplicationContext();
CharSequence text = "Enter a value in c";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text,
duration);
toast.show();
}
if (TF == true) {
double x = Double.parseDouble(ix.getText()
.toString());
double a = Double.parseDouble(ia.getText()
.toString());
double b = Double.parseDouble(ib.getText()
.toString());
double c = Double.parseDouble(ic.getText()
.toString());
double y=a*(x*x)+b*x+c;
String sy = String.valueOf(y);
result.setText(sy);
}
}
});
}
});
}
}
这是 qf.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lf"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Space
android:layout_width="fill_parent"
android:layout_height="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter_all"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF0000" />
<Space
android:layout_width="fill_parent"
android:layout_height="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/x"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/lfET1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="true"
android:ems="10"
android:hint="@string/x"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/lfET2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="true"
android:ems="10"
android:hint="@string/b"
android:inputType="number" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/lfET3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="true"
android:ems="10"
android:hint="@string/a"
android:inputType="number" />
<Space
android:layout_width="fill_parent"
android:layout_height="20dp" />
<Button
android:id="@+id/qfButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sub" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/y"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvResultLf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>