0

因此,我的团队正在尝试创建一个简单的 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
4

3 回答 3

0

声明一个 xml 调用“my_xml_with_spinner.xml”,在其中您将一个带有 id spinner1 的微调器视图放置在一个容器(LinearLayout、RelativeLayout 等...)中,然后在您的代码更改中:

setContentView(textView)

为了

setContentView(R.layout.my_xml_with_spinner)

现在将找到微调器视图,当您调用 findViewById 时,微调器不会像现在那样为空。

之所以为空,是因为您将一个简单的 TextView 设置为布局,并且代码在您设置的布局(textView)上找不到任何视图,ID 为 spinner1。

于 2013-07-16T00:52:17.023 回答
0

在调用 (Spinner) findViewById(R.id.spinner1) 之前使用 setContentView(layoutid), (布局 id 中应该定义一个微调器);在此之前 spinner 将为空,这会使您的应用程序崩溃

于 2013-07-16T00:46:58.517 回答
0

您正在调用setContentView()并传入一个 TextView。您可能希望传入布局引用而不是(而不是 form R.layout.something),否则使用微调器的 id 调用 findViewById() 将返回 null,因此您会NullPointerException在该行得到 a 。

于 2013-07-16T00:45:15.440 回答