set adapter null pointer

时间:2017-04-07 13:36:42

标签: java android

我一直试图解决一个问题几个小时了,但是我没想弄清楚。我为可扩展列表视图尝试了一个设置适配器,但我接受了这个错误。谢谢。 “尝试在空对象引用上调用虚方法'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)'”

Main_Activity

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button b1;


    private ResideMenu resideMenu;
    private Context mContext;
    private ResideMenuItem itemAnasayfa;
    private ResideMenuItem itemRastgele;
    private ResideMenuItem itemEncok;
    private ResideMenuItem itemTarifyaz;

    private ExpandableListView listView;
    private ExpandableListAdapter listAdapter;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHash;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        listView=(ExpandableListView)findViewById(R.id.exp2);
        initData();
        listAdapter=new com.yeni.ExpandableListAdapter(this,listDataHeader,listHash);
        listView.setAdapter(listAdapter);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        setUpMenu();
        if (savedInstanceState == null) {

            changeFragment(new F_ana_sayfa());

        }

    }

    private void initData() {
        listDataHeader=new ArrayList<>();
        listHash=new HashMap<>();

        listDataHeader.add("et");
        listDataHeader.add("tavuk");
        listDataHeader.add("sebze");
        listDataHeader.add("diger");

        List<String> l_et=new ArrayList<>();
        l_et.add("kemikli");
        l_et.add("kuşbaşı");

        List<String> l_tavuk=new ArrayList<>();
        l_tavuk.add("bonfile");
        l_tavuk.add("but");
        l_tavuk.add("bütün tavuk");

        List<String> l_sebze=new ArrayList<>();
        l_sebze.add("patates");
        l_sebze.add("biber");
        l_sebze.add("kabak");
        l_sebze.add("patlıcan");

        List<String> l_diger=new ArrayList<>();
        l_diger.add("tuz");
        l_diger.add("yağ");
        l_diger.add("kekik");

        listHash.put(listDataHeader.get(0),l_et);
        listHash.put(listDataHeader.get(1),l_tavuk);
        listHash.put(listDataHeader.get(2),l_sebze);
        listHash.put(listDataHeader.get(3),l_diger);

    }

    private void setUpMenu() {
        resideMenu = new ResideMenu(this);
        resideMenu.setBackground(R.drawable.menu_background);
        resideMenu.attachToActivity(this);


        resideMenu.setMenuListener(menuListener);
        resideMenu.setScaleValue(0.6f);
        //create menu items;
        itemAnasayfa = new ResideMenuItem(this, R.drawable.icon_home, "Ana Sayfa");
        itemRastgele = new ResideMenuItem(this, R.drawable.icon_profile, "Random tarif bul");
        itemEncok = new ResideMenuItem(this, R.drawable.icon_calendar, "En cok begenilenler");
        itemTarifyaz = new ResideMenuItem(this, R.drawable.icon_settings, "tarif Yaz");


        itemAnasayfa.setOnClickListener(this);
        itemRastgele.setOnClickListener(this);
        itemEncok.setOnClickListener(this);
        itemTarifyaz.setOnClickListener(this);


        resideMenu.addMenuItem(itemAnasayfa, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemRastgele, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemEncok, ResideMenu.DIRECTION_RIGHT);
        resideMenu.addMenuItem(itemTarifyaz, ResideMenu.DIRECTION_RIGHT);

        findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
            }
        });

        findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
            }
        });
    }

    @Override

    public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.dispatchTouchEvent(ev);
    }

    @Override
    public void onClick(View view) {

        if (view == itemAnasayfa) {
            changeFragment(new F_ana_sayfa());
        } else if (view == itemRastgele) {
            changeFragment(new F_rastgele_tarif());
        } else if (view == itemEncok) {
            changeFragment(new F_encok_beg());
        } else if (view == itemTarifyaz) {
            changeFragment(new F_tarif_yaz());
        }
        else
            changeFragment(new F_ana_sayfa());


        resideMenu.closeMenu();
    }


    private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
        @Override
        public void openMenu() {
            Toast.makeText(mContext, "Menu is opened!", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void closeMenu() {

            Toast.makeText(mContext, "Menu is closed!", Toast.LENGTH_SHORT).show();
        }


    };




    private void changeFragment(Fragment targetFragment) {
        resideMenu.clearIgnoredViewList();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_fragment, targetFragment, "fragment")
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }

    public ResideMenu getResideMenu(){
        return resideMenu;}
}

适配器类

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context cont;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHashMap;

    public ExpandableListAdapter(Context cont, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
        this.cont = cont;
        this.listDataHeader = listDataHeader;
        this.listHashMap = listHashMap;
    }

    @Override
    public int getGroupCount() {
        return listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return listHashMap.get(listDataHeader.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return listDataHeader.get(i);
    }

    @Override
    public Object getChild(int i, int i1) { // i=group position i=item position
        return listHashMap.get(listDataHeader.get(i)).get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        String headerTitle=(String)getGroup(i);

        if (view==null){
            LayoutInflater inflater1=(LayoutInflater)this.cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater1.inflate(R.layout.list_group,null);

        }

        TextView lblHeader=(TextView)view.findViewById(R.id.header);
        lblHeader.setTypeface(null, Typeface.BOLD);
        lblHeader.setText(headerTitle);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final String childText=(String)getChild(i,i1);

        if (view==null){
            LayoutInflater inflater1=(LayoutInflater)this.cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater1.inflate(R.layout.list_item,null);

        }

        TextView txtListChild=(TextView)view.findViewById(R.id.list_item);
        txtListChild.setText(childText);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

Activity_Main XML

    <?xml version="1.0" encoding="utf-8"?>

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

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/exp2" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/layout_top">

            <!--<ImageView-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="3dp"-->
                <!--android:background="#2ea3fe"/>-->

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <Button
                    android:layout_width="28dp"
                    android:layout_height="28dp"
                    android:background="@drawable/titlebar_menu_selector"
                    android:id="@+id/title_bar_left_menu"
                    android:layout_gravity="left|center_vertical"
                    android:layout_marginLeft="10dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="7dp"
                    android:text="Yemek Do"
                    android:textSize="24sp"
                    android:layout_gravity="center"
                    android:textColor="?attr/actionModeSplitBackground" />

                <Button
                    android:layout_width="28dp"
                    android:layout_height="28dp"
                    android:background="@drawable/titlebar_menu_selector"
                    android:id="@+id/title_bar_right_menu"
                    android:layout_gravity="right|center_vertical"
                    android:layout_marginRight="10dp"/>
            </FrameLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="#bcb8b8"/>

        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/main_fragment">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <Button
                    android:text="Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="107dp"
                    android:layout_marginStart="107dp"
                    android:layout_marginTop="188dp"
                    android:id="@+id/button2" />

                <Button
                    android:text="Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignRight="@+id/button2"
                    android:layout_alignEnd="@+id/button2"
                    android:layout_marginTop="78dp"
                    android:id="@+id/button3" />
            </RelativeLayout>
        </FrameLayout>

    </LinearLayout>

list_group.xml

    <?xml version="1.0" encoding="utf-8"?>

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

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/exp2" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/layout_top">

            <!--<ImageView-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="3dp"-->
                <!--android:background="#2ea3fe"/>-->

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <Button
                    android:layout_width="28dp"
                    android:layout_height="28dp"
                    android:background="@drawable/titlebar_menu_selector"
                    android:id="@+id/title_bar_left_menu"
                    android:layout_gravity="left|center_vertical"
                    android:layout_marginLeft="10dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="7dp"
                    android:text="Yemek Do"
                    android:textSize="24sp"
                    android:layout_gravity="center"
                    android:textColor="?attr/actionModeSplitBackground" />

                <Button
                    android:layout_width="28dp"
                    android:layout_height="28dp"
                    android:background="@drawable/titlebar_menu_selector"
                    android:id="@+id/title_bar_right_menu"
                    android:layout_gravity="right|center_vertical"
                    android:layout_marginRight="10dp"/>
            </FrameLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="#bcb8b8"/>

        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/main_fragment">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <Button
                    android:text="Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="107dp"
                    android:layout_marginStart="107dp"
                    android:layout_marginTop="188dp"
                    android:id="@+id/button2" />

                <Button
                    android:text="Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignRight="@+id/button2"
                    android:layout_alignEnd="@+id/button2"
                    android:layout_marginTop="78dp"
                    android:id="@+id/button3" />
            </RelativeLayout>
        </FrameLayout>

    </LinearLayout>

list_item.xml

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

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/header" />
    </LinearLayout>

error_log中

  

E / AndroidRuntime:致命异常:主要                     过程:com.yeni,PID:2404                     java.lang.RuntimeException:无法启动活动ComponentInfo {com.yeni / com.yeni.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)”   在null对象引用上                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                      引起:java.lang.NullPointerException:尝试调用虚方法'void   android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)”   在null对象引用上                         在com.yeni.MainActivity.onCreate(MainActivity.java:55)                         在android.app.Activity.performCreate(Activity.java:6662)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:1)

将onCreate方法更改为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = this;
    setUpMenu();
    if (savedInstanceState == null) {

        changeFragment(new F_ana_sayfa());

    }

  listView=(ExpandableListView)findViewById(R.id.exp2);
    initData();
    listAdapter=new com.yeni.ExpandableListAdapter(this,listDataHeader,listHash);
    listView.setAdapter(listAdapter);

}

答案 1 :(得分:1)

你在做什么:

 listView=(ExpandableListView)findViewById(R.id.exp2);
initData();
listAdapter=new com.yeni.ExpandableListAdapter(this,listDataHeader,listHash);
listView.setAdapter(listAdapter);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

在setContentView错误之前初始化listview对象。它应该是这样的

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ExpandableListView)findViewById(R.id.exp2);
initData();
listAdapter=new 
          com.yeni.ExpandableListAdapter(this,listDataHeader,listHash);
listView.setAdapter(listAdapter);