因此,我的团队正在尝试创建一个简单的 Android 程序来帮助引导人们穿过建筑物。我们试图在 Spinner 中显示搜索结果,但不幸的是,每当初始化 Spinner 时程序就会崩溃。我们知道代码在 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