I am developing an Android application that uses fragments. I got this error.
Caused by: java.lang.NullPointerException: name == null
What does name == null mean? Here is the full magazine
08-04 15:02:22.422: E/AndroidRuntime(2499): FATAL EXCEPTION: main 08-04 15:02:22.422: E/AndroidRuntime(2499): java.lang.RuntimeException: Unable to start activity ComponentInfo{fi.peltoset.mikko.home/fi.peltoset.mikko.home.Koti}: android.view.InflateException: Binary XML file line
This is a class called Navigation . It extends android.app.Fragment .
In it, the onCreateView method onCreateView the layout and onCreateView it. In the onActivityCreated method onActivityCreated I check if the layout has two panels (I have different layouts for different devices), and based on this I add onClickListeners to them.
My code is as follows.
public class Navigation extends Fragment { private OnItemSelectedListener listener; private boolean dualPane = false; public interface OnItemSelectedListener { public void onMenuItemSelected(int fragmentId); } public void onAttach(Activity activity) { super.onAttach(activity); if(activity instanceof OnItemSelectedListener) { this.listener = (OnItemSelectedListener) activity; } else { ... } } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_navigation, container, false); return view; } public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View contentPane = getActivity().findViewById(R.id.contentFragment); dualPane = contentPane != null && contentPane.getVisibility() == View.VISIBLE; if(dualPane) { final ListView menu = (ListView) getActivity().findViewById(R.id.valikko); menu.setChoiceMode(ListView.CHOICE_MODE_SINGLE); String[] texts = new String[] { ... }; ArrayList<MenuItem> items = new ArrayList<MenuItem>(); items.add(new MenuItem("Home", R.drawable.koti2)); ... ImageArrayAdapter adapter = new ImageArrayAdapter(getActivity(), items, texts); menu.setAdapter(adapter); menu.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String item = ((TextView) view.findViewById(R.id.label)).getText().toString(); listener.onMenuItemSelected(0);
The layout file layout-sw720dp-land/activity_navigation.xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/pressed_koti" android:orientation="vertical" > <ListView android:id="@+id/valikko" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </ListView> </LinearLayout>
Here is the activity_koti.xml file.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:id="@+id/menuFragment" android:layout_width="300dp" android:layout_height="match_parent" class="fi.peltoset.mikko.home.Navigation" /> <fragment android:id="@+id/contentFragment" android:layout_width="fill_parent" android:layout_height="match_parent" /> </LinearLayout>
I launched the application on an emulator with Android 4.1.2.
What causes these errors and how to fix them?