Android 如何以编程方式实例化和添加视图对象
我试图以编程方式实例化和添加视图对象Android 如何以编程方式实例化和添加视图对象,android,Android,我试图以编程方式实例化和添加视图对象 RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main); View playControlsPanelMinimized = new View(this); activityMain.addView(playControlsPanelMinimized); 我在log cat中得到了这个错误 Attempt to invoke virtual method
RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
View playControlsPanelMinimized = new View(this);
activityMain.addView(playControlsPanelMinimized);
我在log cat中得到了这个错误
Attempt to invoke virtual method
'void android.widget.RelativeLayout.addView(android.view.View)'
on a null object reference
at com.example.michael.musicplayer.PlayPanel.onBackPressed(PlayPanel.java:50)
这是onBackPressed()
方法和行#50
这是activity\u main.xml
<RelativeLayout 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="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context=".MainActivity"
android:id="@+id/activity_main">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<RelativeLayout
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listView1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</RelativeLayout>
</RelativeLayout>
请记住,在使用findViewById()之前,您要引用的视图必须已充气…使用setContentView()或使用充气器手动充气。否则你只会得到一个空值,这就是为什么NPE 'void android.widget.RelativeLayout.addView(android.view.view)' 关于空对象引用 检查
activityMain
是否为空
差不多
RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
View playControlsPanelMinimized = new View(this);
if(activityMain != null)
activityMain.addView(playControlsPanelMinimized); // Line # 50
如果activityMain
为空,则可能是您忘记了对视图进行充气
在一个片段中你可以做到
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.name_of_your_fragment, container, false);
//Some code like
//rootView.findViewById(...);
return rootView;
}
你能给我指出一个具体的链接,从那里我了解了为什么要这样做?我真的不理解膨胀,它的必要性很好地解释了…你必须膨胀它们,以便在活动的viewhierarchy中注册你的视图和viewcontainer。如果你没有放大你的活动主布局(顺便说一句,坏名字),方法不会发现它只是简单地说。因此需要膨胀来从xml创建一个对象并获取对它的引用……或者只是在代码中创建对象,但是您必须手动处理其初始化,而不是依赖xml属性……这意味着需要更多的代码和更多的手工工作……所以您没有膨胀视图。用一个例子来检查编辑。是的,但是你仍然会得到空指针,它不会像你想要的那样。检查我的答案和评论部分。从xml理解膨胀和构建视图对于android开发人员来说是必不可少的
R.id.activity\u main
是一个布局,而不是视图,因此您的findViewById(R.id.activity\u main)代码>将返回null。你必须用布局充气来代替。@U柱好的,我的坏。如果我们已经膨胀了布局,那么我们可以使用findViewById获得它。如果您的布局已经膨胀,那么您应该可以。您是否尝试将mainActivity声明为类私有变量并在onCreate()方法中而不是在onBackPressed事件中实例化它?我想不是。我已经创建了一个测试项目&在运行中添加新元素是没有问题的。您发布的代码没有错误。新视图已成功添加到主活动
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.name_of_your_fragment, container, false);
//Some code like
//rootView.findViewById(...);
return rootView;
}