0

这是控制布局的可见性

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...

如何解决这个问题?

4

1 回答 1

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

类似的东西。

;

于 2012-05-03T02:23:08.120 回答