Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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 尝试调用虚拟方法';int io.realm.RealmResults.size()';关于空对象引用_Android - Fatal编程技术网

Android 尝试调用虚拟方法';int io.realm.RealmResults.size()';关于空对象引用

Android 尝试调用虚拟方法';int io.realm.RealmResults.size()';关于空对象引用,android,Android,在适配器类中: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int io.realm.RealmResults.size()' on a null object reference at com.subhasishlive.goal

在适配器类中:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int io.realm.RealmResults.size()' on a null object reference
                                                                               at com.subhasishlive.goalDiary.adapters.AdapterGoals.getItemCount(AdapterGoals.java:59)
                                                                               at com.subhasishlive.goalDiary.widgets.GoalRecyclerView.toggleViews(GoalRecyclerView.java:70)
                                                                               at com.subhasishlive.goalDiary.widgets.GoalRecyclerView.access$000(GoalRecyclerView.java:21)
                                                                               at com.subhasishlive.goalDiary.widgets.GoalRecyclerView$1.onChanged(GoalRecyclerView.java:35)
                                                                               at com.subhasishlive.goalDiary.widgets.GoalRecyclerView.setAdapter(GoalRecyclerView.java:109)
                                                                               at com.subhasishlive.goalDiary.Activitymain.onCreate(Activitymain.java:81)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
这就是错误发生的地方

在ActivityMain.java中,我已经像这样实例化了: mRealm=Realm.getDefaultInstance()

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRealm=Realm.getDefaultInstance();
/**
*RealmResults是数组列表的一种特殊类型。
*能够管理领域数据库中的数据。。。
*/
RealmResults results=mRealm.where(Goal.class).findalsync();
mToolbar=(工具栏)findviewbyd(R.id.Toolbar);
mEmptyView=(视图)findViewById(R.id.empty\u目标);
mBtnAdd=(按钮)findviewbyd(R.id.btn_add);
mRecycler=(GoalRecyclerView)findViewById(R.id.rv_目标);
mRecycler.hideIfEmpty(mToolbar);
mRecycler.showIfEmpty(mEmptyView);
//正在为mRecycler RecyclerView设置布局管理器。。。。
LinearLayoutManager=新的LinearLayoutManager(此);
mRecycler.setLayoutManager(管理器);
//现在我已经在回收器视图上设置了适配器。。。
//通过调用setAdapter方法。
//将适配器类的新实例作为参数传递。
//在实例化适配器类时,传递此当前上下文
//作为论据。。。。
mAdapter=新适配器(此,mResults);
mRecycler.setAdapter(mAdapter);
setOnClickListener(mBtnAddListener);
设置支持操作栏(mToolbar);
initBackgroundImage();
} 
我的适配器类,其中定义了mReasults: 在update()内部,它被初始化为传递的参数。 那它怎么还能是空的

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRealm = Realm.getDefaultInstance();
        /**
         * RealmResults is a special typer of array list.
         * which is capable of managing data from realm database...
         */
        RealmResults<Goal> results = mRealm.where(Goal.class).findAllAsync();
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mEmptyView = (View) findViewById(R.id.empty_goals);
        mBtnAdd = (Button) findViewById(R.id.btn_add);
        mRecycler = (GoalRecyclerView) findViewById(R.id.rv_goals);
        mRecycler.hideIfEmpty(mToolbar);
        mRecycler.showIfEmpty(mEmptyView);
        // setting up layout manager for mRecycler RecyclerView....
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecycler.setLayoutManager(manager);
        // now I have set adapter on recycler view...
        // by calling setAdapter method.
        // passing new instance of my adapter class as argument.
        // and while instanciating the adapter class, passing this present context
        // as argument....
        mAdapter = new AdapterGoals(this, mResults);
        mRecycler.setAdapter(mAdapter);
        mBtnAdd.setOnClickListener(mBtnAddListener);
        setSupportActionBar(mToolbar);
        initBackgroundImage();
    } 
包com.subbasishlive.goalDiary.adapters;
导入android.content.Context;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
进口com.subbasishlive.goalDiary.R;
导入com.subbasishlive.goalDiary.beans.Goal;
导入java.util.ArrayList;
导入io.realm.RealmResults;
/**
*由Subbasishnath于2018年5月3日创建。
*/
公共类AdapterGoals扩展了RecyclerView.Adapter{
私人停车场;
//正在创建RealmResult数组类型实例变量,
//可以保存目标类型的RealmObjects。。。
私有RealmResults mResults;
公共静态最终字符串TAG=“subbasish”;
公共AdapterGoals(上下文,RealmResults结果){//在参数化构造函数中,
mInflater=LayoutInflater.from(上下文);
//mReasults=结果;
更新(结果);
}
//已创建公共方法更新,该更新采用RealmResults类型数组。。。
公共作废更新(RealmResults结果){
mReasults=结果;
//TODO添加新目标后不更新列表…从视频(067显示适配器内的数据…)
this.notifyDataSetChanged();
}
//此方法返回RecyclerView.ViewHolder
@凌驾
公共GoalHolder onCreateViewHolder(视图组父级,int-viewType){
View=mInflater.inflate(R.layout.row\u目标,父项,false);
球门支架=新球门支架(视图);
Log.d(标记“onCreateViewHolder:”);
报税表持有人;
}
//onCreateViewHolder()方法返回的RecyclerView.ViewHolder为
//在onBindViewHolder()类中作为参数传递。。。
@凌驾
BindViewHolder上的公共无效(GoalHolder holder,内部位置){
目标=mReasults.get(位置);
holder.mTextWhat.setText(goal.getWhat());
Log.d(标签“onBindViewHolder:+位置);
}
@凌驾
public int getItemCount(){
返回mReasults.size();
}
//创建自定义类
公共静态类GoalHolder扩展了RecyclerView.ViewHolder{
text查看mTextWhat;
//参数化构造函数,将视图作为参数。。。
公共目标文件夹(查看项目视图){
超级(项目视图);
mTextWhat=(TextView)itemView.findViewById(R.id.tv_what);
}
}
}

感谢您的帮助:)

我重新分配了mRealm=Realm.getDefaultInstance();从getCount()内部开始,现在它可以工作了:)

@覆盖
public int getItemCount(){
领域mRealm;
mRealm=Realm.getDefaultInstance();
RealmResults mReasults=mRealm.where(Goal.class).findalsync();
返回mReasults.size();
}

@Override public int getItemCount(){return mReasults.size();}不确定这里的问题是什么:您正在尝试访问mReasults上的大小,日志告诉您该大小为空。可能的重复项需要在此处输入更多详细信息。你的原木猫什么也没告诉我们。尝试编辑添加代码我们需要查看在适配器类中设置mReasults的位置,因为这是null。
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRealm = Realm.getDefaultInstance();
        /**
         * RealmResults is a special typer of array list.
         * which is capable of managing data from realm database...
         */
        RealmResults<Goal> results = mRealm.where(Goal.class).findAllAsync();
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mEmptyView = (View) findViewById(R.id.empty_goals);
        mBtnAdd = (Button) findViewById(R.id.btn_add);
        mRecycler = (GoalRecyclerView) findViewById(R.id.rv_goals);
        mRecycler.hideIfEmpty(mToolbar);
        mRecycler.showIfEmpty(mEmptyView);
        // setting up layout manager for mRecycler RecyclerView....
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecycler.setLayoutManager(manager);
        // now I have set adapter on recycler view...
        // by calling setAdapter method.
        // passing new instance of my adapter class as argument.
        // and while instanciating the adapter class, passing this present context
        // as argument....
        mAdapter = new AdapterGoals(this, mResults);
        mRecycler.setAdapter(mAdapter);
        mBtnAdd.setOnClickListener(mBtnAddListener);
        setSupportActionBar(mToolbar);
        initBackgroundImage();
    } 
package com.subhasishlive.goalDiary.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.subhasishlive.goalDiary.R;
import com.subhasishlive.goalDiary.beans.Goal;

import java.util.ArrayList;

import io.realm.RealmResults;

/**
 * Created by SubhasishNath on 5/3/2018.
 */

public class AdapterGoals extends RecyclerView.Adapter<AdapterGoals.GoalHolder>{
    private LayoutInflater mInflater;
    // creating RealmResult array type instance variable,
    // that can hold Goal type RealmObjects...
    private RealmResults<Goal> mReasults;
    public static final String TAG = "SUBHASISH";
    public AdapterGoals(Context context,RealmResults<Goal> results){// Inside parameterized constructor,
        mInflater = LayoutInflater.from(context);
        //mReasults = results;
        update(results);
    }
    // created the public method update,which takes a RealmResults type array...
    public void update(RealmResults<Goal> results) {
        mReasults = results;
        // TODO not updating the list after adding new goal...from video (067 show data inside adapter...)
        this.notifyDataSetChanged();
    }
    // this method returns RecyclerView.ViewHolder
    @Override
    public GoalHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view = mInflater.inflate(R.layout.row_goals,parent,false);
        GoalHolder holder = new GoalHolder(view);
        Log.d(TAG, "onCreateViewHolder: ");
        return holder;
    }
    // the returned RecyclerView.ViewHolder from onCreateViewHolder() method is
    // passed as parameter in onBindViewHolder() class...
    @Override
    public void onBindViewHolder(GoalHolder holder, int position) {
        Goal goal = mReasults.get(position);
        holder.mTextWhat.setText(goal.getWhat());
        Log.d(TAG, "onBindViewHolder: "+ position);
    }


    @Override
    public int getItemCount() {
        return mReasults.size();
    }
    // creating custom class
    public static class GoalHolder extends RecyclerView.ViewHolder{
        TextView mTextWhat;
        // parameterized constructor, that takes a View as argument...
        public GoalHolder(View itemView) {
            super(itemView);
            mTextWhat = (TextView) itemView.findViewById(R.id.tv_what);
        }
    }



}
@Override
public int getItemCount() {
    Realm mRealm;
    mRealm = Realm.getDefaultInstance();
    RealmResults<Goal> mReasults = mRealm.where(Goal.class).findAllAsync();
    return mReasults.size();
}