gpt4 book ai didi

android - 使用 Spinner 选定项填充 TextView - Android

转载 作者:行者123 更新时间:2023-11-29 17:48:59 29 4
gpt4 key购买 nike

我希望根据在下拉微调器中所做的选择来设置 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();
}

初始化控件()

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

}

});


}

洛卡特:

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

关于android - 使用 Spinner 选定项填充 TextView - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24367003/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com