gpt4 book ai didi

android - 我在应用程序中添加了一个微调器,但由于适配器代码, Activity 没有启动

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:43:13 26 4
gpt4 key购买 nike

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rishabh.casino/com.example.rishabh.casino.Game}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

最佳答案

主要问题是您正在 View 上方初始化微调器。你应该放 setContentView(R.layout.activity_game);在上面和之后你应该初始化其他组件。更正您的 onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_game); //Initialize layout first

spin=(Spinner)findViewById(R.id.spinner);
rembal=(TextView)findViewById(R.id.textView8);
bet_amount=(TextView)findViewById(R.id.textView9);
beton=(TextView)findViewById(R.id.textView10);
result=(TextView)findViewById(R.id.textView11);
bet=(EditText)findViewById(R.id.editText2);
okgen=(Button)findViewById(R.id.button4);

ArrayAdapter<String> adapter_option=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,options);
adapter_option.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_option);
spin.setOnItemSelectedListener(this);
super.onCreate(savedInstanceState);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}

这就是空指针异常背后的原因。 View 尚未初始化,您正在尝试初始化 View 内的微调器。

关于android - 我在应用程序中添加了一个微调器,但由于适配器代码, Activity 没有启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300338/

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