我是 android 新手,遇到了 null Pointer 的异常。我什至尝试过防御性编码,但它仍然显示错误。
这是我的代码:
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;
}
}
和日志猫显示:
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
请帮忙!