如果我的代码中存在任何基本错误,请道歉。我是初学者,所以我需要一些额外的帮助。当我尝试在listView中设置子元素的文本时,我得到一个空指针异常错误。下面是我加载listView的类的代码,我在有错误的行前面有注释。
public class MainTimetableDisplay extends Activity {
public static final String CODE = "com.liliana.maintimetabledisplay.code";
public static final String NAME = "com.liliana.maintimetabledisplay.fullname";
public static final String CLASS_TYPE = "com.liliana.maintimetabledisplay.lecturetype";
public static final String LOCATION = "com.liliana.maintimetabledisplay.location";
public static final String WEEKDAY = "com.liliana.maintimetabledisplay.weekday";
public static final String TIME = "com.liliana.maintimetabledisplay.starttime";
public static final String REMINDER = "com.liliana.maintimetabledisplay.endtime";
private List<UCDModule> modulesList = new ArrayList<UCDModule>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_timetable_display);
populateModulesList();
populateListView();
onClickActivityLauncher();
}
private void populateModulesList() {
// TODO Populate this list with data from the DB:
// TODO: delete these dummy values used to test the listView
modulesList.add(new UCDModule("COMP30510", "Android Development", "Practical","Online", "Tuesday", "17:00", "19:00","No reminder"));
modulesList.add(new UCDModule("COMP30510", "Android Development", "Lecture","Online", "Friday", "17:00", "19:00","No reminder"));
modulesList.add(new UCDModule("COMP41090", "SQL Programming", "Lecture","B.106", "Monday", "9:00", "11:00","No reminder"));
modulesList.add(new UCDModule("COMP41090", "SQL Programming", "Practical","B.106", "Wednesday", "9:00", "11:00","No reminder"));
}
private void populateListView() {
// TODO Auto-generated method stub
ArrayAdapter<UCDModule> adapter = new MyArrayAdapter();
ListView listView = (ListView) findViewById(R.id.listView_modulesDisplay);
listView.setAdapter(adapter);
}
private class MyArrayAdapter extends ArrayAdapter<UCDModule> {
public MyArrayAdapter () {
super(MainTimetableDisplay.this, R.layout.module_item, modulesList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
// Set view in case it's null
View elementView = convertView;
if (elementView == null)
{
elementView = getLayoutInflater().inflate(R.layout.module_item,parent,false);
Log.i("INFLATING VIEW","true");
}
// Retrieve current module in the list
UCDModule currentModule = modulesList.get(position);
//Composing the textView's text strings
String code = currentModule.getCode();
String lectureType;
if(currentModule.getLectureType().equals("Lecture")){
lectureType = "L";
}else{
lectureType = "P";
}
// TODO: add this as a value automatically stored in DB upon saving module details
String weekDay = currentModule.getWeekDay();
String short_weekDay = null;
if(weekDay.equals("Monday")){
short_weekDay = "Mon";
} else if(weekDay.equals("Tuesday")) {
short_weekDay = "Tue";
} else if(weekDay.equals("Wednesday")){
short_weekDay = "Wed";
} else if (weekDay.equals("Thursday")) {
short_weekDay = "Thu";
} else if (weekDay.equals("Friday")){
short_weekDay = "Fri";
} else if (weekDay.equals("Saturday")){
short_weekDay = "Sat";
}
String startTime = currentModule.getStartTime();
String location = currentModule.getLocation();
String codeAndType = code + " (" + lectureType + ")" ;
String details = short_weekDay + " " + startTime + " " + location;
// Fill in the view
TextView tv_codeAndType = (TextView)findViewById(R.id.textView_moduleCode_classType);
tv_codeAndType.setText(codeAndType); // NULL POINTER EXCEPTION HERE
TextView tv_details = (TextView)findViewById(R.id.textView_module_details);
tv_details.setText(details);
return elementView;
}
}
private void onClickActivityLauncher() {
// TODO Auto-generated method stub
ListView list = (ListView) findViewById(R.id.listView_modulesDisplay);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id){
UCDModule selectedUCDModule = modulesList.get(position);
String code = selectedUCDModule.getCode();
String fullName = selectedUCDModule.getFullName();
String classType = selectedUCDModule.getLectureType();
String location = selectedUCDModule.getLocation();
String weekDay = selectedUCDModule.getWeekDay();
String startTime = selectedUCDModule.getStartTime();
String endTime = selectedUCDModule.getEndTime();
String time = startTime + " to " + endTime;
String reminder = selectedUCDModule.getReminder();
Intent sender = new Intent(MainTimetableDisplay.this, ShowModule.class);
Bundle extras = new Bundle();
extras.putString(CODE, code);
extras.putString(NAME, fullName);
extras.putString(CLASS_TYPE, classType);
extras.putString(LOCATION, location);
extras.putString(WEEKDAY, weekDay);
extras.putString(TIME, time);
extras.putString(REMINDER, reminder);
sender.putExtras(extras);
startActivity(sender);
}
});
}
public void addModuleButton(View view){
Intent addModuleLaunch = new Intent(MainTimetableDisplay.this,AddModule.class);
startActivity(addModuleLaunch);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_timetable_display_menu, menu);
return true;
}
}
这是我在LogCat中显示的内容:
03-25 15:21:46.703: D/AndroidRuntime(438): Shutting down VM
03-25 15:21:46.703: W/dalvikvm(438): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-25 15:21:46.753: E/AndroidRuntime(438): FATAL EXCEPTION: main
03-25 15:21:46.753: E/AndroidRuntime(438): java.lang.NullPointerException
03-25 15:21:46.753: E/AndroidRuntime(438): at com.liliana.ucdmoduletimetable.MainTimetableDisplay$MyArrayAdapter.getView(MainTimetableDisplay.java:128)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.ListView.makeAndAddView(ListView.java:1727)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.ListView.fillDown(ListView.java:652)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.ListView.fillFromTop(ListView.java:709)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.ListView.layoutChildren(ListView.java:1580)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.View.layout(View.java:7035)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.View.layout(View.java:7035)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.View.layout(View.java:7035)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.View.layout(View.java:7035)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.View.layout(View.java:7035)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.os.Looper.loop(Looper.java:123)
03-25 15:21:46.753: E/AndroidRuntime(438): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-25 15:21:46.753: E/AndroidRuntime(438): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:21:46.753: E/AndroidRuntime(438): at java.lang.reflect.Method.invoke(Method.java:521)
03-25 15:21:46.753: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-25 15:21:46.753: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-25 15:21:46.753: E/AndroidRuntime(438): at dalvik.system.NativeStart.main(Native Method)
当我运行调试时,我看到textView为null,但我无法弄清楚原因。
谢谢!
答案 0 :(得分:2)
尝试将您的findViewById
方法与充气视图elementView
相关联,如:
TextView tv_codeAndType = (TextView) elementView.findViewById(R.id.textView_moduleCode_classType);
其他人也一样..