I'm struggeling adding some content for a scroll view dynamically. I would apreaciate if someone could give me an explanation, why my app crashes.
public class LessonView extends AppCompatActivity {
int maxWidth = 450;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// old
ScrollView SView = new ScrollView(this);
// new
// LinearLayout activityLL = (LinearLayout) findViewById(R.id.myLL);
RelativeLayout.LayoutParams centerTableParameters = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
);
centerTableParameters.addRule(RelativeLayout.CENTER_IN_PARENT);
LinearLayout llayout = new LinearLayout(this);
llayout.setOrientation(LinearLayout.VERTICAL);
llayout.setGravity(Gravity.CENTER);
Resources res = getResources();
Intent in = getIntent();
int index = in.getIntExtra("com.example.ITEM_INDEX", -1);
if(true){
// create tables:
boolean tablesEnd = false;
boolean lineEnd = false;
int j = 0;
int i = 0;
while(i < cn.length){
TableLayout tableLayout = new TableLayout(this);
TableRow tableRow1 = new TableRow(this);
TableRow tableRow2 = new TableRow(this);
TableRow tableRow3 = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
tableLayout.setLayoutParams(lp);
// create lines:
lineEnd = false;
while(i < cn.length && lineEnd == false){
[...]
}
tableLayout.addView(tableRow1);
tableLayout.addView(tableRow2);
tableLayout.addView(tableRow3);
llayout.addView(tableLayout, centerTableParameters);
Space space = new Space(this);
space.setLayoutParams(new LinearLayout.LayoutParams(10, 20));
llayout.addView(space);
}
// old:
SView.addView(llayout);
setContentView(SView);
// new:
//activityLL.addView(llayout);
//setContentView(R.layout.activity_lesson_view);
}
}
if I run it with the "old" Code (see the lines below the comment "old") it runs fine without errors. The old code doesn't use any layout file. I would like to add some more functions to the layout ;) Once I start the activity with the "new" lines (old lines as comments), the app crashes with the error message:
2019-10-26 23:51:42.694 4599-4599/com.example.MyApp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.MyApp, PID: 4599 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyApp/com.example.MyApp.LessonView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference at com.example.MyApp.LessonView.onCreate(LessonView.java:242) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
The error seems to occur at "activityLL.addView(llayout);" (line 242) but I have no explanation why...