ArrayAdapter
を介して配列に保持されているデータを表示するために を使用してListView
います。コードは正常にコンパイルされますが、以下のクラスを呼び出すと、プロセスがSorry application has stopped
例外で終了します。
public class Timetable extends Activity {
private ListView timeTableView;
private ArrayList <Module> Modules;
private ArrayAdapter <Module> adaptModule;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
timeTableView = (ListView) findViewById(R.id.timeTableView);
adaptModule = new ArrayAdapter<Module>(this, android.R.layout.simple_list_item_1, Modules);
timeTableView.setAdapter(adaptModule);
}
}
Add
配列の作成と配列への項目の追加を扱う私のクラスは次のとおりです。
public class Add extends Activity implements OnClickListener{
private EditText ModuleText;
private EditText RoomText;
private Spinner Day;
private Spinner Start;
private Spinner Duration;
private Spinner Session;
private ArrayList<Module> Modules;
/*
* OnCreate method for add class
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
// spinner for days
Spinner day = (Spinner) findViewById(R.id.spinnerDay);
ArrayAdapter<CharSequence> adapterDay = ArrayAdapter.createFromResource(
this, R.array.days_array, android.R.layout.simple_spinner_item);
adapterDay.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
day.setAdapter(adapterDay);
// spinner for start time
Spinner startTime = (Spinner) findViewById(R.id.spinnerStart);
ArrayAdapter<CharSequence> adapterstart = ArrayAdapter.createFromResource(
this, R.array.start_array, android.R.layout.simple_spinner_item);
adapterstart.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
startTime.setAdapter(adapterstart);
// spinner for duration
Spinner duration = (Spinner) findViewById(R.id.spinnerDuration);
ArrayAdapter<CharSequence> adapterduration = ArrayAdapter.createFromResource(
this, R.array.duration_array, android.R.layout.simple_spinner_item);
adapterduration.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
duration.setAdapter(adapterduration);
// spinner for class type
Spinner session = (Spinner) findViewById(R.id.spinnerSession);
ArrayAdapter<CharSequence> adapterSession = ArrayAdapter.createFromResource(
this, R.array.session_array, android.R.layout.simple_spinner_item);
adapterSession.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
session.setAdapter(adapterSession);
//create array
Modules = new ArrayList<Module>();
// ref to UI Widgets
ModuleText = (EditText) findViewById(R.id.ModuleText);
RoomText = (EditText) findViewById(R.id.RoomText);
Day = (Spinner) findViewById(R.id.spinnerDay);
Start = (Spinner) findViewById(R.id.spinnerStart);
Duration = (Spinner) findViewById(R.id.spinnerDuration);
Session = (Spinner) findViewById(R.id.spinnerSession);
// buttons for this activity also includes listeners
View addEntryButton = findViewById(R.id.addEntry_button);
addEntryButton.setOnClickListener(this);
View cancelEntryButton = findViewById(R.id.cancelEntry_button);
cancelEntryButton.setOnClickListener(this);
}
/*
* on click listeners for buttons
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View v) {
switch (v.getId()) {
case R.id.cancelEntry_button:
Intent i = new Intent(this, Home.class);
startActivity(i);
break;
}
switch (v.getId()) {
case R.id.addEntry_button:
Intent i = new Intent(this, Home.class);
addModule();
startActivity(i);
break;
}
}
/*
* Method for adding an entry into the table
*/
public void addModule()
{
if(ModuleText !=null || RoomText !=null)
{
Modules.add(new Module(ModuleText.getText().toString(), RoomText.getText().toString(), Day.getSelectedItem().toString(), Start.getSelectedItem().toString(), Duration.getSelectedItem().toString(), Session.getSelectedItem().toString()));
Toast.makeText(getApplicationContext(), "Timetable entry has been added succesfully", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Error adding timetable entry, please check details and try again", Toast.LENGTH_SHORT).show();
}
}
}
これが私logcat
の例外です:
04-22 11:52:46.290: E/AndroidRuntime(426): FATAL EXCEPTION: main
04-22 11:52:46.290: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.assignment.timetable/org.assignment.timetable.Timetable}: java.lang.NullPointerException
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 11:52:46.290: E/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:52:46.290: E/AndroidRuntime(426): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 11:52:46.290: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 11:52:46.290: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 11:52:46.290: E/AndroidRuntime(426): at dalvik.system.NativeStart.main(Native Method)
04-22 11:52:46.290: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException
04-22 11:52:46.290: E/AndroidRuntime(426): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.widget.ListView.setAdapter(ListView.java:436)
04-22 11:52:46.290: E/AndroidRuntime(426): at org.assignment.timetable.Timetable.onCreate(Timetable.java:25)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-22 11:52:46.290: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)