实现微调器会导致程序崩溃

时间:2013-07-16 00:39:04

标签: android spinner

因此,我的小组正在尝试创建一个简单的Android程序,以帮助引导人们穿越建筑物。我们试图在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

3 个答案:

答案 0 :(得分:0)

您正在调用setContentView()并传入TextView。您可能希望传递布局引用(格式为R.layout.something),否则使用spinner的id调用findViewById()将返回null,因此您在该行上获得NullPointerException

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

setContentView(textView)

setContentView(R.layout.my_xml_with_spinner)

现在可以找到微调器视图,当你调用findViewById时,spinner将不会像现在一样为空。

之所以因为它是null,是因为你将布局设置为一个简单的TextView,并且代码无法在你设置的布局(textView)上找到id为spinner1的任何视图。