我有一个这样的 Android xml 布局文件:
<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:layout_below="@id/logoTop"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="date"
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="title"
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="text"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
它是一个简单的布局,屏幕顶部和底部有两个 ImageView,中间有一个 LinearLayout。我想要的是从 Bundle 中获取一些数据并将其写入 LinearLayout。为此,我有以下 Java 代码:
public class NewsD 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();
m_ProgressDialog = ProgressDialog.show(NewsD.this,
this.getString(R.string.wait), this.getString(R.string.receiving, true));
runOnUiThread(new Runnable(){
@Override
public void run() {
getNews();
}
});
}
public void getNews() {
m_news = new News();
m_news.setNewsTitle(bundle.getString("title"));
m_news.setNewsText(bundle.getString("text"));
m_news.setNewsDate(bundle.getString("data"));
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", e.getMessage());
}
m_ProgressDialog.dismiss();
}
}
问题是 3 TextViews 和 ImageView 显示默认文本/图像,在 xml 中指定。我检查过
ImageView iv = (ImageView) v.findViewById(R.id.icon);
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
声明,似乎工作正常。它们不为空。此外,来自捆绑包的数据也很好。我对此进行了大约 1 小时的研究,并更改了代码以添加 runOnUiThread() 语句以确保 getNews() 函数在 UI 线程中运行。但没有任何效果。任何人都可以帮助我吗?
提前致谢。