简体   繁体   English

Android:java TextView颜色

[英]Android: java TextView Color

I need a little help. 我需要一点帮助。 I need change the color of a text view with java code.. I tried different way but it always gives me an error: 我需要使用Java代码更改文本视图的颜色。我尝试了不同的方法,但它总是给我一个错误:

java.lang.NullPointerException

I tried: 我试过了:

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

or.. 要么..

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

but both ways give me the same error on start.. My Class extends ListActivity 但是两种方式都会在启动时给我相同的错误。我的类扩展了ListActivity

how do I? 我如何? thanks in advance 提前致谢

FULL CODE: 完整代码:

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);

Seems like your view R.id.stato wasn't found and so sts == null . 似乎未找到您的视图R.id.stato,因此sts == null Try check that sts is not null, or some your additional code needed 尝试检查sts不为null,或者需要一些其他代码

EDIT: 编辑:

In case when you cannot access layout of your item in list instead of doing 如果您无法访问列表中项目的布局而不是这样做

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

you can write your own adapter like this: 您可以这样编写自己的适配器:

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

I hope you got my idea 我希望你有我的主意

Are you setting the content view before you try to get sts? 您是否在尝试获取sts之前设置了内容视图? You need to have the content view set in your activity before you can retrieve views from it. 您需要先在活动中设置内容视图,然后才能从中检索视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM