Android:java文本视图颜色
我需要一点帮助。 我需要用java代码更改文本视图的颜色。。 我尝试了不同的方法,但它总是给我一个错误:Android:java文本视图颜色,java,android,colors,textview,Java,Android,Colors,Textview,我需要一点帮助。 我需要用java代码更改文本视图的颜色。。 我尝试了不同的方法,但它总是给我一个错误: java.lang.NullPointerException 我试过: TextView sts; sts = (TextView) findViewById(R.id.stato); sts.setTextColor(Color.rgb(12,255,0)); 或者 但两者都有 在开始时给我同样的错误。。 我的类扩展了ListActivity 我该怎么办?提前谢谢 完整代码: publ
java.lang.NullPointerException
我试过:
TextView sts;
sts = (TextView) findViewById(R.id.stato);
sts.setTextColor(Color.rgb(12,255,0));
或者
但两者都有
在开始时给我同样的错误。。
我的类扩展了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);
似乎找不到视图R.id.stato,因此sts==null。尝试检查sts是否为null,或者是否需要一些附加代码 编辑: 如果您无法访问列表中项目的布局 而不是做
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);
}
}
我希望你了解我的想法在尝试获取sts之前,你是否设置了内容视图?您需要先在活动中设置内容视图,然后才能从中检索视图。在按id获取textview之前,您是否正在膨胀布局?在尝试设置颜色时,sts很可能为空。您确定正在为您所处的活动设置内容视图吗?R.id.stato为空,因为它从数据库获取数据…这可能是问题所在吗?还需要一些代码来了解您在何处搜索此视图,这将显示您在设置sts值之前是否设置了ContentView,如@dwemthy已经提到的put
super.onCreate(savedInstanceState);setContentView(R.layout.main2);
在onCreate方法的开头。找不到sts,因为您的活动的内容未设置。此R.id.stato文本视图是否存在于您的R.layout.main2布局中?可能我们应该查看它否,R.id.stato存在于另一个xml文件的另一布局中
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);
}
}