當我點擊提交Button
,看看qf.xml和Math.java崩潰的地方,我的應用程序崩潰,下面的文本是從logcat
(從該應用直到它崩潰)的初始化:我的Android應用程序崩潰時,使用onClick回調
05-20 12:08:11.671: D/libEGL(22022): loaded /system/lib/egl/libGLES_android.so 05-20 12:08:11.686: D/libEGL(22022): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 05-20 12:08:11.694: D/libEGL(22022): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 05-20 12:08:11.694: D/libEGL(22022): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 05-20 12:08:11.866: D/OpenGLRenderer(22022): Enabling debug mode 0 05-20 12:08:16.514: D/OpenGLRenderer(22022): Flushing caches (mode 0) 05-20 12:08:22.335: D/AndroidRuntime(22022): Shutting down VM 05-20 12:08:22.335: W/dalvikvm(22022): threadid=1: thread exiting with uncaught exception (group=0x40a421f8) 05-20 12:08:22.342: E/AndroidRuntime(22022): FATAL EXCEPTION: 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): at android.view.View.performClick(View.java:3511) 05-20 12:08:22.342: E/AndroidRuntime(22022): at android.view.View$PerformClick.run(View.java:14105) 05-20 12:08:22.342: E/AndroidRuntime(22022): at android.os.Handler.handleCallback(Handler.java:605) 05-20 12:08:22.342: E/AndroidRuntime(22022): at android.os.Handler.dispatchMessage(Handler.java:92) 05-20 12:08:22.342: E/AndroidRuntime(22022): at android.os.Looper.loop(Looper.java:137) 05-20 12:08:22.342: E/AndroidRuntime(22022): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-20 12:08:22.342: E/AndroidRuntime(22022): at java.lang.reflect.Method.invokeNative(Native Method) 05-20 12:08:22.342: E/AndroidRuntime(22022): at java.lang.reflect.Method.invoke(Method.java:511) 05-20 12:08:22.342: E/AndroidRuntime(22022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-20 12:08:22.342: E/AndroidRuntime(22022): at 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): Sending signal. 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>
請張貼Math.java –
添加的代碼提交的按鈕'onClick'法和點,這是行85 – Luksprog
@Luksprog我已經加入我已經添加了佈局代碼 – wael