当我点击提交Button
时,我的应用程序崩溃了,看看它崩溃的地方是qf.xml和Math.java,下面的文字来自logcat
(来自初始化)应用程序直到它崩溃):
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>
答案 0 :(得分:2)
NullPointerException
表示您尝试访问尚未初始化的内容。例如,如果您执行以下操作:
Button myButton = null;
myButton.setText("some text");
它在语法上是正确的,但myButton
并未在UI上引用任何内容,因此尝试更改未初始化对象的属性会使应用程序崩溃。由于我还没有看到你的代码,这只是导致该异常的一个例子。
来自NullPointerException
当程序在没有要使用的实例或数组时尝试访问对象的字段或方法或数组的元素时抛出,如果对象或数组指向null ,则 。它也出现在其他一些不太明显的情况下,例如throw Th语句,其中Throwable引用为null。
答案 1 :(得分:0)
修改:
在第一个OnClickListener
按钮1 的Button
中,您搜索标识为{{1}的TextView
(结果)但是你的tvResultQf
布局中没有一个(你有一个标识为R.layout.qf
,可能是拼写错误?)当你尝试使用它来设置文本时{{1} 1}})它将抛出tvResultLf
。
同样在result.setText(sy);
按钮2 的NullPointerException
中,您搜索标识为OnCLickListener
的{{1}},但您不会在Button
布局中有一个,所以当您尝试设置inputType(EditText
)时,它会再次抛出qfET4
。
可能还有其他错误,只是快速查看。
您没有指出哪一行R.layout.qf
(引发异常)但我猜你的ic.setInputType(InputType.TYPE_CLASS_NUMBER);
结果是NullPointerException
时你试着在线上设置文字:
85
如果这是行TextView
,请检查布局null
,看看是否有result.setText(sy);// the last line in your code
,其ID为85
。如果你有它,那么也可以通过转到菜单R.layout.qf
- &gt;来尝试清理项目。 TextView
。
如果这不起作用,则发布tvResultLf
文件的布局。