我的佈局XML具有Tablelayout與TextViews的細胞:的TextView沒有初始化
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="Style Details..."
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="Style"
android:padding="3dip" />
<TextView
android:id="@+id/StyleName"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="Price (Euro)"
android:padding="3dip" />
<TextView
android:id="@+id/StylePrice"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="Price for Volume <60<90 pcs"
android:padding="3dip" />
<TextView
android:id="@+id/PriceVolume6090"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="Price for Volume <100<250pcs"
android:padding="3dip" />
<TextView
android:id="@+id/PriceVoume100250"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="Price for Volume <251<500pcs"
android:padding="3dip" />
<TextView
android:id="@+id/PriceVoume251500"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="Price for Volume <501pcs"
android:padding="3dip" />
<TextView
android:id="@+id/PriceVolume501"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
我想通過我的活動來設置StyleName和值。以下是我正在使用的代碼:
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class StyleDetailsActivity extends Activity {
private TextView tvStyleName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.styledetailsview);
String styleName = "Testing Style Name";
tvStyleName = (TextView)findViewById(R.id.StyleName);
tvStyleName.setText(styleName);
}
}
代碼編譯正常,應用程序啓動。在運行應用程序時,我得到一個空指針異常。當我調試時,我看到tvStyleName爲空。任何建議,爲什麼它不會初始化
您確定對setContentView()的調用正在使用適當的佈局資源嗎? setContentView()應該設置爲包含TableLayout的佈局。 – McStretch 2010-09-21 18:23:05
是的,佈局是在styledetailsview.xml中定義的,而setContentView的代碼是setContentView(R.layout.styledetailsview); – user454284 2010-09-21 18:26:03
如果您註釋掉該行,您的活動是否正確顯示?你沒有設置任何android:layout_width或android:layout_height值,所以我不確定它能夠繪製這些文本,所以你不能得到對它們的引用。 – Mike 2010-09-21 18:32:43