Android TextView中的setText()不执行任何操作

时间:2012-09-06 16:22:37

标签: android android-layout

我有一个像这样的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()语句,以确保在UI线程中运行getNews()函数。但没有任何作用。有人可以帮帮我吗?

提前致谢。

2 个答案:

答案 0 :(得分:3)

好的,问题是你在getNews调用中将视图膨胀到内存中,但是没有显示该视图。一旦调用setContentView,视图就会被充气。因此,请使用以下视图:

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"));



    try{
        ImageView iv = (ImageView) findViewById(R.id.icon);
        TextView tt = (TextView) findViewById(R.id.toptext);
        TextView bt = (TextView) findViewById(R.id.bottomtext);

答案 1 :(得分:0)

我相信,你正在获得NetworkOnMainThreadException,但是因为你正在捕获异常,所以你正在丢失关于你得到的异常的数据。在你的情况下RunOnuiThread不是正确的方法。

使用Asynctask并将所有UI代码(例如layoutinflation,setText,setImageBitmap)移动到OnPreExecute或OnPostExecute,并将下移到Asynctask的doInbackground。

Bitmap bmp = Utils.getBitmapFromURL("http://www.myserver.com/IMG/" + m_news.getNewsPhoto());