在除main之外的其他活动中使用的上下文,在数据库操作中给出空指针异常 @覆盖 创建时的公共void(Bundle savedInstanceState){ /*Intent z=新的Intent(这个,HomePage.class); startActivityForResult(z,请求代码)*/ super.onCreate(savedInstanceState); setContentView(R.layout.main); ctx=这个; 数据源=新注释数据源(此); datasource.open(); 刷新(); } 私有无效刷新(){ 列表值=datasource.getAllComments(); 列表值_dif=新的ArrayList(); 对于(int i=0;icomment.getToTake()) comment.setComment(comment.getComment()+“”+(comment.getToGive()-comment.getToTake()); 其他的 comment.setComment(comment.getComment()+“”+(comment.getToTake()-comment.getToGive()); 增加价值(i,评论); } ArrayAdapter=新的ArrayAdapter(此, android.R.layout.simple_list_item_1,value_dif); setListAdapter(适配器); ListView ListView=(ListView)findViewById(android.R.id.list); setAdapter(新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,value\u dif){ @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ TextView TextView=(TextView)super.getView(position,convertView,parent); 列表值=datasource.getAllComments(); Comment Comment=datasource.getHisaab(values.get(position.getComment()); if(comment.getToGive()>comment.getToTake()){ textView.setTextColor(Color.RED); } 否则{ textView.setTextColor(Color.GREEN); } /*String currentLocation=RouteFinderBookmarksActivity.this.getResources().getString(R.String.Current_位置); int textColor=textView.getText().toString().equals(当前位置)?R.color.holo_蓝色:R.color.text_颜色_btn_holo_深色; textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor))*/ 返回文本视图; } }); }

在除main之外的其他活动中使用的上下文,在数据库操作中给出空指针异常 @覆盖 创建时的公共void(Bundle savedInstanceState){ /*Intent z=新的Intent(这个,HomePage.class); startActivityForResult(z,请求代码)*/ super.onCreate(savedInstanceState); setContentView(R.layout.main); ctx=这个; 数据源=新注释数据源(此); datasource.open(); 刷新(); } 私有无效刷新(){ 列表值=datasource.getAllComments(); 列表值_dif=新的ArrayList(); 对于(int i=0;icomment.getToTake()) comment.setComment(comment.getComment()+“”+(comment.getToGive()-comment.getToTake()); 其他的 comment.setComment(comment.getComment()+“”+(comment.getToTake()-comment.getToGive()); 增加价值(i,评论); } ArrayAdapter=新的ArrayAdapter(此, android.R.layout.simple_list_item_1,value_dif); setListAdapter(适配器); ListView ListView=(ListView)findViewById(android.R.id.list); setAdapter(新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,value\u dif){ @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ TextView TextView=(TextView)super.getView(position,convertView,parent); 列表值=datasource.getAllComments(); Comment Comment=datasource.getHisaab(values.get(position.getComment()); if(comment.getToGive()>comment.getToTake()){ textView.setTextColor(Color.RED); } 否则{ textView.setTextColor(Color.GREEN); } /*String currentLocation=RouteFinderBookmarksActivity.this.getResources().getString(R.String.Current_位置); int textColor=textView.getText().toString().equals(当前位置)?R.color.holo_蓝色:R.color.text_颜色_btn_holo_深色; textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor))*/ 返回文本视图; } }); },android,android-context,Android,Android Context,如果我在其他活动中使用数据库操作,再次创建数据源对象,并提供上下文,则显示错误……如果我在该活动中执行数据库操作,则一切运行正常……初始化ListView ListView=(ListView)findViewById(android.R.id.list)行。尝试使用应用程序的上下文而不是活动的上下文:将这个替换为这个。getApplicationContext()谢谢大家的回答……我犯了一个愚蠢的错误……我没有打开数据库-| @Override public void onCreate(Bun

如果我在其他活动中使用数据库操作,再次创建数据源对象,并提供上下文,则显示错误……如果我在该活动中执行数据库操作,则一切运行正常……

初始化
ListView ListView=(ListView)findViewById(android.R.id.list)onCreate
中或在
setListAdapter(适配器)之前使用code>refresh()
方法中的code>行。

尝试使用应用程序的上下文而不是活动的上下文:将
这个
替换为
这个。getApplicationContext()

谢谢大家的回答……我犯了一个愚蠢的错误……我没有打开数据库-|
@Override
public void onCreate(Bundle savedInstanceState) {

    /*Intent z=new Intent(this, HomePage.class);
    startActivityForResult(z, REQUEST_CODE);*/

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ctx = this;
    datasource = new CommentsDataSource(this);
    datasource.open();
    refresh();
}
private void refresh(){
    List<Comment> values = datasource.getAllComments();
    List<Comment> value_dif= new ArrayList<Comment>();
    for(int i=0;i<values.size();i++){
        Comment comment=new Comment();
        comment=values.get(i);
        if(comment.getToGive()>comment.getToTake())
            comment.setComment(comment.getComment()+" "+(comment.getToGive()-comment.getToTake()));
        else
            comment.setComment(comment.getComment()+" "+(comment.getToTake()-comment.getToGive()));
            value_dif.add(i,comment);
    }
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, value_dif);
    setListAdapter(adapter);



    ListView listView = (ListView)findViewById(android.R.id.list);
    listView.setAdapter(new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, value_dif) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) super.getView(position, convertView, parent);
            List<Comment> values = datasource.getAllComments();
            Comment comment=datasource.getHisaab(values.get(position).getComment());
            if(comment.getToGive()>comment.getToTake()){
                textView.setTextColor(Color.RED);
            }
            else{
                textView.setTextColor(Color.GREEN);
            }
            /*String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
            int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
            textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));*/
            return textView;
        }
    });

}