单击菜单按钮时,Android应用程序崩溃

时间:2019-02-16 04:48:52

标签: android

因此,一切正常都可以在应用程序中正常加载,直到我单击菜单按钮,然后它会杀死该应用程序,而不是显示信息。没有语法错误,但出现以下运行时错误
  java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.ListView.setAdapter(android.widget.ListAdapter)”         在burt.example.com.MenuItemList.onViewCreated(MenuItemList.java:48)

下面是我警告的班级代码

package burt.example.com;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class MenuItemList extends Fragment {

    public List<Item> items = itemDataProvider.items;
    public static final String ITEM_ID = "ITEM_ID";


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_menu_item_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ItemAdapter itemAdapter = new ItemAdapter(view.getContext(), R.layout.single_menu_item, items);

        ListView listView = view.findViewById(R.id.item_list);

        listView.setAdapter(itemAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Item item = items.get(position);
                getActivity().getIntent().putExtra(ITEM_ID, item.getId());

                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                transaction.replace(R.id.item_detail_container, new MenuItemDetail());
                transaction.addToBackStack(null);
                transaction.commit();

            }
        });
    }

}

fragment_menu_item_list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MenuItemList" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在您的xml中。您需要分配一个id的listview。 item_list

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:id="@+id/item_list"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />