我的片段没有正确初始化对象。我很难理解为什么。
许多StackOverflow问题都建议在片段中添加以下行,OnCreateView()应该正确初始化对象。
mList = (ListView) view.findViewById(R.id.locationList);
它似乎没有工作,正如我在调试中看到的,我的Switch和ListView返回为null。在我的片段xml中,我有一个适当的lostToggle和locationList的Switch和ListView id,所以他们应该指向这些对象。我正在努力想知道为什么会这样。任何帮助表示赞赏。
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");
}
}
});
}
}
<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)
答案 0 :(得分:0)
尝试初始化onViewCreated(View view, Bundle savedInstanceState)
中的对象
方法