gpt4 book ai didi

nullpointerexception - 如何获取 Spinner List 选定的项目文本并用该值填充 android 中的 EditText

转载 作者:行者123 更新时间:2023-11-29 00:21:41 28 4
gpt4 key购买 nike

当我尝试在选择监听器的 Itemselected 函数中获取 EditText 对象时,该行抛出 NullPointerException

<resources>
<string-array name="gender">
<item name="Male">Male</item>
<item name="Female">Female</item>
</string-array>
</resources>

布局

<EditText
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textview1"
android:layout_below="@+id/textview1"
android:ems="10"
android:hint="answer" >

<requestFocus />
</EditText>

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/answer"
android:layout_below="@+id/answer"
android:layout_marginTop="22dp"
android:entries="@array/gender"
android:prompt="@string/spin_prompt" />

主要 Activity

public class MenuActivity extends Activity {

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

Spinner sp= (Spinner) findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(new SpinnerActivity());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}


}

旋转器 Activity

public class SpinnerActivity extends Activity implements OnItemSelectedListener{

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

String selec = (parent.getItemAtPosition(pos)).toString();

EditText ans= (EditText) findViewById(R.id.answer);
//NullPointerException at the time of retrieving EditText object

ans.setText(selec);
}

public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}

}

Log-Cat 错误条目

03-21 17:23:42.232: E/AndroidRuntime(4019): FATAL EXCEPTION: main
03-21 17:23:42.232: E/AndroidRuntime(4019): java.lang.NullPointerException
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.app.Activity.findViewById(Activity.java:1853)
03-21 17:23:42.232: E/AndroidRuntime(4019): at com.practice.menu.SpinnerActivity.onItemSelected(SpinnerActivity.java:19)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.widget.AdapterView.access$200(AdapterView.java:49)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.os.Handler.handleCallback(Handler.java:730)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.os.Looper.loop(Looper.java:137)
03-21 17:23:42.232: E/AndroidRuntime(4019): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 17:23:42.232: E/AndroidRuntime(4019): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 17:23:42.232: E/AndroidRuntime(4019): at java.lang.reflect.Method.invoke(Method.java:525)
03-21 17:23:42.232: E/AndroidRuntime(4019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 17:23:42.232: E/AndroidRuntime(4019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 17:23:42.232: E/AndroidRuntime(4019): at dalvik.system.NativeStart.main(Native Method)

最佳答案

这里:

sp.setOnItemSelectedListener(new SpinnerActivity());

您正在尝试实例化另一个 Activity 并尝试将其设置为监听器。这行不通,因为 Android Activity 不喜欢它被显式实例化。

尝试像这样为您的微调器实现监听器:

    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});

你不再需要的 SpinnerActivity

关于nullpointerexception - 如何获取 Spinner List 选定的项目文本并用该值填充 android 中的 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570146/

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