创建动态textViews

时间:2012-10-15 07:19:30

标签: android textview android-linearlayout

我正在尝试在android中创建一系列数字,如pascal triangle。我必须在android中执行。我有动态创建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);


            }

程序在打印内部行语句后通过给出空指针异常来崩溃。我在很多方面试过创建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)

1 个答案:

答案 0 :(得分:0)

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