Android空指针onClick

时间:2013-12-03 10:26:37

标签: android onclick nullpointerexception

所以我正在创建一个2x2和3x3矩阵求解器。在测试3x3矩阵时,我遇到了空指针错误。我试图弄清楚导致错误的原因但是没有任何帮助会非常感激。

错误

12-03 10:08:37.363: E/AndroidRuntime(1467): FATAL EXCEPTION: main
12-03 10:08:37.363: E/AndroidRuntime(1467): java.lang.NullPointerException
12-03 10:08:37.363: E/AndroidRuntime(1467):     at com.uzusoft.matrixmaster.M15$1.onClick(M15.java:86)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at android.view.View.performClick(View.java:4240)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at android.view.View$PerformClick.run(View.java:17721)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at android.os.Handler.handleCallback(Handler.java:730)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at android.os.Looper.loop(Looper.java:137)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at java.lang.reflect.Method.invoke(Method.java:525)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 10:08:37.363: E/AndroidRuntime(1467):     at dalvik.system.NativeStart.main(Native Method)

这是M15.java的代码:

public class M15 extends Activity {
    EditText a1text;
    EditText b1text;
    EditText c1text;
    EditText d1text;
    EditText e1text;
    EditText f1text;
    EditText g1text;
    EditText h1text;
    EditText i1text;
    TextView aR;
    TextView bR;
    TextView cR;
    TextView dR;
    TextView eR;
    TextView fR;
    TextView gR;
    TextView hR;
    TextView iR;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.matrix15); 

        a1text = (EditText) findViewById(R.id.EditText01);
        b1text = (EditText) findViewById(R.id.EditText02);
        c1text = (EditText) findViewById(R.id.EditText03);
        d1text = (EditText) findViewById(R.id.EditText04);
        e1text = (EditText) findViewById(R.id.EditText05);
        f1text = (EditText) findViewById(R.id.EditText06);
        g1text = (EditText) findViewById(R.id.EditText07);
        h1text = (EditText) findViewById(R.id.EditText08);
        i1text = (EditText) findViewById(R.id.EditText09);

        aR = (TextView) findViewById(R.id.textView1);
        bR = (TextView) findViewById(R.id.textView2);
        cR = (TextView) findViewById(R.id.textView3);
        dR = (TextView) findViewById(R.id.textView4);
        eR = (TextView) findViewById(R.id.textView5);
        hR = (TextView) findViewById(R.id.textView6);
        gR = (TextView) findViewById(R.id.textView7);
        hR = (TextView) findViewById(R.id.textView8);
        iR = (TextView) findViewById(R.id.textView9);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            float a = Float.parseFloat(a1text.getText().toString());
            float b = Float.parseFloat(b1text.getText().toString());
            float c = Float.parseFloat(c1text.getText().toString());
            float d = Float.parseFloat(d1text.getText().toString());
            float e = Float.parseFloat(e1text.getText().toString());
            float f = Float.parseFloat(f1text.getText().toString());
            float g = Float.parseFloat(g1text.getText().toString());
            float h = Float.parseFloat(h1text.getText().toString());
            float i = Float.parseFloat(i1text.getText().toString());

            aR.setText(String.valueOf( -1*a ));
            bR.setText(String.valueOf( -1*b ));
            cR.setText(String.valueOf( -1*c ));
            dR.setText(String.valueOf( -1*d ));
            eR.setText(String.valueOf( -1*e ));
            fR.setText(String.valueOf( -1*f ));
            gR.setText(String.valueOf( -1*g ));
            hR.setText(String.valueOf( -1*h ));
            iR.setText(String.valueOf( -1*i ));
            }
        });
    }
}

好的,这已经解决了

5 个答案:

答案 0 :(得分:1)

我认为你忘了初始化fR

您正在访问fR.setText(String.valueOf( -1*f ));

答案 1 :(得分:0)

尝试aR.setText("" + String.valueOf( -1*a ));

答案 2 :(得分:0)

试试这个..你忘了初始化 foR

fR = (TextView) findViewById(R.id.textView6);

答案 3 :(得分:0)

你有两次TextView初始化忘记初始化fR

    hR = (TextView) findViewById(R.id.textView6);
    gR = (TextView) findViewById(R.id.textView7);
    hR = (TextView) findViewById(R.id.textView8);

初始化fR TextView

答案 4 :(得分:0)

您忘记初始化fR。 替换fR =(TextView)findViewById(R.id.textView6);用hR =(TextView)findViewById(R.id.textView6);这是紧接着eR。