尝试填充 ViewStub 时如何解决空指针异常?
很长一段时间以来,我一直在尝试解决填充 ViewStub 时出现空指针异常的问题。我想要做的是在一个包含一些 imageButton 和 ViewStub 的 SlidingDrawer 中,以便在按下 ImageButton 时填充 ViewStub。我一直在按照 ViewStub 类概述 创建 ViewStub,但是我仍然遇到空指针异常。如果有人可以在这里帮助我,那就太好了,因为我真的不知道应该做什么来解决这个问题......我将在下面发布我的代码,
非常感谢您!
这是我的代码中导致异常的部分:
btnExposure = (ImageButton) findViewById(R.id.button_exposure);
btnExposure.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
try{
ViewStub stub = (ViewStub) findViewById(R.id.stub_exposure);
importStub = stub.inflate();
}catch (Exception e){
Log.e("ERROR", "Error in Code:" +e.toString());}
}
});
这是包含 ViewStub 的 DrawingSlider XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shape_background_gradient"
>
<LinearLayout
android:layout_width="205px"
android:layout_height="155px"
android:background="#FFCCCCCC"
android:id="@+id/VideoViewHolder"
android:padding="2.5px"
android:layout_centerHorizontal="true"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
>
<VideoView
android:layout_width="200px"
android:layout_height="150px"
android:id ="@+id/VideoViewEdit"
android:layout_centerHorizontal="true"
>
</VideoView>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/VideoViewHolder">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0">
<TableRow
android:background="@drawable/shape_track_background"
android:padding="5px"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_editor_videotrack"
android:padding="5px"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:background="@drawable/shape_track_background"
android:padding="5px"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_editor_audiotrack"
android:padding="5px"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:background="@drawable/shape_track_background"
android:padding="5px"
>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_editor_add_media"
android:padding="5px"
android:layout_gravity="left"
/>
</TableRow>
</TableLayout>
</ScrollView>
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:handle="@+id/handle"
android:content="@+id/content">
<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/slider_handle"
/>
<LinearLayout
android:id="@id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/button_exposure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_exposure"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_properties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_properties"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_special_effects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_special_effects"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_exposure"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_exposure"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
</LinearLayout>
<ViewStub
android:id="@+id/stub_exposure"
android:inflatedId="@+id/stub_exposure_view"
android:layout="@layout/exposureview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#77000000">
</ViewStub>
<ViewStub
android:id="@+id/stub_properties"
android:inflatedId="@+id/stub_properties_view"
android:layout="@layout/propertiesview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#77000000">
</ViewStub>
<ViewStub
android:id="@+id/stub_special_effects"
android:inflatedId="@+id/stub_special_effects_view"
android:layout="@layout/specialeffectsview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#77000000">
</ViewStub>
</SlidingDrawer>
</RelativeLayout>
这是 ViewStub 布局 xml (exposureview.xml),调用它来扩充存根:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+String/hello"
></TextView>
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/seek_thumb"
android:progress="50"
android:max="100">
</SeekBar>
</RelativeLayout>
for quite a while I have been trying to solve an issue with a Null Pointer Exception while populating a ViewStub. What I am trying to do is within a SlidingDrawer that contains some imageButtons and ViewStubs to fill in the ViewStubs when an ImageButton is pressed. I have been following the ViewStub Class Overview to create the ViewStubs, but I still get the Null Pointer Exception. If someone could help me out here, that would be great because I really don't know anymore what I should do to solve this issue...I will post my code below
Thank you very much in advance!
Here is the part of my code causing the exception:
btnExposure = (ImageButton) findViewById(R.id.button_exposure);
btnExposure.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
try{
ViewStub stub = (ViewStub) findViewById(R.id.stub_exposure);
importStub = stub.inflate();
}catch (Exception e){
Log.e("ERROR", "Error in Code:" +e.toString());}
}
});
This is the DrawingSlider XML file containing the ViewStub:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shape_background_gradient"
>
<LinearLayout
android:layout_width="205px"
android:layout_height="155px"
android:background="#FFCCCCCC"
android:id="@+id/VideoViewHolder"
android:padding="2.5px"
android:layout_centerHorizontal="true"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
>
<VideoView
android:layout_width="200px"
android:layout_height="150px"
android:id ="@+id/VideoViewEdit"
android:layout_centerHorizontal="true"
>
</VideoView>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/VideoViewHolder">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0">
<TableRow
android:background="@drawable/shape_track_background"
android:padding="5px"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_editor_videotrack"
android:padding="5px"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:background="@drawable/shape_track_background"
android:padding="5px"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_editor_audiotrack"
android:padding="5px"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:background="@drawable/shape_track_background"
android:padding="5px"
>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_editor_add_media"
android:padding="5px"
android:layout_gravity="left"
/>
</TableRow>
</TableLayout>
</ScrollView>
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:handle="@+id/handle"
android:content="@+id/content">
<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/slider_handle"
/>
<LinearLayout
android:id="@id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/button_exposure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_exposure"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_properties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_properties"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_special_effects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_special_effects"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_exposure"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
<ImageButton
android:id="@+id/button_test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#77000000"
android:src="@drawable/ic_slider_exposure"
android:padding="5px"
android:layout_marginBottom="1px"
android:layout_gravity="left"/>
</LinearLayout>
<ViewStub
android:id="@+id/stub_exposure"
android:inflatedId="@+id/stub_exposure_view"
android:layout="@layout/exposureview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#77000000">
</ViewStub>
<ViewStub
android:id="@+id/stub_properties"
android:inflatedId="@+id/stub_properties_view"
android:layout="@layout/propertiesview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#77000000">
</ViewStub>
<ViewStub
android:id="@+id/stub_special_effects"
android:inflatedId="@+id/stub_special_effects_view"
android:layout="@layout/specialeffectsview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#77000000">
</ViewStub>
</SlidingDrawer>
</RelativeLayout>
And this is the ViewStub layout xml (exposureview.xml), which is called to inflate the Stub:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+String/hello"
></TextView>
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/seek_thumb"
android:progress="50"
android:max="100">
</SeekBar>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
膨胀或显示 ViewStub 会导致它从布局中删除(它的全部要点是被另一个视图替换)。您的 onClick 方法只会在第一次时正常工作。您应该 (a) 检查从 findViewById 返回的 null,以及 (b) 使用在膨胀后要膨胀到存根中的视图的 id。
Inflating or showing a ViewStub causes it to be removed from the layout (the whole point of it is to be replaced by another view). Your onClick method is only going to work properly the first time. You should (a) check for a null returned from findViewById, and (b) use the id of the view you're inflating into the stub after the inflation.
显然
findViewById(R.id.stub_exposure);
为您返回 null 。您是否正确地重新编译了您的类以重新生成 R.java 文件?另外,我注意到您在视图存根布局 XML 文件中将文本视图的文本属性之一定义为
android:text="@+String/hello"
。不知道是否只是一个拼写错误,但是
R.java
是否已正确生成?如果您在strings.xml
中有一个现有值(注意不是大写),那么编译器应该抱怨。你应该尝试做一个项目 -> Clean 查看是否收到警告或错误(如果您使用的是 Eclipse)。
Apparently
findViewById(R.id.stub_exposure);
returns null for you. Did you recompile your class properly to regenerate the R.java file?Also, I noticed you defined one of your text view's text property in your view stub layout XML file as
android:text="@+String/hello"
.Don't know if it's just a typo or not, but has the
R.java
been properly generated? If you have an existing value instrings.xml
(notice not uppercase) then the compiler should complain.You should try to do a Project -> Clean to see if you get warnings or errors (if you're using Eclipse).
正如 JRL 怀疑的那样,这是因为
findViewById(R.id.stub_exposure)
没有找到/查看/捕获stub_exposure
,尽管 ID 已在R 中生成。 .class
,然后该方法返回null
,当调用inflate()
时,什么也没有发生,因为stub
仍未初始化,只是一个null
对象。您可以使用调试器看到这一点。因此我们可以看出 XML 解析器或编译器可能存在一些问题。我通过将
StubView
使用的View
从单个专用 XML 文件放到主界面与更多Layout 的 XML 文件中解决了这个问题
和View
。我将它放在第一个父层Layer
中,它是一个LinearLayout
但我必须将它与我的StubView< 一起嵌入到
FrameLayout
中/code> 保持所有Layer
的外观相同。我认为这一定很重要,因为您的StubView
也位于Layer
中,但它对您不起作用。因此,请尝试将StubView
放入层次结构的不同级别。As JRL suspects, this is because
findViewById(R.id.stub_exposure)
doesn't find/see/catchstub_exposure
although the ID has been well generated inR.class
, and then, the method returnsnull
and wheninflate()
is called nothing happens becausestub
is still not initialized, is just anull
object. You can see this by using the debugger.So hence we can see that maybe there is some problem with the XML parser or compiler. I solved the problem by putting the
View
to be used by theStubView
from a single dedicated XML file to the XML file where I had the main interface with moreLayout
s andView
s. I put it inside the first parentLayer
which was aLinearLayout
but I had to embed it into aFrameLayout
together with myStubView
to keep all theLayer
s appearing the same. I think this must be important because you also have yourStubView
s insideLayer
s but it doesn't work to you. So try to put theStubView
s into different levels of the hierarchy.