我希望通过下拉Spinner中的选择设置TextView对象的字符串内容(它们需要作为标签,并且会经常更改)。现在,我只是想让两个工作。我已经声明了字段并在每个字段上完成了FindViewById。该应用程序可以完美运行并显示微调框及其内容,但是,当我向微调框添加侦听器时,它会崩溃。任何帮助将不胜感激,因为我看不到哪里出了问题

onCreate()

public class MainActivity extends ActionBarActivity {


    EditText valueInput;
    Spinner distance_selector1;
    Spinner distance_selector2;

    TextView resultOutput;
    TextView unitsInput;
    TextView unitsOutput;

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Ensures keyboard is hidden at the start of activity
               this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    initControls();
}


initControls()

public void initControls(){

    valueInput=(EditText)findViewById(R.id.input);

    if(valueInput.getText().toString().equals(null) || valueInput.getText().toString().equals(""))
       {
            Toast.makeText(this,"Please", Toast.LENGTH_LONG).show();
       }

    resultOutput=(TextView)findViewById(R.id.result);

            /** The following is for the population of the spinner drop down menu with the
     *  array of values in strings.xml
     */

    unitsInput=(TextView)findViewById(R.id.unitsInput);
    unitsOutput=(TextView)findViewById(R.id.unitsOutput);
    Spinner spinner1 = (Spinner) findViewById(R.id.distance_selector1);
    Spinner spinner2 = (Spinner) findViewById(R.id.distance_selector2);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.distance_array, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    // Apply the adapter to the spinner
    spinner1.setAdapter(adapter);
    spinner2.setAdapter(adapter);

    distance_selector1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String str = distance_selector1.getSelectedView().toString();
            unitsInput.setText(str);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

    });


    }


LOGCAT:

06-23 14:04:58.140: E/AndroidRuntime(1258): FATAL EXCEPTION: main
06-23 14:04:58.140: E/AndroidRuntime(1258): Process: com.example.converterking, PID: 1258
06-23 14:04:58.140: E/AndroidRuntime(1258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.converterking/com.example.converterking.MainActivity}: java.lang.NullPointerException
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.os.Looper.loop(Looper.java:136)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at java.lang.reflect.Method.invoke(Method.java:515)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at dalvik.system.NativeStart.main(Native Method)
06-23 14:04:58.140: E/AndroidRuntime(1258): Caused by: java.lang.NullPointerException
06-23 14:04:58.140: E/AndroidRuntime(1258):     at com.example.converterking.MainActivity.initControls(MainActivity.java:76)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at com.example.converterking.MainActivity.onCreate(MainActivity.java:45)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.Activity.performCreate(Activity.java:5231)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-23 14:04:58.140: E/AndroidRuntime(1258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-23 14:04:58.140: E/AndroidRuntime(1258):     ... 11 more

最佳答案

试试这个

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String str = spinner1.getSelectedItem().toString();
        unitsInput.setText(str);
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});


您应该将setOnItemSelectedListener(..)设置为spinner1。我猜你有NPE是因为忘记了初始化distance_selector1

07-27 17:14