1

我想Activity入驻FrameLayout。这是我的代码:

FrameLayout fl = new FrameLayout(this);
fl = (FrameLayout ) findViewById(R.id.actioncontent);           
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
View myview =LayoutInflater.from(this).inflate(R.layout.wallpaper, null);
fl.removeAllViews();
fl.addView(myview);

我收到一个错误NullPointerException

4

3 回答 3

1

你需要使用inflator而不是LayoutInflater.from(this)

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View myview =LayoutInflater.from(this).inflate(R.layout.wallpaper, null);
            fl.removeAllViews();
                fl.addView(myview);

用下面的代码替换上面的代码

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View myview =inflater .inflate(R.layout.wallpaper, null);
            fl.removeAllViews();
                fl.addView(myview);
于 2013-10-18T12:44:08.667 回答
0

您没有说明“this”是什么,或者 NPE 发生在哪里 - 但可能是“this”呈现了错误的上下文。尝试改用您的活动名称?例如,MainActivity.this

此外,正如我在下面所述,将 from 替换View myview =LayoutInflater.from(this).inflateView myview = inflater.inflate

于 2013-10-18T12:43:09.787 回答
0

改变

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
于 2013-10-18T12:44:27.477 回答