![](/img/trans.png)
[英]Changing Color with LinearLayout and TextView in Java (Android)
[英]Android: java TextView Color
我需要一點幫助。 我需要使用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);
似乎未找到您的視圖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之前設置了內容視圖? 您需要先在活動中設置內容視圖,然后才能從中檢索視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.