【发布时间】:2019-05-15 14:32:32
【问题描述】:
我在我的应用程序的抽屉布局中引入了一个列表视图。我已经创建了 customadapter 和所有必需的东西,但我收到以下错误:
java.lang.RunTimeException:UnabletoStart 活动组件信息{com.example.myapp/com.example.myapp.SecondActivity; java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void (android.widget.ListView)” 在 android.app.activity.launchActivity(ActivityThread.java:2843)
我希望这会有所帮助(我必须输入错误)。我是 android 新手(尤其是适配器和 listview),所以请帮助我找到错误。
CustomAdapter.java
package com.example.myapp;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<TrueFalse> {
Context mContext;
int mlayoutResourceId;
TrueFalse[] questions;
public CustomAdapter(Context mContext,int layoutResourceId,TrueFalse[] questions)
{
super(mContext,layoutResourceId,questions);
this.mContext=mContext;
this.questions=questions;
this.mlayoutResourceId=layoutResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView=((Activity)mContext).getLayoutInflater().inflate (mlayoutResourceId,parent,false);
TextView quesno=(TextView)convertView.findViewById(R.id.quesnameid);
TrueFalse mQ=getItem(position);
int index=mQ.getIndex();
quesno.setText("Question No."+Integer.toString(index));
return convertView;
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawerlayout);
mDrawerList=(ListView)findViewById(R.id.left_drawer);
CustomAdapter adapter=new CustomAdapter(this,R.layout.listview_item,mQuestionBank);
mDrawerList.setAdapter(adapter);
}
#I think this much code is sufficient
}
listview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/drawer_layout2">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="@+id/left_drawer"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@color/colorPrimary"
/>
</android.support.v4.widget.DrawerLayout>
nav_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:openDrawer="start"
android:background="#ffff99"
android:fitsSystemWindows="true">
<include
layout="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<include
layout="@layout/listview_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
/>
</android.support.v4.widget.DrawerLayout>
提前致谢。
【问题讨论】:
-
如果您是 android 新手,我建议您使用 RecyclerView(如文档中所述):developer.android.com/reference/android/widget/ListView 和 developer.android.com/guide/topics/ui/layout/recyclerview
-
添加了文件;你能看看@RomainGoutte-Fangeas
-
我的活动中有一个左侧的导航抽屉和右侧的列表视图抽屉
-
代码好像没问题,可能和这里的问题一样:stackoverflow.com/questions/10803672/…
-
我发现了我的错误:没有在我的 java 代码中添加抽屉布局。感谢您的努力
标签: android android-listview android-arrayadapter