English 中文(简体)
昂儒昂岛
原标题: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

我尝试:

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

或。

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

but both ways give me the same error on start.. My Class extends ListActivity

我怎么办? 预先感谢

FUL 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 such as You view R.id.stato wasn t found and so sts = 无。 检查sts不是无效的,或者说你需要补充的法典

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

您可以这样说:

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

我希望你们能提出我的想法。

问题回答

在你试图弄清内容之前,你是否提出内容观点? 在你能够从中获取意见之前,你需要在其活动中提出内容观点。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签