在从导航菜单项调用的片段上加载Listview时遇到问题

时间:2016-09-18 19:36:27

标签: android listview android-fragments

我是Android编程的新手。我在ListFragment上显示Listview时遇到困难。我已经在活动上尝试了它并且它工作得很好,但是,我尝试在从导航抽屉调用的Listfragment上显示相同的列表视图但我的应用程序在项目点击时崩溃。我已经使用了很长时间,尝试从不同的网站获得答案,但都无济于事。

请:一点帮助会很好,非常感激

logcat上显示的错误是:

RewriteCond %{REQUEST_FILENAME} !-d

content_main.xml

09-18 14:35:53.285
3602-3602/com.example.ceede.classwork E/AndroidRuntime: FATAL
EXCEPTION: main Process: com.example.ceede.classwork, PID: 3602       
java.lang.NullPointerException at                                     
com.example.ceede.classwork.MainActivity.onNavigationItemSelected(MainActivity.java:92)
at
android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:152)

CustomAdapter.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.ceede.classwork.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="android.support.v4.app.ListFragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

SingleRow.java

package com.example.ceede.classwork;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
    ArrayList<SingleRow> list;
    Context context;

    CustomAdapter(Context context) {
        list = new ArrayList<>();
        this.context = context;
        Resources res = context.getResources();
        String[] titles = res.getStringArray(R.array.Titles);
        int[] images = {R.drawable.admisntrationicon, R.drawable.articon, R.drawable.businesseducationicon,
                R.drawable.homeicon, R.drawable.drivericon, R.drawable.stafficon,
                R.drawable.counciloricon, R.drawable.physicalcon, R.drawable.englishicon,
                R.drawable.mathsicon, R.drawable.scienceicon, R.drawable.historyicon,
                R.drawable.suporticon};

        for (int i = 0; i < titles.length; i++) {
            list.add(new SingleRow(titles[i], images[i]));
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        SingleRow temp = list.get(i);
        View row = null;
        if(view==null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.single_row,viewGroup,false);
        }

        TextView titles= (TextView) row.findViewById(R.id.textAdmin);
        ImageView image= (ImageView)row.findViewById(R.id.adminImage);

        return row;
    }
}

MyFragmentClass.java

class SingleRow {

    public String titles;
    public int images;

    SingleRow(String titles, int images) {
        this.titles = titles;
        this.images = images;
    }
}

MainActivity

package com.example.ceede.classwork;

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

public class MyFirstFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.my_list_fragment,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ListView listView= (ListView) getActivity().findViewById(R.id.listView);
        listView.setAdapter(new CustomAdapter(getActivity()));


    }

    public static Fragment newInstance() {

        MyFirstFragment frag=new MyFirstFragment();

        return frag;
    }
}

}

2 个答案:

答案 0 :(得分:0)

您不应该初始化FragmentManager并在beginTransaction中致电onCreatebeginTransactioncommit应该配对。如果两次调用onNavigationItemSelected,则在事务已提交时遇到麻烦。

更好地以这种方式使用它:

if (id == R.id.contact) {
    FragmentManager manager=getSupportFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    // Handle the camera action
    transaction.replace(R.id.fragment, MyFirstFragment.newInstance());
    transaction.commit();
}

答案 1 :(得分:0)

试试这个:

使用

pd.read_sql

问题是您的列表视图附加到Fragment而不是Activity。