gpt4 book ai didi

java - Android:java TextView颜色

转载 作者:行者123 更新时间:2023-12-01 15:25:53 25 4
gpt4 key购买 nike

我需要一点帮助。我需要用java代码改变 TextView 的颜色。我尝试了不同的方法,但它总是给我一个错误:

java.lang.NullPointerException

我尝试过:

TextView sts;
sts = (TextView) findViewById(R.id.stato);
sts.setTextColor(Color.rgb(12,255,0));

或者..

sts.setTextColor(android.graphics.Color.GREEN);

但双向开始时给我同样的错误..我的类扩展了 ListActivity

我该怎么办?提前致谢

完整代码:

public class ProvaDatabase extends ListActivity{

TextView sts;
private DatabaseHelper databaseHelper;

@Override
public void onCreate(Bundle savedInstanceState)
{
sts = (TextView) findViewById(R.id.stato);
sts.setTextColor(android.graphics.Color.GREEN);
//sts.setTextColor(Color.rgb(12,255,0));

super.onCreate(savedInstanceState);
databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
databaseHelper.insertdb(db, "sta", "25/11/2016", "SUCCESS", null, null, null);
databaseHelper.insertdb(db, "ssis", "25/11/2016", "WAITING", null, null, null);
databaseHelper.insertdb(db, "AAAAAAAA", "25/11/2016", "FAILED", null, null, null);
databaseHelper.insertdb(db, "BBBB", "bBBBBBB", "SUCCESS", null, null, null);
Cursor c = databaseHelper.getDB();
startManagingCursor(c);
setListAdapter(new SimpleCursorAdapter(this, R.layout.registryvista, c, new String[]
{ TabellaRegistry.TYPE, TabellaRegistry.DATE, TabellaRegistry.STATUS }, new int[]
{ R.id.tipo, R.id.data, R.id.stato }));


setContentView(R.layout.main2);

最佳答案

似乎未找到您的 View R.id.stato,因此sts == null。尝试检查 sts 是否不为空,或者需要一些额外的代码

编辑:

如果您无法访问列表中项目的布局而不是做

setListAdapter(new SimpleCursorAdapter(this, R.layout.registryvista, c, new String[]
{ TabellaRegistry.TYPE, TabellaRegistry.DATE, TabellaRegistry.STATUS }, new int[]
{ R.id.tipo, R.id.data, R.id.stato }));

你可以像这样编写自己的适配器:

public class MyAdapter extends SimpleCursorAdapter
{
//here goes methods that should be overriden

//and method getView in which we can access layout of our list item
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View view = super.getView(postition, convertView, parent);
TextView sts = (TextView) view.findViewById(R.id.stato);
sts.setTextColor(android.graphics.Color.GREEN);
}
}

希望你明白我的想法

关于java - Android:java TextView颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com