model对象为null

时间:2017-10-02 21:24:45

标签: android oop android-togglebutton

我正在尝试通过切换按钮单击来保存列表项。 当我点击切换按钮我的应用程序崩溃,我调试我的应用程序,发现每次模型对象为空! 任何人都可以告诉我为什么它是空的?我怎么能解决这个问题? 我试图处理切换按钮单击适配器类,但徒劳无功

这是myAdapter.java

package com.example.remon.my_app2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.remon.my_app2.models.CatModel;

import java.util.ArrayList;

/**
 * Created by Remon on 9/27/2017.
 */

public class myAdapter extends BaseAdapter{

    ArrayList<CatModel> catModels;
    Context mContext;


    ToggleButton btnTglFavourite;
    DatabaseHelper databaseHelper;
    CatModel catModel;
    public myAdapter(Context context,ArrayList<CatModel>catModels){
        this.mContext=context;
        this.catModels=catModels;

    }
    @Override
    public int getCount() {
        return catModels == null ? 0 : catModels.size();
    }

    @Override
    public Object getItem(int position) {
        return catModels.get(position);
    }

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

    @Override
    public View getView(int position, View ConvertedView, ViewGroup viewGroup) {
        View view=ConvertedView;
        if (view==null)
        {
            LayoutInflater inflater=(LayoutInflater)
                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.recycler_1_items,viewGroup,false);

            CatModel catModel = (CatModel)getItem(position);
            TextView txtCatName = (TextView)view.findViewById(R.id.txtCat);
             txtCatName.setText(catModel.getCatName());
        }
        btnTglFavourite = (ToggleButton)view.findViewById(R.id.favoriteToggleButton);
//            btnTglFavourite.setChecked(false);
//            if (databaseHelper.check(catModelss.getCatName())){
//                btnTglFavourite.setChecked(true);
//            }
        btnTglFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!databaseHelper.check(catModel.getCatName())){  //////object is null !!
                    databaseHelper.insert(catModel.getCatName());
                    Toast.makeText(mContext,"in database: " +catModel.getCatName(),Toast.LENGTH_SHORT).show();
                }
                else {databaseHelper.deleteRow(catModel.getCatName());}
            }
        });
        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

您正在使用的catmodel

    if (!databaseHelper.check(catModel.getCatName())){
                databaseHelper.insert(catModel.getCatName());
                Toast.makeText(mContext,"in database: "+catModel.getCatName(),Toast.LENGTH_SHORT).show();
    }
    else {databaseHelper.deleteRow(catModel.getCatName());}

是从未初始化的全局/字段引用。

解决方案应该是

    public View getView(int position, View ConvertedView, ViewGroup viewGroup) {
        final CatModel catModel = (CatModel)getItem(position);
        View view=ConvertedView;
        if (view==null){
            LayoutInflater inflater=(LayoutInflater)
                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.recycler_1_items,viewGroup,false);

            TextView txtCatName = (TextView)view.findViewById(R.id.txtCat);
             txtCatName.setText(catModel.getCatName());
        }
        btnTglFavourite = (ToggleButton)view.findViewById(R.id.favoriteToggleButton);
//            btnTglFavourite.setChecked(false);
//            if (databaseHelper.check(catModelss.getCatName())){
//                btnTglFavourite.setChecked(true);
//            }
        btnTglFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!databaseHelper.check(catModel.getCatName())){  //////object is null !!
                    databaseHelper.insert(catModel.getCatName());
                    Toast.makeText(mContext,"in database: " +catModel.getCatName(),Toast.LENGTH_SHORT).show();
                }
                else {databaseHelper.deleteRow(catModel.getCatName());}
            }
        });
        return view;
    }

答案 1 :(得分:0)

试试这个

package com.example.remon.my_app2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.remon.my_app2.models.CatModel;

import java.util.ArrayList;

/**
 * Created by Remon on 9/27/2017.
 */

public class myAdapter extends BaseAdapter{

    ArrayList<CatModel> catModels;
    Context mContext;


    ToggleButton btnTglFavourite;
    DatabaseHelper databaseHelper;
    CatModel catModel;
    public myAdapter(Context context,ArrayList<CatModel>catModels){
        this.mContext=context;
        this.catModels=catModels;

    }
    @Override
    public int getCount() {
        return catModels == null ? 0 : catModels.size();
    }

    @Override
    public Object getItem(int position) {
        return catModels.get(position);
    }

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

    @Override
    public View getView(int position, View ConvertedView, ViewGroup viewGroup) {
        View view=ConvertedView;
        if (view==null)
        {
            LayoutInflater inflater=(LayoutInflater)
                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.recycler_1_items,viewGroup,false);


            TextView txtCatName = (TextView)view.findViewById(R.id.txtCat);
             txtCatName.setText(catModels.get(position).getCatName());
        }
        btnTglFavourite = (ToggleButton)view.findViewById(R.id.favoriteToggleButton);
//            btnTglFavourite.setChecked(false);
//            if (databaseHelper.check(catModelss.getCatName())){
//                btnTglFavourite.setChecked(true);
//            }
        btnTglFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!databaseHelper.check(catModels.get(position).getCatName())){  //////object is null !!
                    databaseHelper.insert(catModels.get(position).getCatName());
                    Toast.makeText(mContext,"in database: " +catModels.get(position).getCatName(),Toast.LENGTH_SHORT).show();
                }
                else {databaseHelper.deleteRow(catModels.get(position).getCatName());}
            }
        });
        return view;
    }
}