0

我正在尝试在 android 中创建一系列数字,例如帕斯卡三角形。我必须在android中执行。我有 tio 动态创建 textViews 并为其分配行和列值。在这里发布我的代码。

    btnShow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            LinearLayout linearLayout=new LinearLayout(getApplicationContext());
            Log.d("outside","for");

            List<TextView> Rows = new ArrayList<TextView>(row);
            List<TextView> OuterColumns= new ArrayList<TextView>(col);
            List<TextView> InnerColumns = new ArrayList<TextView>(col);

            int nor=Integer.parseInt(IEdit.getText().toString());

            for(row=1;row<=nor;row++)
            {
                Log.d("before","text");
                /*LayoutParams lp = new LayoutParams(
                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);*/


                text_rows = new TextView(getApplicationContext());
                text_rows.setText(String.valueOf(row));
                Log.d("Inside","rows");
                Log.i("EditText","Number"+nor);

                for(col=1;col<row-1;col++)
                Log.d("Inside","col");
                text_cols=new TextView(getApplicationContext());
                text_cols.setText(String.valueOf(col));


                for(col=row-1;col>=row;col--)
                text_cols2=new TextView(getApplicationContext());
                text_cols2.setText(String.valueOf(col));


                linearLayout.addView(text_rows);
                linearLayout.addView(text_cols);
                linearLayout.addView(text_cols2);

                Rows.add(text_rows);
                OuterColumns.add(text_cols);
                InnerColumns.add(text_cols2);


            }

程序在打印 inside rows 语句后给出空指针异常而崩溃。我尝试了多种方式来创建 textViews 数组,在 for 循环中独立创建它们并添加它们。但在所有上下文中,我都得到了 nullPointerException。作为android的新手,我无法弄清楚为什么会这样?请帮帮我..提前谢谢

我什至在发布我的 logcat:

10-15 12:57:18.199: W/dalvikvm(1209): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-15 12:57:18.219: E/AndroidRuntime(1209): FATAL EXCEPTION: main
10-15 12:57:18.219: E/AndroidRuntime(1209): java.lang.NullPointerException
10-15 12:57:18.219: E/AndroidRuntime(1209):     at com.example.pyramid_of_numbers.MainActivity$1.onClick(MainActivity.java:66)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at android.view.View.performClick(View.java:2408)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at android.view.View$PerformClick.run(View.java:8816)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at android.os.Handler.handleCallback(Handler.java:587)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at android.os.Looper.loop(Looper.java:123)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at java.lang.reflect.Method.invoke(Method.java:521)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-15 12:57:18.219: E/AndroidRuntime(1209):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

我认为您应该取消注释 LayoutParams 并使用它。

于 2013-12-12T14:28:28.847 回答