Android java.lang.RuntimeException:无法启动活动组件信息{com.example.gchat/com.example.gchat.HomeActivity}:java.lang.NullPointerException
我试图创建一个这样的菜单(灰色背景的菜单)- 我是用平板电脑做的。其代码如下-Android java.lang.RuntimeException:无法启动活动组件信息{com.example.gchat/com.example.gchat.HomeActivity}:java.lang.NullPointerException,android,textview,onclicklistener,Android,Textview,Onclicklistener,我试图创建一个这样的菜单(灰色背景的菜单)- 我是用平板电脑做的。其代码如下- ImageView imgv1, imgv2; TextView tv1, tv2, tv3; private Animation down; View child; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.
ImageView imgv1, imgv2;
TextView tv1, tv2, tv3;
private Animation down;
View child;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
imgv1 = (ImageView) findViewById(R.id.imageViewHA1);
imgv2 = (ImageView) findViewById(R.id.imageViewHA2);
tv1 = (TextView) findViewById(R.id.textViewMenu1);
tv2 = (TextView) findViewById(R.id.textViewMenu2);
tv3 = (TextView) findViewById(R.id.textViewMenu3);
down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
imgv2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
LinearLayout item = (LinearLayout) findViewById(R.id.linearLayout2);
if (child == null)
{
item.setAnimation(down);
down.start();
child = getLayoutInflater().inflate(R.layout.menu_layout, null);
item.addView(child);
} else
{
item.removeAllViews();
child = null;
}
}
});
tv3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
System.exit(0);
}
});
}
R.layout.menu\u布局代码-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/menulayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/White"
android:orientation="vertical"
android:showDividers="middle" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="2dp"
android:background="@color/DimGray" >
<TextView
android:id="@+id/textViewMenu1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:text="New Group"
android:textColor="@color/White"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="2dp"
android:background="@color/DimGray" >
<TextView
android:id="@+id/textViewMenu2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:text="Contacts"
android:textColor="@color/White"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="2dp"
android:background="@color/DimGray"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewMenu3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:text="Exit"
android:textColor="@color/White"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
我在这里和谷歌搜索了很多,但没有找到任何有用的东西。如何更正此错误?即使在展开布局之前,您仍在访问子视图,这是导致出现
NullPointerException
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gchat/com.example.gchat.HomeActivity}: java.lang.NullPointerException
首先充气布局,然后使用充气的布局参照访问其子视图。否则,系统将无法理解您尝试使用的视图属于膨胀布局
例如:
View view = getLayoutInflater().inflate(R.layout.menu_layout, null);
TextView tv1 = (TextView) view.findViewById(R.id.textViewMenu1);
更新:
您应该初始化充气布局的文本视图
,如下所示
if (child == null) {
item.setAnimation(down);
down.start();
child = getLayoutInflater().inflate(R.layout.menu_layout, null);
tv1 = (TextView) child.findViewById(R.id.textViewMenu1);
tv2 = (TextView) child.findViewById(R.id.textViewMenu2);
tv3 = (TextView) child.findViewById(R.id.textViewMenu3);
tv3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//you code
}
});
item.addView(child);
}
请发布您的完整日志。并且,请删除系统。exit@blackbelt我应该用什么来代替System.exit?HomeActivity.java中的第58行是什么?@Lal tv3.setOnClickListener()正如你所说,我尝试过,但仍然得到相同的异常。请您在我的java代码中进行一些编辑并演示给我看好吗?您能告诉我,哪个视图的id为
linearLayout2
?这是我添加充气机的视图。我已经更新了我的答案,还有一件事,哪一行是HomeActivity
中的第58行?第58行是tv3.setOnClickListener()。
if (child == null) {
item.setAnimation(down);
down.start();
child = getLayoutInflater().inflate(R.layout.menu_layout, null);
tv1 = (TextView) child.findViewById(R.id.textViewMenu1);
tv2 = (TextView) child.findViewById(R.id.textViewMenu2);
tv3 = (TextView) child.findViewById(R.id.textViewMenu3);
tv3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//you code
}
});
item.addView(child);
}