无法在列表视图中设置可见性

时间:2012-05-03 02:10:24

标签: android layout nullpointerexception

这是控制布局的可见性

if (!(imagepath[i].toString().equals("no picture"))) {
            try {
                aURL = new URL("http://www.orientaldaily.com.my/"
                        + imagepath[i]);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();

                layout_image.setVisibility(View.VISIBLE);
                imageview.setVisibility(View.VISIBLE);
                imageview.setScaleType(ScaleType.CENTER_CROP);
                imageview.setImageBitmap(bm);

            } catch (IOException e) {
                Log.e("DEBUGTAG", "Remote Image Exception", e);
            }
        }

                    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.main_alllatestnewslist, from, to);
    lv.setAdapter(adapter);

这是初始化

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_alllatestnews);
        lv = (ListView) findViewById(android.R.id.list);
        RelativeLayout temp = (RelativeLayout)findViewById(R.id.layout_temp);
    LayoutInflater liInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    temp.addView(liInflater.inflate(R.layout.main_alllatestnewslist, null));

这是main_alllatestnews.xml

<LinearLayout
    android:id="@+id/layout_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/layout_menu"
    android:layout_below="@id/layout_title"
    android:orientation="vertical" >
<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

这是main_alllatestnewslist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_temp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
 android:background="@drawable/background_news_list" >

<LinearLayout
    android:id="@+id/layout_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
    android:layout_marginTop="10px" >

    <ImageView
        android:id="@+id/image_alllatestnewstitle"
        android:layout_width="134px"
        android:layout_height="80px"
        android:src="@drawable/image_loading_failed_1"
        android:visibility="invisible" />
</LinearLayout>

当我运行它时,它在行

给了我Nullpointerexception
temp.addView(liInflater.inflate(R.layout.main_alllatestnewslist, null)); <-- inflater still null...

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

更改

setContentView(R.layout.main_alllatestnews);

setContentView(R.layout.main_alllatestnewslist);

因为layout_image位于布局main_alllatestnewslist

编辑:

        LayoutInflater liInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        yourView.addView(liInflater.inflate(R.layout.main_alllatestnewslist, null))

类似的东西。