3

我使用本教程http://www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/创建了一个自定义复合组件

现在我正在尝试使用此组件将其动态添加到片段中的布局中。我正在使用充气机..

LinearLayout column1 = (LinearLayout) getActivity().findViewById(R.id.linear2);
IdeaWidget idea = (IdeaWidget) inflater.inflate(R.layout.idea_component, column1);

我收到一个错误:

11-02 16:07:25.074: E/AndroidRuntime(883): FATAL EXCEPTION: main
11-02 16:07:25.074: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.architter/com.example.architter.MainActivity}: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity

这是代码..谢谢!

自定义组件的 XML 文档* * *

<?xml version="1.0" encoding="utf-8"?>
<com.example.architter.IdeaWidget 
    xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/hello_world"
        ></ImageView>
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        ></TextView>
    <ImageButton
        android:id="@+id/archthis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"
        ></ImageButton>
</com.example.architter.IdeaWidget>

自定义组件的 Java 类* **

package com.example.architter;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IdeaWidget extends LinearLayout {

    private ImageView image;
    private TextView description;
    private ImageButton archthis;

    public IdeaWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ((Activity)getContext()).getLayoutInflater().inflate(R.layout.idea_component, this);
        setupViewItems();
    }

    private void setupViewItems() {
        description = (TextView) findViewById(R.id.description);
        image = (ImageView) findViewById(R.id.image);
        archthis = (ImageButton) findViewById(R.id.archthis);
    }

    public void setDescription(String text) {
        description.setText(text);
    }

    public void setImage(String url) {
        Bitmap bitImage = getBitmapFromURL(url);
        image.setImageBitmap(bitImage);
    }

    public void setArchthis(ImageButton imgb) {
        archthis = imgb;
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src",src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap","returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception",e.getMessage());
            return null;
        }
    }
}
4

1 回答 1

0

LogCat 指出您正在尝试将 ContextThemeWrapper 对象转换为 Activity 对象,这是您无法做到的。这似乎发生在这里:

((Activity)getContext()).getLayoutInflater().inflate(R.layout.idea_component, this);

好消息是您可以通过不同的方式获取 LayoutInflater 的副本:

LayoutInflater.from(this).inflate(R.layout.idea_component, this);
于 2012-11-02T22:28:44.167 回答