android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

问题遇到的现象和发生背景

运行后能打开软件,但是点击按钮后就会停止运行

img

用代码块功能插入代码,请勿粘贴截图

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.ymx.fragment07_1.MainActivity"
    android:background="@drawable/ic_launcher_foreground">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/btnTj"
            android:text="推荐"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
        <Button
            android:id="@+id/btnCc"
            android:text="炒菜"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            />
        <Button
            android:id="@+id/btnGjf"
            android:text="盖浇饭"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/flList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_centerInParent="true"
        android:background="@drawable/recom_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_below="@id/img"
        android:id="@+id/tvShow"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:text="这边是Fragment的内容"
        android:layout_height="wrap_content" />
</RelativeLayout>


fragment_my.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ListView
        android:listSelector="@color/colorPrimary"
        android:id="@+id/foodlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>


list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img"
        android:layout_centerInParent="true"
        android:src="@drawable/must_buy_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tvShow"
        android:textSize="20sp"
        android:layout_below="@id/img"
        android:text="这边是Fragment的内容"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

MainActivity.java

package com.example.ymx.fragment07_1;

import android.app.*;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.ymx.fragment07_1.bean.FoodBean;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    //推荐菜单的内容
    private String[] title1 = {"狮子头", "宫保鸡丁", "佛跳墙"};
    private int[] img1 = {R.drawable.must_buy_one, R.drawable.must_buy_two, R.drawable.must_buy_three};
    //限时特惠内容
    private String[] title2 = {"小鸡炖蘑菇", "东坡肉", "西红柿炒蛋"};
    private int[] img2 = {R.drawable.recom_one, R.drawable.recom_two, R.drawable.recom_three};
    //限时特惠内容
    private String[] title3 = {"小鸡炖蘑菇", "东坡肉"};
    private int[] img3 = {R.drawable.recom_one, R.drawable.recom_two};

    private List<FoodBean> list1;
    private List <FoodBean> list2;
    private List <FoodBean> list3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnTj = (Button) findViewById(R.id.btnTj);
        Button btnCc = (Button) findViewById(R.id.btnCc);
        Button btnGjf = (Button) findViewById(R.id.btnGjf);

        btnTj.setOnClickListener(this);
        btnCc.setOnClickListener(this);
        btnGjf.setOnClickListener(this);

        list1 = new ArrayList<FoodBean>();
        for (int i = 0; i < title1.length; i++) {
            FoodBean foodBean = new FoodBean();
            foodBean.setImgId(img1[i]);
            foodBean.setTitle(title1[i]);
            list1.add(foodBean);
        }
        list2 = new ArrayList<FoodBean>();
        for (int i = 0; i < title2.length; i++) {
            FoodBean foodBean = new FoodBean();
            foodBean.setImgId(img2[i]);
            foodBean.setTitle(title2[i]);
            list2.add(foodBean);
        }
        list3 = new ArrayList<FoodBean>();
        for (int i = 0; i < title3.length; i++) {
            FoodBean foodBean = new FoodBean();
            foodBean.setImgId(img3[i]);
            foodBean.setTitle(title3[i]);
            list3.add(foodBean);
        }
    }
    @Override
    public void onClick(View view) {
//        FoodBean foodBean = new FoodBean();
        switch (view.getId()) {
            case R.id.btnTj:
//                createFragment(list1);
//                foodBean.setTitle("这里是推荐内容");
//                foodBean.setImgId(R.drawable.recom_one);
                setFragment(list1);
//                setFragment("这里是推荐的内容",R.drawable.recom_one);
                break;
            case R.id.btnCc:
//                createFragment(list2);
//                foodBean.setTitle("这里是炒菜内容");
//                foodBean.setImgId(R.drawable.recom_two);
                setFragment(list2);
//                setFragment("这里是炒菜的内容",R.drawable.recom_two);
                break;
            case R.id.btnGjf:
                setFragment(list3);
                break;
        }
    }


    private void setFragment (List<FoodBean> list){
        ListFragment listFragment = new ListFragment();
        ListFragment instance = listFragment.getInstance(list);
       FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.flList,instance);
        fragmentTransaction.commit();

    }
}


FoodBean.java

package com.example.ymx.fragment07_1.bean;

/**
 * Created by Ymx on 2022/11/11.
 */

public class FoodBean {
    private String title;
    private int imgId;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }
}


ListFragment.java

package com.example.ymx.fragment07_1;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.ymx.fragment07_1.adpater.FoodAdapter;
import com.example.ymx.fragment07_1.bean.FoodBean;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Ymx on 2022/11/11.
 */

public class ListFragment extends Fragment{
    private FoodBean foodBean;
    public ListFragment getInstance(List<FoodBean> list){
        ListFragment listFragment = new ListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("foodList",(Serializable) list);
        listFragment.setArguments(bundle);
        return listFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list,container,false);
        Bundle bundle = getArguments();
        List<FoodBean> list = (List<FoodBean>) bundle.getSerializable("foodList");
        ListView foodList = (ListView) view.findViewById(R.id.foodlist);
        FoodAdapter foodAdapter = new FoodAdapter(list,getActivity());
        foodList.setAdapter(foodAdapter);
        return view;
    }
}

FoodAdpater.java

package com.example.ymx.fragment07_1.adpater;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ymx.fragment07_1.R;
import com.example.ymx.fragment07_1.bean.FoodBean;

import java.util.List;

/**
 * Created by Ymx on 2022/11/11.
 */

public class FoodAdapter extends BaseAdapter{
    private List<FoodBean> list;
    private Context context;
    public FoodAdapter(List<FoodBean> list, Context context){
        this.list = list;
        this.context = context;
    }
    @Override
    public int getCount() { //表示listview控件一共显示多少条数据
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.list_item,null);
        TextView text = (TextView) view.findViewById(R.id.tvShow);
        ImageView img = (ImageView) view.findViewById(R.id.img);
        FoodBean foodBean = list.get(i);
        text.setText(foodBean.getTitle());
        img.setImageResource(foodBean.getImgId());
        return view;
    }
}


运行结果及报错内容

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ymx.fragment07_1, PID: 32682
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.ymx.fragment07_1.ListFragment.onCreateView(ListFragment.java:39)
at android.app.Fragment.performCreateView(Fragment.java:2611)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1276)
at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2415)
at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2194)
at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2148)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2049)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:718)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

你的listview不是在fragment_my里面吗,fragment_list里面没有
找不到自然就空指针了

img

,换成fragment_my

直接崩溃应该是空指针了,看一下报错信息找一下对应位置去看一下