我正在开发类似下一个的片段类:
public class DemandFragment extends Fragment implements LocationListener{...
我已声明:
Spinner spinner_loc;
ArrayAdapter<String> spinner_loc_adapter = null;
@Override
public void onActivityCreated(Bundle state){
super.onActivityCreated(state);
Bundle bundle = this.getArguments();
source_category = Integer.parseInt(bundle.getString("category"));
prepareSpinner();
...
我有从onActivityCreated方法调用它的下一个函数(模块化代码):
private void prepareSpinner(){
spinner_loc = (Spinner) getView().findViewById(R.id.spinner_loc);
...
进一步......在prepareSpinner()方法内部。
spinner_loc = null;
spinner_loc_adapter = null;
spinner_loc_adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_content, values);
spinner_loc.setAdapter(spinner_loc_adapter);
spinner_loc_adapter.notifyDataSetChanged();
值是一个字符串数组。
问题:我在spinner_loc_adapter上遇到空指针异常。我相信错误是背景,但我无法弄清楚原因。
必须包含的内容:Fragment_class - &gt; oncreate() - &gt; prepareSpinner()??
我在声明中尝试使用Context _c,并将其传递给适配器以代替getActivity(),但我得到了同样的错误。
logcat的
03-24 12:51:46.605 12785-12785/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.test.fragment.DemandFragment.prepareSpinner(DemandFragment.java:330)
at com.test.fragment.DemandFragment.onActivityCreated(DemandFragment.java:95)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white">
<ScrollView
android:layout_weight="0.9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout_select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#1794d4"
android:alpha="0.9"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="@string/offer_location"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:id="@+id/offer_location" />
<Spinner
android:id="@+id/spinner_location"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:background="@drawable/spinner_button"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:divider="@drawable/spinner_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="@string/choose_brand_model"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:textIsSelectable="false" />
<Spinner
android:id="@+id/spiner_marca"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center_vertical"
android:background="@drawable/spinner_button"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:divider="@drawable/spinner_button"
/>
<Spinner
android:id="@+id/spiner_modelo"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:background="@drawable/spinner_button"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:divider="@drawable/spinner_button"
/>
<Button
android:id="@+id/bt_disable_spinner"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:gravity="center"
android:textColor="@drawable/dark_blue_text_button"
android:text="@string/brand_not_sure"
android:textSize="10sp"
android:background="@drawable/dark_blue_border_button"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#cccccc"
android:text="@string/wanted"
android:layout_below="@id/layout_select"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:id="@+id/layout_options">
<ImageButton
android:id="@+id/bt_add_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_add_text"/>
<ImageButton
android:id="@+id/bt_add_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_add_photo"/>
<ImageButton
android:id="@+id/bt_add_audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/ic_add_audio"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:id="@+id/layout_type">
<Button
android:id="@+id/bt_nuevo"
android:layout_width="90dp"
android:layout_height="20dp"
android:textColor="@drawable/gray_text_button"
android:text="@string/demand_new"
android:textSize="10sp"
android:layout_marginRight="10dp"
android:background="@drawable/gray_border_button"/>
<Button
android:id="@+id/bt_km0"
android:layout_width="90dp"
android:layout_height="20dp"
android:textColor="@drawable/gray_text_button"
android:text="@string/demand_km0"
android:textSize="10sp"
android:layout_marginRight="10dp"
android:background="@drawable/gray_border_button"/>
<Button
android:id="@+id/bt_2mano"
android:layout_width="90dp"
android:layout_height="20dp"
android:textColor="@drawable/gray_text_button"
android:text="@string/demand_used"
android:textSize="10sp"
android:background="@drawable/gray_border_button"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/imageViewSend"
android:src="@drawable/bt_enviar_solicitud"
android:layout_gravity="center|bottom"
android:layout_marginTop="10dp"
android:background="#1794d4"
android:paddingLeft="80dp"
android:paddingRight="80dp"
android:layout_weight="0.1"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
答案 0 :(得分:1)
在片段的onActivtiyCreated
中使用bewlow。 spinner_loc
可能为null coz getView()
返回null。您需要等到创建活动并将片段附加到活动
spinner_loc = (Spinner) getView().findViewById(R.id.spinner_loc);
spinner_loc_adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_content, values);
spinner_loc.setAdapter(spinner_loc_adapter);
spinner_loc_adapter.notifyDataSetChanged();
编辑:
<Spinner
android:id="@+id/spinner_location"
更改
spinner_loc = (Spinner) getView().findViewById(R.id.spinner_loc);
到
spinner_loc = (Spinner) getView().findViewById(R.id.spinner_location);
答案 1 :(得分:0)
尝试使用以下代码
View view =inflater.inflate(R.layout.fragment_view,null);
spinner_loc = (Spinner) view.findViewById(R.id.spinner_loc);
spinner_loc_adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_content, values);
spinner_loc.setAdapter(spinner_loc_adapter);
spinner_loc_adapter.notifyDataSetChanged();