Я ввел в свое приложение список в виде списка ящиков. Я создал Customadapter и все необходимые вещи, но я получаю следующую ошибку:
java.lang.RunTimeException: действие UnabletoStart ComponentInfo {com.example.myapp / com.example.myapp.SecondActivity;
java.lang.NullPointerException: попытка вызвать виртуальный метод «void (android.widget.ListView)» для ссылки на пустой объект
в
android.app.activity.launchActivity (ActivityThread.java:2843)
Надеюсь, это очень поможет (я должен напечатать ошибку). Я новичок в Android (особенно адаптеры и просмотр списка), поэтому, пожалуйста, помогите мне в поиске ошибки.
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>
Заранее спасибо.