Java android中的LayoutFlater nullpointerexception
我想在Java android中的LayoutFlater nullpointerexception,java,android,nullpointerexception,layout-inflater,android-framelayout,Java,Android,Nullpointerexception,Layout Inflater,Android Framelayout,我想在FrameLayout中设置Activity。这是我的密码: FrameLayout fl = new FrameLayout(this); fl = (FrameLayout ) findViewById(R.id.actioncontent); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout
中设置Activity
。这是我的密码:
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您没有说明“这”是什么,或者NPE发生在哪里-但可能是“这”显示了错误的上下文。 尝试改用您的活动名称?例如,
MainActivity.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);
另外,正如我下面所说的,从
View myview=LayoutInflater.from(this.inflate)替换为View myview=inflater.inflate
您没有说明“this”是什么,也没有说明NPE发生在哪里,但可能是“this”呈现了错误的上下文。
尝试改用您的活动名称?例如,MainActivity.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);
另外,正如我下面所说的,从查看myview=LayoutInflater.从(此)开始,充气到查看myview=Flater.充气您需要使用充气机而不是LayoutInflater.从(此)
将上面的代码替换为下面的代码
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myview =inflater .inflate(R.layout.wallpaper, null);
fl.removeAllViews();
fl.addView(myview);
您需要使用充气机
而不是LayoutInflater.from(this)
将上面的代码替换为下面的代码
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myview =inflater .inflate(R.layout.wallpaper, null);
fl.removeAllViews();
fl.addView(myview);
改变
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
到
改变
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
到
View myview=LayoutInflater.from(this).充气(R.layout.wallpaper,null);将此更改为查看myview=充气机。充气(R.layout.wallpaper,null);谢谢我知道了我解决了这个错误现在我有另一个问题我想在这个框架布局中显示一个活动,活动名称是Wallpaper如何在框架布局中显示这个活动myview=LayoutFlater.from(this).充气(R.layout.Wallpaper,null);将此更改为查看myview=充气机。充气(R.layout.wallpaper,null);谢谢我知道了我解决了这个错误现在我有另一个问题我想在这个框架布局中显示一个活动,活动名称是墙纸我如何在框架布局中显示这个活动