我有这样的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的线性布局,然后是图像,然后是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");
它不起作用。
我想我做的不对。有人可以帮帮我吗?
答案 0 :(得分:2)
我认为问题在于您没有访问当前视图,但是您正在创建新视图:
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.activity_news_activity, null);
您根本不需要这两行,因为您可以直接访问当前视图组件
ImageView iv = (ImageView) findViewById(R.id.icon);
TextView tt = (TextView) findViewById(R.id.toptext);
TextView bt = (TextView) findViewById(R.id.bottomtext);
然后设置值。
当然,由于非ui线程调用了getNews()方法,因此必须通过runOnUiThread调用其中涉及UI的所有代码:
public void getNews() {
// ...
runOnUiThread(new Runnable() {
@Override
public void run() {
ImageView iv = (ImageView) findViewById(R.id.icon);
TextView tt = (TextView) findViewById(R.id.toptext);
TextView bt = (TextView) findViewById(R.id.bottomtext);
tt.setText("some random text here");
//...
}
});
//...
答案 1 :(得分:0)
您应该指定LinearLayout
在RelativeLayout
内的位置。例如,您应该指定:
android:layout_below="@+id/logoTop"
此外,如果您希望LinearLayout
中的项目垂直对齐,则需要设置
android:orientation="vertical"
这会使您的总LinearLayout
声明看起来像:
<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" >