gpt4 book ai didi

java 运行时异常 :java. lang.nullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:22:34 24 4
gpt4 key购买 nike

我是android新手,陷入了空指针异常。我什至尝试过防御性编码,但仍然显示错误。

这是我的代码:

public class MainActivity extends Activity {

private ArrayList<SubjectType> subjects;
private ArrayList<Days> days;
private ExpandableListAdapter adapter;
private ExpandableListView exv;
private String DayNames[] = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
private String TypeName[]= {"Lecture","Practical","Tutorial"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadContent();

exv = (ExpandableListView) findViewById(R.id.expandableListView1);
adapter = new ExpandableListAdapter(MainActivity.this, subjects);
exv.setAdapter(adapter);
collapseAll();


}

private void collapseAll() {
for(int i=0;i<3;i++)
{
exv.collapseGroup(i);
}
}

private void loadContent() {

int daySize,typeSize;
Days day_1 = null;
boolean False = false;
SubjectType sub1=null;
daySize = 5;
typeSize = 3;

for(int i=0;i<daySize;i++)
{
if(DayNames[i]!=null)
{
Days day_12 = day_1;
day_12.setDayAttributes(DayNames[i], False);
days.add(day_1);
}
}

for(int j=0;j<typeSize;j++)
{
SubjectType sub12 = sub1;
sub12.setChecked(false);
if(days!=null&&TypeName[j]!=null&&sub12!=null&&sub12!=null)
{
sub12.setDays(days);
sub12.setSubjectTypeName(TypeName[j]);
subjects.add(sub12);
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

和 log-cat 显示:

07-05 17:14:26.942: E/AndroidRuntime(991): FATAL EXCEPTION: main
07-05 17:14:26.942: E/AndroidRuntime(991): java.lang.RuntimeException: Unable to start activity ComponentInfo{trying.expandables/trying.expandables.MainActivity}: java.lang.NullPointerException
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.os.Looper.loop(Looper.java:137)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 17:14:26.942: E/AndroidRuntime(991): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:14:26.942: E/AndroidRuntime(991): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 17:14:26.942: E/AndroidRuntime(991): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 17:14:26.942: E/AndroidRuntime(991): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 17:14:26.942: E/AndroidRuntime(991): at dalvik.system.NativeStart.main(Native Method)
07-05 17:14:26.942: E/AndroidRuntime(991): Caused by: java.lang.NullPointerException
07-05 17:14:26.942: E/AndroidRuntime(991): at trying.expandables.MainActivity.loadContent(MainActivity.java:54)
07-05 17:14:26.942: E/AndroidRuntime(991): at trying.expandables.MainActivity.onCreate(MainActivity.java:23)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.Activity.performCreate(Activity.java:5104)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 17:14:26.942: E/AndroidRuntime(991): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 17:14:26.942: E/AndroidRuntime(991): ... 11 more

请帮忙!

最佳答案

onCreate 在例程的第 3 行调用 loadContent

在您调用的loadContent例程中:

days.add(day_1);

在第一个 for 循环中。声明:

private ArrayList<Days> days;

...从未实例化。在尝试使用它之前将其放在某处:

days = new ArrayList<Days>();

编辑:

正如其他人所说。对主题执行相同的操作

关于java 运行时异常 :java. lang.nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17494155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com