-1

当我点击提交时,我的应用程序崩溃了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>    
4

2 回答 2

2

ANullPointerException表示您试图访问尚未初始化的内容。例如,如果您执行以下操作:

Button myButton = null;
myButton.setText("some text");

它在语法上是正确的,但myButton在 UI 上没有引用任何内容,因此尝试更改未初始化对象的属性会使您的应用程序崩溃。由于我还看不到您的代码,因此这只是可能导致该异常的示例。

从关于NullPointerException

当程序尝试访问对象的字段或方法或数组的元素时,如果没有实例或数组可供使用,即对象或数组指向 null 时抛出。它也发生在其他一些不太明显的情况下,例如 Throwable 引用为空的 throw e 语句。

于 2012-05-20T09:36:22.087 回答
0

编辑:OnClickListener第一个Button button1中,您搜索带有 id 的TextView结果),tvResultQf但您的R.layout.qf布局中没有一个(您有一个带有 id 的tvResultLf,可能是错字?),当您尝试使用它时设置 text( result.setText(sy);) 它会抛出NullPointerException.

同样在OnCLickListenerbutton2Button ,您搜索EditText带有 id的 an,qfET4R.layout.qf布局中没有,因此当您尝试设置 inputType( ic.setInputType(InputType.TYPE_CLASS_NUMBER); ) 时,它会再次抛出 a NullPointerException

可能还有其他错误,这只是快速浏览。


您没有指出哪一行85(引发异常),但我猜您的TextView 结果null当您尝试在该行上设置文本时:

result.setText(sy);// the last line in your code

如果这是行85,则检查布局R.layout.qf并查看是否有TextViewid tvResultLf。如果你有它,那么也尝试通过转到菜单Project->来清理项目Clean

如果这不起作用,请发布R.layout.qf文件的布局。

于 2012-05-20T17:21:44.167 回答