使用微调器选择项目填充TextView - Android

时间:2014-06-23 13:18:44

标签: android textview spinner listener android-spinner

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

的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

2 个答案:

答案 0 :(得分:2)

试试这种方式

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

答案 1 :(得分:1)

另外,试试这个

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

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

第二路

spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){

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


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

第三路

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) {
        } 
    });

NJOY:)