【问题标题】:Inflate custom view android problems膨胀自定义视图android问题
【发布时间】:2011-03-14 19:42:39
【问题描述】:

我已将视图扩展到 ScrollImageView。然后我把它放到xml中:

在 main.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:orientation="vertical">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/my_xml" />

    <com.Android.Maps03.ScrollImageView
        android:id="@+id/BaseView" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />



</RelativeLayout>

然后我想在这样的主要活动中使用它:

 public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        ScrollImageView sIV = (ScrollImageView)findViewById(R.id.BaseView);
        Bitmap markerImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.mapbase );
        sIV.setImage(markerImage);

当我运行应用程序时,不会出现 ScrollImage。

类(ScrollImageView)的构造函数是这样的:

public ScrollImageView(Context context, AttributeSet attributeSet) {
    super(context);
    initScrollImageView(context);

private void initScrollImageView(Context context) {
    mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    mPadding = DEFAULT_PADDING;
}

你有什么想法吗?

谢谢。

评论 #1 是的,我忘记了,但我有同样的问题。

评论 #2 不,我用第二个 /> 关闭所有布局,而不是关闭两次

评论 #3 这是新的 logcat,我认为这是最了不起的条目。

请帮忙!

【问题讨论】:

  • 不确定这是否是问题,但超级调用应该是 super(context, attributeSet)。 attributeSet 包含 xml 中的所有属性。
  • 您还要关闭 ScrollImageView 元素两次。
  • @Gonzalo Diaz Harcha 你确定设置的当前布局定义的是包含滚动图像视图的布局吗?...如果它在对象链的某个地方出现问题,它可能会在你面前抛出试图找到它,听起来更像是您正在查找的视图不包含嵌套视图,并且显然将 null 转换为类型从来都不是一件有趣的事情。所以你的活动视图使用setContentView()传递包含视图?
  • 我在问题中添加了更多代码。 @Quintin Robinson 我不知道layot 是否包含滚动图像视图。错误日志说这个“com.Android.Maps03.ScrollImageView 无法实例化”。我认为这可能是问题所在。集合是 setContentView(R.layout.main);程序给出的main.xml。
  • @Gonzalo Diaz Harcha 好的两件事,你已经扩展了抛出错误的行,哪一行实际上抛出了异常?另外,如果您在调试中运行应用程序,您是否可以进入ScrollImageView 构造函数并查看它是否在特定行出现异常?

标签: android view inflate


【解决方案1】:

您没有在 Activity onCreate 中调用 setContentView

【讨论】:

  • 我把 setContentView() 放在指针之前,所以现在我没有空指针异常,但是我的 ScrollImageView 没有出现...:S
  • @Gonzalo Diaz Harcha 好吧,它里面没有任何你希望看到的内容?
  • 我加载图片资源。例如,如果我调用“...ScrollImageView sIV = new ScrollImageView(this); 然后 setContentView(sIV); 视图是完美的,这就是我想要的,但我需要其他东西,比如按钮,而不仅仅是那个视图。我认为这样会更好(尝试将我的 ScrollImageView 放入 main.xml 并从那里调用)但它没有任何好的结果。你有什么想法吗?谢谢
  • 一切看起来都不错,您只需在滚动视图中添加内容,它是空的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
相关资源
最近更新 更多