gpt4 book ai didi

android - 我不能使用 spinner.setAdapter(adapter);

转载 作者:搜寻专家 更新时间:2023-11-01 07:40:48 24 4
gpt4 key购买 nike

我有 CameraList 类,它提供像“SONY”、“VIVOTEK”、“VIVOTEK 7XXX”、“PIXORD”、“TOSHIBA”、“JVC E-CON”这样的相机列表......

我想在微调器中列出它们,但我不能。有我的代码..

public class AddCam extends MainActivity {
CameraList cams = new CameraList();

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

Toast msg = Toast.makeText(AddCam.this, getString(R.string.add_new), Toast.LENGTH_LONG);

msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);

String a = getString(R.string.add_new);

TextView tv = (TextView) findViewById(R.id.head);
tv.setText(getString(R.string.add_new));

Spinner s = (Spinner) findViewById(R.id.spinner);

ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(
this, R.array.cameras, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//s.setAdapter(adapter); **** there is null poınter ex.
setContentView(R.layout.spinnerex);
}
}

我的camlist代码是

public class CameraList {
private static String[] cameraModelNames = {"AXIS","AVOISYS","SONY","..."}

public String[] getlist()
{
return cameraModelNames;
}
}

这是我的日志

10-02 12:33:50.007: ERROR/AndroidRuntime(4624): Uncaught handler: thread main exiting due to uncaught exception
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): java.lang.RuntimeException: Unable to start activity ComponentInfo{spexco.hus.cepvizyon/spexco.hus.cepvizyon.AddCam}: java.lang.NullPointerException
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.os.Looper.loop(Looper.java:123)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at java.lang.reflect.Method.invoke(Method.java:521)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at dalvik.system.NativeStart.main(Native Method)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): Caused by: java.lang.NullPointerException
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at spexco.hus.cepvizyon.AddCam.onCreate(AddCam.java:39)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): ... 11 more

最佳答案

您需要调用 setContentView(R.layout.spinnerex) 尝试访问该布局中的 UI 元素。

关于android - 我不能使用 spinner.setAdapter(adapter);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3845036/

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