0

少し助けが必要です。Javaコードでテキストビューの色を変更する必要があります..別の方法を試しましたが、常にエラーが発生します:

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

2 に答える 2

1

ビュー 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);
    }
}

あなたが私の考えを理解してくれることを願っています

于 2012-04-13T19:51:10.240 に答える
0

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.

于 2012-04-13T19:54:59.800 に答える