【问题标题】:All Objects within a fragment returning null片段中的所有对象都返回 null
【发布时间】:2016-12-10 04:21:05
【问题描述】:

我的片段没有正确初始化对象。我很难理解为什么。

许多 StackOverflow 问题都建议在片段的 OnCreateView() 中添加以下行应该正确初始化对象。

mList = (ListView) view.findViewById(R.id.locationList);

它似乎不起作用,正如我在调试中看到的那样,我的 Switch 和 ListView 返回为空。在我的片段的 xml 中,我有一个 Switch 和 ListView id 的 lostToggle 和 locationList 适当,所以它们应该指向这些对象。我很难理解为什么会这样。任何帮助表示赞赏。

page2_fragment.java

public class page2_fragment extends Fragment {

    private Switch mToggle;
    private ListView mList;
    private ArrayAdapter<String> mLocationArrayAdapter;
    private ArrayList<String> items = new ArrayList<String>();

    public page2_fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.page2_fragment, container, false);
        mToggle = (Switch) view.findViewById(R.id.lostToggle);
        mList = (ListView) view.findViewById(R.id.locationList);
        mLocationArrayAdapter = new ArrayAdapter<>(
                getActivity().getApplicationContext(),
                android.R.layout.simple_expandable_list_item_1,
                items);

        setupLostToggle();
        // Inflate the layout for this fragment
        return view;
    }

    public ArrayAdapter getArrAdapter() { return mLocationArrayAdapter; }
    public ListView getList() { return mList; }
    public ArrayList<String> getArrayList() { return items; }
    public Switch getToggle() { return mToggle; }

    private void setupLostToggle() {
        //Add "Lost" toggle functionality
        mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // The toggle is enabled
                    Log.d("Toggle: ","Enabled");
                } else {
                    // The toggle is disabled
                    Log.d("Toggle: ","Disabled");
                }
            }
        });
    }
}

page2_fragment.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.robertdavis.aloost.page2_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/t1_colorPrimaryDark"
        android:baselineAligned="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/activity_vertical_margin"
            android:text="@string/keyword_location"
            android:textAllCaps="true"
            android:textColor="@android:color/white"/>
    </LinearLayout>
    <android.support.v7.widget.CardView
        android:id="@+id/main_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginBottom="8dp"
        app:cardCornerRadius="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|end"
                android:gravity="top"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="9dp"
                    android:layout_weight="0.25"
                    android:text="I'm looking for"/>

                <Spinner
                    android:id="@+id/spinnerCategory"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:layout_weight="0.75"/>

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|end"
                android:gravity="top"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="within"/>

                <Spinner
                    android:id="@+id/spinnerRadius"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="kilometres." />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|end"
                android:gravity="top"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/button_getPlacesData"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="getPlacesData"
                    android:text="Go"
                    android:textAllCaps="true" />

                <Switch
                    android:id="@+id/lostToggle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="22dp"
                    android:text="Lost? "
                    android:layout_centerHorizontal="true"/>
            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:id="@+id/results_list_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="30dp"
        app:cardCornerRadius="4dp">

        <ListView
            android:id="@+id/locationList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.v7.widget.CardView>
</LinearLayout>

编辑:Logcat 数据(根据要求)

12-10 06:57:20.088 18139-18139/com.example.robertdavis.aloost E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.robertdavis.aloost, PID: 18139
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.example.robertdavis.aloost.AsyncTaskParseJson.onPostExecute(AsyncTaskParseJson.java:95)
    at com.example.robertdavis.aloost.AsyncTaskParseJson.onPostExecute(AsyncTaskParseJson.java:16)
    at android.os.AsyncTask.finish(AsyncTask.java:651)
    at android.os.AsyncTask.-wrap1(AsyncTask.java)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

【问题讨论】:

  • 它们是否在 android studio 错误或崩溃/堆栈跟踪中显示 null?
  • 两者。调试时,我可以看到 mList 和 mToggle 为空,当尝试将适配器设置到列表中时,我得到一个“java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android. widget.ListAdapter)' 在空对象引用上"错误
  • 这是您的 xml 的完整版本吗?
  • 对于我的片段,是的
  • 你有镜像布局文件吗?说 layout-sw600dp/page2_fragment.xml

标签: android android-fragments nullpointerexception


【解决方案1】:

尝试在onViewCreated(View view, Bundle savedInstanceState) 中初始化对象 方法

【讨论】:

  • 这似乎不起作用。使用此方法,我的对象仍然会收到 NullPointerException。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多