gpt4 book ai didi

java - 将适配器应用于 Spinner 会导致 NullPointerException?

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

我有一个包含 2 个 fragment 的基本 Android 应用程序。

我正在尝试用一些 String 填充 Spinner 中的一个 Fragments。这是我的方法(顺便说一句,如果我在 onCreate 中注释掉整个 Try 语句,这一切都有效。

public class RadarSelectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public RadarSelectionFragment() {
}

@Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.\
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
return inflater.inflate(R.layout.radars, container, false);
} else {
return container;
}

}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

try {
Spinner s = (Spinner) findViewById(R.id.stateselector);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.states, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), 1).show();
}
}
}

R.array.states 是一个包含澳大利亚各州和领地的数组,R.id.stateselector 定义为:

    <Spinner
android:id="@+id/stateselector"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

如果我注释掉 Try 语句,则不显示任何项目。

这一切都是按照来自 - http://developer.android.com/guide/topics/ui/controls/spinner.html 的教程完成的

非常感谢任何帮助!!!

编辑 1:堆栈跟踪

01-20 22:25:09.402: E/AndroidRuntime(8442): FATAL EXCEPTION: main
01-20 22:25:09.402: E/AndroidRuntime(8442): java.lang.NullPointerException
01-20 22:25:09.402: E/AndroidRuntime(8442): at com.example.radarau.MainActivity$RadarSelectionFragment.onCreate(MainActivity.java:176)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:801)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.view.ViewPager.populate(ViewPager.java:1010)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 22:25:09.402: E/AndroidRuntime(8442): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.os.Handler.handleCallback(Handler.java:725)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.os.Looper.loop(Looper.java:137)
01-20 22:25:09.402: E/AndroidRuntime(8442): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-20 22:25:09.402: E/AndroidRuntime(8442): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:25:09.402: E/AndroidRuntime(8442): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 22:25:09.402: E/AndroidRuntime(8442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-20 22:25:09.402: E/AndroidRuntime(8442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-20 22:25:09.402: E/AndroidRuntime(8442): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您不能在 onCreate 方法中使用 findViewById,因为尚未创建 View 层次结构。尝试将您的代码移动到 onActivityCreated:

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

try {
Spinner s = (Spinner) findViewById(R.id.stateselector);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.states, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(), 1).show();
}
}

关于java - 将适配器应用于 Spinner 会导致 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424383/

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