简体   繁体   中英

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.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

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 . Try check that sts is not null, or some your additional code needed

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? You need to have the content view set in your activity before you can retrieve views from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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