我有这样的 Android Activity 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#184A64">
<ImageView
android:id="@+id/logoTop"
android:layout_width="fill_parent"
android:layout_height="42dip"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:background="#F1CF2F"
android:contentDescription="@string/descLogoTop"
android:padding="4dip"
android:src="@drawable/logoTop" />
<ImageView
android:id="@+id/logoBot"
android:layout_width="fill_parent"
android:layout_height="42dip"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#F1CF2F"
android:contentDescription="@string/descLogoBot"
android:padding="4dip"
android:src="@drawable/logoBot" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip" >
<TextView
android:id="@+id/data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text=""
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="normal" />
<ImageView
android:id="@+id/icon"
android:layout_width="103dip"
android:layout_height="62dip"
android:layout_marginBottom="6dip"
android:layout_marginRight="6dip"
android:contentDescription="@string/descLogo"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:textColor="#F1CF2F"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/bottomtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text=""
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>
这个 xml 定义了一个布局,其中顶部矩形带有一个日志,底部矩形带有一个徽标。这两个元素显示正确。但是中间的组件是一个线性布局,有一个 TextView,然后是一个 Image,然后是 2 个 TextView。该组件不显示。活动的 Java 代码如下所示:
public class NewsActivity extends Activity {
private ProgressDialog m_ProgressDialog = null;
private Runnable viewNews;
private Bundle bundle;
private News m_news = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_activity);
bundle = this.getIntent().getExtras();
viewNews = new Runnable(){
@Override
public void run() {
getNews();
}
};
Thread thread = new Thread(null, viewNews, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(NewsActivity.this,
this.getString(R.string.wait), this.getString(R.string.receiving, true));
}
public void getNews() {
m_news = new News();
m_news.setNewsTitle(bundle.getString("title"));
m_news.setNewsText(bundle.getString("text"));
m_news.setNewsDate(bundle.getString("date"));
m_news.setNewsPhoto(bundle.getString("photo"));
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.activity_news_activity, null);
try{
ImageView iv = (ImageView) v.findViewById(R.id.icon);
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (iv != null) {
try{
Bitmap bmp = Utils.getBitmapFromURL("http://www.myServer.com/IMG/" + m_news.getNewsPhoto());
if (bmp == null){
Log.i("PHOTO ERROR", "http://www.myServer.com/IMG/" + m_news.getNewsPhoto());
}
iv.setImageBitmap(bmp);
} catch (Exception e){
Log.e("IMAGE ERROR", e.getMessage() + " - " + e.getStackTrace());
}
}
if (tt != null) {
tt.setText(m_news.getNewsTitle());
}
if(bt != null){
bt.setText(m_news.getNewsText());
}
TextView dt = (TextView) v.findViewById(R.id.data);
Date nd = new Date(Long.parseLong(m_news.getNewsDate())*1000);
Calendar cal = Calendar.getInstance();
cal.setTime(nd);
dt.setText(cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR));
} catch (Exception e){
Log.e("ERROR NEWS", e.getMessage());
}
runOnUiThread(returnRes);
}
private Runnable returnRes = new Runnable() {
@Override
public void run() {
m_ProgressDialog.dismiss();
}
};
}
我在这里做的是从包中提取一些数据(它可以正常工作),然后将内容设置为 TextViews 和 ImageView。但是,当我在模拟器上测试时,Image 和 TextViews 是空的。
我检查了电话
ImageView iv = (ImageView) v.findViewById(R.id.icon);
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
而且,显然,然后不要返回null。但如果做
tt.setText("some random text here");
它不起作用。
我觉得我做的不对。任何人都可以帮助我吗?