如何修复android中的ArithmeticException错误 [英] how to fix ArithmeticException error in android

查看:20
本文介绍了如何修复android中的ArithmeticException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我也不知道问题出在哪里,我觉得应该是在创建TableLayout.我不知道如何修复错误并使应用程序正常工作.此程序显示错误java.lang.ArithmeticException : 除以零"错误该程序具有 EditText(edText),它要求用户输入.它的类型是 NUMBER.(这里 r 是来自 edText 的值)在 EditText 下方有一个 Button(bt) 显示 edText 值.在按钮 (bt) 下方还有另一个 Button(bt1),它再次显示 edText 值.此按钮还必须显示 r 行 4 列 EditText 视图,即用户必须在 EditText 中输入 r*4 输入数据.我无法清除错误,也不知道如何清除该错误.

Actually i dont know where the problem is.I think that it must be in creating TableLayout. I dont know how to fix the error and make the app to work. This program shows the error "java.lang.ArithmeticException : divide by zero" error The program has EditText(edText) which asks for input from user. Its type is NUMBER.(here r is the value from edText) Below the EditText there is a Button(bt) which displays the edText value. Below the button (bt) there is another Button(bt1) which again displays the edText Value. This button must also displays r rows and 4 columns of EditText views i.e, user must enter r*4 input data in EditText. I cant clear the error and i dont know how to clear that error.

public class Ybus_Activity extends Activity {
    int i;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ybus);
        final LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        final LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
        TextView getData=new TextView(this);
        getData.setText("Enter the number of LineData : ");
        getData.setId(5);
        getData.setLayoutParams(params);
        main.addView(getData);
        final EditText edText = new EditText(this);
        edText.setId(3);
        edText.setLayoutParams(params);
        edText.setWidth(100);
        edText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        edText.setInputType(InputType.TYPE_CLASS_NUMBER);
        edText.setKeyListener(DigitsKeyListener.getInstance());
        edText.setMaxLines(1);
        main.addView(edText );
        Button bt = new Button(this);
        bt.setText("Click to enter Linedata");
        bt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        main.addView(bt);
        final TextView text = new TextView(this);
        bt.setOnClickListener(new View.OnClickListener() 
        { 
            public void onClick(View v) 
            {
                String ed=edText.getText().toString(); 

                try{
                    i =Integer.parseInt(ed);
                    //setting value here
                    text.setText(i+"");
                    //or you can do like this
                    //text.setText(String.valueOf(i));
                }catch(NumberFormatException ex){
                    text.setText("Value at TextView is not a valid integer");
                }
            }
        });
        main.addView(text);
        final TextView text2 = new TextView(this);
        Button two = new Button(this);
        two.setText("Second");
        main.addView(two);
        main.addView(text2);
        two.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                text2.setText(String.valueOf(i));
                main.addView(createTL(i,getBaseContext()));
                System.out.println(+i);
            }

        });
    }
    public static TableLayout createTL(int r, Context context)
    {
        int c=4;
        LayoutParams params = new 

                LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        TableLayout tl = new TableLayout(context);
        tl.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
        tl.setStretchAllColumns(true);
        TableRow[] tr = new TableRow[r];
        EditText[][] et = new EditText[r][c];
        for(int i=0;i<r;i++)
        {
            System.out.println("line i called" + i);
            tr[i] = new TableRow(context);
            for(int j=0;j<4;j++)
            {
                et[i][j] = new EditText(context);
                et[i][j].setText("o.oo");
                et[i][j].setLayoutParams(params);
                et[i][j].setWidth(100);
                et[i][j].setImeOptions(EditorInfo.IME_ACTION_NEXT);
                et[i][j].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                et[i][j].setKeyListener(DigitsKeyListener.getInstance());
                et[i][j].setMaxLines(1);
                System.out.println("EditText is created" + i  + j);
                tr[i].addView(et[i][j]);
                System.out.println("Tr is created" + i);
            }
            tl.addView(tr[i]);
            System.out.println("TLL is created" + i);
        }
        return tl;
    }
}

我的日志:

02-23 12:56:36.147: I/System.out(1742): line i called0
02-23 12:56:36.155: I/System.out(1742): EditText is created00
02-23 12:56:36.155: I/System.out(1742): Tr is created0
02-23 12:56:36.186: I/System.out(1742): EditText is created01
02-23 12:56:36.186: I/System.out(1742): Tr is created0
02-23 12:56:36.195: I/System.out(1742): EditText is created02
02-23 12:56:36.206: I/System.out(1742): Tr is created0
02-23 12:56:36.206: I/System.out(1742): EditText is created03
02-23 12:56:36.215: I/System.out(1742): Tr is created0
02-23 12:56:36.225: I/System.out(1742): TLL is created0
02-23 12:56:36.225: I/System.out(1742): line i called1
02-23 12:56:36.235: I/System.out(1742): EditText is created10
02-23 12:56:36.235: I/System.out(1742): Tr is created1
02-23 12:56:36.245: I/System.out(1742): EditText is created11
02-23 12:56:36.245: I/System.out(1742): Tr is created1
02-23 12:56:36.265: I/System.out(1742): EditText is created12
02-23 12:56:36.265: I/System.out(1742): Tr is created1
02-23 12:56:36.275: I/System.out(1742): EditText is created13
02-23 12:56:36.275: I/System.out(1742): Tr is created1
02-23 12:56:36.275: I/System.out(1742): TLL is created1
02-23 12:56:36.285: I/System.out(1742): 2
02-23 12:56:36.285: D/AndroidRuntime(1742): Shutting down VM
02-23 12:56:36.285: W/dalvikvm(1742): threadid=1: thread exiting with uncaught exception (group=0xb2f97288)
02-23 12:56:36.305: E/AndroidRuntime(1742): FATAL EXCEPTION: main
02-23 12:56:36.305: E/AndroidRuntime(1742): java.lang.ArithmeticException: divide by zero
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:583)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:572)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.TableLayout.measureVertical(TableLayout.java:470)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.TableLayout.onMeasure(TableLayout.java:435)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.View.measure(View.java:15172)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.View.measure(View.java:15172)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.View.measure(View.java:15172)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.View.measure(View.java:15172)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.View.measure(View.java:15172)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.Choreographer.doFrame(Choreographer.java:525)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.os.Handler.handleCallback(Handler.java:615)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.os.Looper.loop(Looper.java:137)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at java.lang.reflect.Method.invoke(Method.java:511)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 12:56:36.305: E/AndroidRuntime(1742):     at dalvik.system.NativeStart.main(Native Method)
02-23 12:56:36.425: D/dalvikvm(1742): GC_CONCURRENT freed 162K, 3% free 11001K/11271K, paused 24ms+27ms, total 107ms

推荐答案

这行代码导致了错误.这是我参考这个link

This line of code is causing the error. This is what I understood after referring to this link

int c=4;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); //this is causing the error
tl.setStretchAllColumns(true);

你需要把它改成

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

这篇关于如何修复android中的ArithmeticException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆