Click here to Skip to main content
15,886,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I want to create multiple TextViews in a LinearLayout Programmatically, and for this tried the below code but it gives me the error below.

Please help me to resolve the issue.


XML
2023-03-23 11:50:19.704 9003-9003/com.nyt.kitaab.quranshareef E/MiuiMultiWindowUtils: initFreeFormResolutionArgs failed, device is citrus
2023-03-23 11:50:20.381 9003-9050/com.nyt.kitaab.quranshareef E/ab.quransharee: open libmigui.so failed! dlopen - dlopen failed: library "libmigui.so" not found
2023-03-23 11:50:23.157 9003-9003/com.nyt.kitaab.quranshareef E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nyt.kitaab.quranshareef, PID: 9003
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyt.kitaab.quranshareef/com.nyt.kitaab.quranshareef.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3774)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3941)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:211)
        at android.os.Looper.loop(Looper.java:300)
        at android.app.ActivityThread.main(ActivityThread.java:8322)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
        at com.nyt.kitaab.quranshareef.MainActivity.onCreate(MainActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:8198)
        at android.app.Activity.performCreate(Activity.java:8165)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1342)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3754)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3941) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:211) 
        at android.os.Looper.loop(Looper.java:300) 
        at android.app.ActivityThread.main(ActivityThread.java:8322) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038) 
2023-03-23 11:51:18.893 9571-9571/com.nyt.kitaab.quranshareef E/MiuiMultiWindowUtils: initFreeFormResolutionArgs failed, device is citrus
2023-03-23 11:51:19.446 9571-9605/com.nyt.kitaab.quranshareef E/ab.quransharee: open libmigui.so failed! dlopen - dlopen failed: library "libmigui.so" not found
2023-03-23 11:51:22.320 9571-9571/com.nyt.kitaab.quranshareef E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nyt.kitaab.quranshareef, PID: 9571
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyt.kitaab.quranshareef/com.nyt.kitaab.quranshareef.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3774)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3941)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:211)
        at android.os.Looper.loop(Looper.java:300)
        at android.app.ActivityThread.main(ActivityThread.java:8322)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
        at com.nyt.kitaab.quranshareef.MainActivity.onCreate(MainActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:8198)
        at android.app.Activity.performCreate(Activity.java:8165)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1342)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3754)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3941) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:211) 
        at android.os.Looper.loop(Looper.java:300) 
        at android.app.ActivityThread.main(ActivityThread.java:8322) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038) 


What I have tried:

Java
setContentView(R.layout.activity_main);
        LinearLayout linearLayout = findViewById(R.id.llayout);
        // Add TextView to LinearLayout
        if (linearLayout != null) {
            TextView[] textView = new TextView[0];
            for(int i=0;i<=113;i++) {
        // Create TextView programmatically.
        textView[i].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView[i].setTextColor(Color.WHITE);
        textView[i].setGravity(Gravity.CENTER);

        textView[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Clicked on me", Toast.LENGTH_LONG).show();
            }
        });
                textView[i].setText("Click on me");
                linearLayout.addView(textView[i]);
            }
        }
Posted
Updated 23-Mar-23 8:46am

You create an array reference of TextViews with no entries:
Java
TextView[] textView = new TextView[0];

and you then try to refer to 114 of them, even though they do not exist.
Java
    for(int i=0;i<=113;i++) {
// Create TextView programmatically.
textView[i].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,

You need to create the 114 TextViews in the array before you can refer to them.
Java
TextView[] textView = new TextView[114];
 
Share this answer
 
Changed but still getting errors:

Java
setContentView(R.layout.activity_main);
        LinearLayout linearLayout = findViewById(R.id.llayout);
        // Add TextView to LinearLayout
        if (linearLayout != null) {
            TextView[] textView = new TextView[14];
            for(int i=0;i<=113;i++) {
        // Create TextView programmatically.
        textView[i].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView[i].setTextColor(Color.WHITE);
        textView[i].setGravity(Gravity.CENTER);

        textView[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Clicked on me", Toast.LENGTH_LONG).show();
            }
        });
                textView[i].setText("Click on me");
                linearLayout.addView(textView[i]);
            }
        }


XML
2023-03-23 23:41:50.336 23450-23450/com.nyt.kitaab.quranshareef E/MiuiMultiWindowUtils: initFreeFormResolutionArgs failed, device is citrus
2023-03-23 23:41:50.883 23450-23482/com.nyt.kitaab.quranshareef E/ab.quransharee: open libmigui.so failed! dlopen - dlopen failed: library "libmigui.so" not found
2023-03-23 23:41:53.710 23450-23450/com.nyt.kitaab.quranshareef E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nyt.kitaab.quranshareef, PID: 23450
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyt.kitaab.quranshareef/com.nyt.kitaab.quranshareef.QuranActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3774)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3941)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:211)
        at android.os.Looper.loop(Looper.java:300)
        at android.app.ActivityThread.main(ActivityThread.java:8322)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
        at com.nyt.kitaab.quranshareef.QuranActivity.onCreate(QuranActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:8198)
        at android.app.Activity.performCreate(Activity.java:8165)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1342)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3754)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3941) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:211) 
        at android.os.Looper.loop(Looper.java:300) 
        at android.app.ActivityThread.main(ActivityThread.java:8322) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038) 
 
Share this answer
 
Comments
David Crow 24-Mar-23 8:06am    
TextView[] textView = new TextView[14];
for(int i=0;i<=113;i++) {

Look at these statements very closely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900