未在活动创建上设置ImageView和TextView

时间:2013-04-04 12:18:22

标签: android android-activity

我正在尝试在我的活动中显示图片和一些字符串,但由于某种原因,视图未被修改。

这是我的代码:

import java.io.File;
import java.io.FileInputStream;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class ObjectProperty extends Activity {


    Activity myAct = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        myAct = this;
        setContentView(R.layout.object_property);

        String objetoNome = myAct.getIntent().getStringExtra("nome");
        String arquivo = myAct.getIntent().getStringExtra("arquivo");
        String descricao = myAct.getIntent().getStringExtra("descricaoCompleta");

        System.out.println("objeto nome: " + objetoNome); //not null/empty
        System.out.println("arquivo: " + arquivo);        //not null/empty
        System.out.println("descricao: " + descricao);    //not null/empty

        ImageView imageView = (ImageView)findViewById(R.id.item_image);
        TextView textViewNome = (TextView)findViewById(R.id.item_nome);
        TextView textViewDescricao = (TextView)findViewById(R.id.item_desc);    

        textViewNome.setText(objetoNome); //does not set text
        textViewDescricao.setText(descricao); //does not set text

        if(!arquivo.equals("")){

            Bitmap myBit = getThumbnail(arquivo);

            if(myBit != null)
                imageView.setImageBitmap(myBit); // does not set image
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.object_property);
    }

    public Bitmap getThumbnail(String filename) 
    {
        Bitmap thumbnail = null;

        try {
            File filePath = myAct.getFileStreamPath(filename);
            FileInputStream fi = new FileInputStream(filePath);
            thumbnail = BitmapFactory.decodeStream(fi);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return thumbnail;
    }

}

我做错了什么?

3 个答案:

答案 0 :(得分:4)

删除此

setContentView(R.layout.object_property);

来自onCreate的最后一部分。

答案 1 :(得分:0)

调用超类方法super.onCreate(savedInstanceState);必须位于oncreate()方法的第一行。

删除oncreate()方法中的最后一行setContentView(R.layout.object_property);

答案 2 :(得分:0)

试试这个,只调用一次setConteinView。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //your code
}