【问题标题】:Not able to inflate a layout in android无法在android中膨胀布局
【发布时间】:2015-11-23 03:36:27
【问题描述】:

mainactivity.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="#ff0"
    android:id="@+id/myframe"
    >


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|top"
        android:background="#00f"
        />
</FrameLayout>

子布局.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f"
    >

</TextView>

主活动

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.sublayout, null);
        FrameLayout frameLayout = (FrameLayout)findViewById(R.id.myframe);
        setContentView(R.layout.activity_main);
        frameLayout.addView(view);
    }


}

我收到错误消息“在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。 java.lang.Throwable:未调用显式终止方法'close'”

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗
  • FrameLayout frameLayout = (FrameLayout)findViewById(R.id.myframe); setContentView(R.layout.activity_main) 你是在设置视图内容之前找到的?
  • 你充气的目的是什么?
  • 08-28 16:07:32.177 3083-3092/? E/StrictMode:在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。 java.lang.Throwable:在 android.os.ParcelFileDescriptor.(ParcelFileDescriptor.java:180) 的 dalvik.system.CloseGuard.open(CloseGuard.java:184) 未调用显式终止方法“关闭”。 .ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)

标签: android


【解决方案1】:

问题是您在设置内容视图之前尝试访问该视图试试这个..

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 FrameLayout frameLayout = (FrameLayout)findViewById(R.id.myframe);
 LayoutInflater layoutInflater = getLayoutInflater();
 View view = layoutInflater.inflate(R.layout.sublayout, null);
 frameLayout.addView(view);
 }

【讨论】:

  • 尝试将 setcontentView() 放在最后。但仍然是同样的错误
  • @BrightVarghese 将 setContentView 放在 oncreate 方法下方(即在 super.onCreate(savedInstanceState) 下方;
  • 是否必须在代码中添加 setContentView() ?我也试过没有这种方法。但同样的错误
  • setContentView 与您的代码无关。您需要在调用 super.onCreate() 之后调用它来设置稍后用于使用 findViewById() 进行操作的视图
  • 我按照你的建议试过了。它的工作。非常感谢你
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多