Android 动态添加 Scrollview-Content
Android dynamically adding Scrollview-Content
我正在努力为滚动视图动态添加一些内容。如果有人能给我一个解释,为什么我的应用程序崩溃,我会很高兴。
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);
}
}
如果我 运行 使用 "old" 代码(请参阅注释 "old" 下方的行),它 运行 没问题。旧代码不使用任何布局文件。我想在布局中添加更多功能;)一旦我使用 "new" 行(旧行作为注释)启动 activity,应用程序就会崩溃并显示错误消息:
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)
错误似乎发生在 "activityLL.addView(llayout);" (line 242)
但我没有解释为什么...
在使用 LinearLayout activityLL = (LinearLayout) findViewById(R.id.myLL);
检索 LinearLayout 之前,您应该调用 setContentView(R.layout.activity_lesson_view);
我正在努力为滚动视图动态添加一些内容。如果有人能给我一个解释,为什么我的应用程序崩溃,我会很高兴。
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);
}
}
如果我 运行 使用 "old" 代码(请参阅注释 "old" 下方的行),它 运行 没问题。旧代码不使用任何布局文件。我想在布局中添加更多功能;)一旦我使用 "new" 行(旧行作为注释)启动 activity,应用程序就会崩溃并显示错误消息:
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)
错误似乎发生在 "activityLL.addView(llayout);" (line 242)
但我没有解释为什么...
在使用 LinearLayout activityLL = (LinearLayout) findViewById(R.id.myLL);
setContentView(R.layout.activity_lesson_view);