试图让第二个微调器根据第一个微调器设置而失败

时间:2014-03-10 19:11:00

标签: android spinner

我正在尝试根据驱动器类型,RAID类型和驱动器数量创建一个应用来计算不同的容量总计。

我一直在看这里的不同例子和其他网站试图让这个工作无济于事。无论我尝试做什么,我在运行应用程序时都会得到“java.lang.NullPointerException”。下面是我到目前为止的代码(我只是试图测试旋转器是否会根据第一个微调器的选择动态设置,然后继续):

activity_capacity_calculation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".CapacityCalculation" >

<Spinner
    android:id="@+id/spinnerDrive"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/drive_selection" />

<Spinner
    android:id="@+id/spinnerCapacity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/capacity_selection" />

<Spinner
    android:id="@+id/spinnerRAID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/RAID_selection" />

<Spinner
    android:id="@+id/spinnerNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/number_of_drives" />

<Button
    android:id="@+id/capacity_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capacity_button" />

</LinearLayout>

CapcityCalcultaion.java

package com.jrod.storagecalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class CapacityCalculation extends Activity {

    private Spinner spinnerDrive, spinnerCapacity;

    ArrayAdapter<CharSequence> stringArrayAdapter = ArrayAdapter.createFromResource(CapacityCalculation.this, R.array.drive_array, android.R.layout.simple_spinner_dropdown_item);
    ArrayAdapter<CharSequence> nlsasAdapter = ArrayAdapter.createFromResource(CapacityCalculation.this, R.array.nlsas_capacity, android.R.layout.simple_spinner_dropdown_item);
    ArrayAdapter<CharSequence> sasAdapter = ArrayAdapter.createFromResource(CapacityCalculation.this, R.array.sas_capacity, android.R.layout.simple_spinner_dropdown_item);
    ArrayAdapter<CharSequence> ssdAdapter = ArrayAdapter.createFromResource(CapacityCalculation.this, R.array.ssd_capacity, android.R.layout.simple_spinner_dropdown_item);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capacity_calculation);
        setupActionBar();


            spinnerDrive = (Spinner) findViewById(R.id.spinnerDrive);
            spinnerDrive.setAdapter(stringArrayAdapter);
            spinnerDrive.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String check = (String) parent.getItemAtPosition(position);

                    if (check.startsWith("NL-SAS")) {
                    spinnerCapacity.setAdapter(nlsasAdapter);
                    }else if (check.startsWith("SAS")) {
                    spinnerCapacity.setAdapter(sasAdapter);
                    }else if (check.startsWith("SSD")) {
                    spinnerCapacity.setAdapter(ssdAdapter);
                    }
                }


                public void onNothingSelected(AdapterView<?> parent){

                }
            });

    private void setupActionBar() {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

和我的logcat输出:

03-10 15:07:23.155: E/AndroidRuntime(29319): FATAL EXCEPTION: main
03-10 15:07:23.155: E/AndroidRuntime(29319): Process: com.jrod.storagecalculator, PID: 29319
03-10 15:07:23.155: E/AndroidRuntime(29319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jrod.storagecalculator/com.jrod.storagecalculator.CapacityCalculation}: java.lang.NullPointerException
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2131)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.os.Looper.loop(Looper.java:136)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.ActivityThread.main(ActivityThread.java:5081)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at java.lang.reflect.Method.invokeNative(Native Method)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at java.lang.reflect.Method.invoke(Method.java:515)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:783)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at dalvik.system.NativeStart.main(Native Method)
03-10 15:07:23.155: E/AndroidRuntime(29319): Caused by: java.lang.NullPointerException
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.widget.ArrayAdapter.createFromResource(ArrayAdapter.java:430)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at com.jrod.storagecalculator.CapacityCalculation.<init>(CapacityCalculation.java:15)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at java.lang.Class.newInstanceImpl(Native Method)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at java.lang.Class.newInstance(Class.java:1208)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-10 15:07:23.155: E/AndroidRuntime(29319):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2122)
03-10 15:07:23.155: E/AndroidRuntime(29319):    ... 11 more

1 个答案:

答案 0 :(得分:0)

你必须添加这一行:

spinnerCapacity = (Spinner) findViewById(R.id.spinnerCapacity);