简体   繁体   English

Android空指针onClick

[英]Android null pointer onClick

So I am creating a 2x2 and 3x3 matrix solver. 所以我要创建一个2x2和3x3矩阵求解器。 When testing the 3x3 matrices I encountered a null pointer error. 在测试3x3矩阵时,我遇到了空指针错误。 I tried to figure out what is causing the error but couldn't any help would be really appreciated. 我试图找出是什么原因导致了错误,但无法提供任何帮助将不胜感激。

The error 错误

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)

and this is the code for M15.java: 这是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 ));
            }
        });
    }
}

Ok this was solved 好了,这解决了

I think you have forgotten to initialize fR 我认为您忘记了初始化fR

And you are accessing it fR.setText(String.valueOf( -1*f )); 您正在访问它fR.setText(String.valueOf( -1*f ));

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

试试这个..您忘记初始化fR

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

You had init two times this TextView and forgot to init fR 您已经两次初始化了此TextView ,却忘记了初始化fR

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

Init fR TextView 初始化fR TextView

You forgot to initialize fR. 您忘记初始化fR。 Replace fR = (TextView) findViewById(R.id.textView6); 替换fR =(TextView)findViewById(R.id.textView6); with hR = (TextView) findViewById(R.id.textView6); 与hR =(TextView)findViewById(R.id.textView6); which is immediately after eR. 紧接eR之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM