Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 片段中的自定义ListView出现错误_Android_Android Fragments_Android Activity - Fatal编程技术网

Android 片段中的自定义ListView出现错误

Android 片段中的自定义ListView出现错误,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,我试图在片段中构建自定义ListView,但出现了一些错误 当我的应用程序运行时,它会崩溃并给我以下行错误: java.lang.NullPointerException: 这是适配器代码中的这一行: convertView = mInflater.inflate(R.layout.list_adapter_view, null); 我做错了什么 这是我的片段: public class MyOrderFragment extends Fragment { TextView name;

我试图在片段中构建自定义ListView,但出现了一些错误

当我的应用程序运行时,它会崩溃并给我以下行错误:

java.lang.NullPointerException:

这是适配器代码中的这一行:

convertView = mInflater.inflate(R.layout.list_adapter_view, null);
我做错了什么

这是我的片段:

public class MyOrderFragment extends Fragment {
    TextView name;
    ListView listOrder;
    private Context context;
    List<cources> collegeList;

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);


    }
    @Subscribe
    public void onMessageEvent(SendDataHelper event){

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
        name = (TextView) rootView.findViewById(R.id.textView);

        listOrder = (ListView) rootView.findViewById(R.id.listOrder);

        ArrayList<cources> listContact = GetlistContact();
        ListviewContactAdapter ad = new ListviewContactAdapter(context,listContact);
        listOrder.setAdapter(ad);

        return rootView;
    }

    private ArrayList<cources> GetlistContact(){
        ArrayList<cources> contactlist = new ArrayList<cources>();

        cources contact = new cources();

        contact.name="sharon";
        contact.price_plate="8";
        contactlist.add(contact);

        contact = new cources();
        contact.name = "sharon2";
        contact.price_plate = "10";
        contactlist.add(contact);

        contact = new cources();
        contact.name = "sheli";
        contact.price_plate="85";
        contactlist.add(contact);

        return contactlist;
    }
}
这是我的列表适配器视图的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white">

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Tx_name_planche"
    tools:ignore="HardcodedText"
    android:textSize="24sp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Tx_price_baget"
    tools:ignore="HardcodedText"
    android:textSize="18sp"
    android:layout_marginRight="38dp"
    android:layout_marginEnd="50dp"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/Tx_price_plate"
    android:layout_toStartOf="@+id/Tx_price_plate" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Tx_price_plate"
    tools:ignore="HardcodedText"
    android:textSize="18sp"
    android:layout_marginRight="31dp"
    android:layout_marginEnd="31dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>

在构造函数列表ViewContactAdapter中,您需要初始化mInflate变量:

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

在构造函数ListviewContactAdapter中,需要初始化mInflate变量:

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

现在,您的行使我出错:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”。将适配器创建替换为:
ListviewContactAdapter ad=new ListviewContactAdapter(getActivity(),listContact)
Omg you myh hero HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH举个例子。@Lethervega Ok现在我明白了,谢谢你的这句话让我出错:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”。将适配器创建替换为:
ListviewContactAdapter ad=new ListviewContactAdapter(getActivity(),listContact);
Omg you myh hero HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH“context=getActivity()”举例说明。@Lethervega现在我明白了,谢谢
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)