0
@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;
        }
    });

}

このようにコンテキストを指定してデータソースオブジェクトを再度作成することにより、他のアクティビティでデータベース操作を使用すると、エラーが表示されます......このアクティビティでデータベース操作を実行すると、すべてが正常に実行されます。

4

2 に答える 2

0

メソッドの行または行の前にを初期化ListView listView = (ListView)findViewById(android.R.id.list);します。onCreatesetListAdapter(adapter);refresh()

于 2013-01-21T05:00:13.263 に答える
0

アクティビティのコンテキストではなく、アプリケーションのコンテキストで試してください: で置き換えthisますthis.getApplicationContext()

于 2013-01-21T05:02:05.597 に答える