そこで、私のグループは、人々が建物を案内するのに役立つ簡単な Android プログラムを作成しようとしています。スピナー内で検索結果を表示しようとしていますが、残念ながら、スピナーが初期化されるたびにプログラムがクラッシュします。コードが Spinner の初期化まで機能することはわかっています。私たちの問題を診断するのを手伝ってくれる人はいますか?
関連機能コード:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here goes the code for the searching of the building in the database file.
// Get the message from the intent
Intent intent = getIntent();
String building = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message = "Search results for \"" + building + "\"";
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(message);
InputStream buildinginfo = null;
try {
buildinginfo = getResources().getAssets().open("namelist.txt");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader myDIS = new BufferedReader(new InputStreamReader(buildinginfo));
//you've now got an instance of BufferedReader called myDIS
ArrayList<String> list = new ArrayList<String>();
int x = -1;
String conglomerate = "";
for(int i=0;i<101;i++){
try {
String something = myDIS.readLine();
x = something.indexOf(building);
if(x>-1){
list.add(something);
}
} catch (IOException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
}
for(int i=0 ; i < (list.size()-1) ; i++) {
String display = list.get(i).substring(5);
conglomerate = conglomerate+display+"\n";
}
setContentView(textView);
//Add Spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
spinner.setAdapter(adapter);
}
これにより、LogCat でこれらのエラーが発生します。
07-16 00:19:47.951: E/AndroidRuntime(273): FATAL EXCEPTION: main
07-16 00:19:47.951: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{iefx.testing.myapp/iefx.testing.myapp.DisplayMessageActivity}: java.lang.NullPointerException
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-16 00:19:47.951: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 00:19:47.951: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521)
07-16 00:19:47.951: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-16 00:19:47.951: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-16 00:19:47.951: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method)
07-16 00:19:47.951: E/AndroidRuntime(273): Caused by: java.lang.NullPointerException
07-16 00:19:47.951: E/AndroidRuntime(273): at iefx.testing.myapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:83)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-16 00:19:47.951: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-16 00:19:47.951: E/AndroidRuntime(273): ... 11 more