【问题标题】:Unable to figure out java.lang.NullPointerException无法弄清楚 java.lang.NullPointerException
【发布时间】: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 android-listview android-arrayadapter


【解决方案1】:

我也曾经遇到过同样的问题。我不了解你,但正如你所说的有两个抽屉,我忘了在活动代码中包含抽屉。它导致了类似的错误。检查你是否已经这样做了。

【讨论】:

  • 非常感谢,它成功了。我没有添加第二个抽屉(用于我的代码中的列表视图)
  • 但是还有一个问题:我的带有抽屉的 Activity 按钮变得没有响应,您能提出一些建议吗?
【解决方案2】:

您应该在使用 ListView 之前对其进行初始化。

【讨论】:

  • 对不起,其实我已经将listview声明为:private ListView mDrawerList;在SecondActivity.java中(我忘了写代码)
  • @HSSingh 初始化它。不是声明。
  • 我认为mDrawerList=(ListView)findViewById(R.id.left_drawer); 初始化了列表视图
【解决方案3】:

您在SecondActivity 中缺少setContentView(R.layout.second_activity); // or whatever you called it,因此没有任何布局被夸大,这就是为什么找不到ListView。 所以就在下面

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

【讨论】:

  • 我实际上在代码中错过了它。我现在已经添加了所有的 xml 文件以及这一行。给您带来不便,敬请见谅
  • setContentView(R.id.drawerlayout); - 不正确。您必须提供不是id 或特定View 的布局。所以在你的场景中setContenView 方法必须接受布局。
  • 其实我的意思是一样的。基本代码应该是正确的。正如错误所说,我预计列表视图/适配器部分中的错误
  • 嘿,你能帮帮我吗?现在,我所有的抽屉都工作正常,但活动变得无响应。 (例如,SecondActivity.java 中的按钮不起作用)您能提出一些建议吗? @Yupi
【解决方案4】:

参考玉皮的回答:

  1. setContentView() 不应以布局 ID 为目标,而是以文件名为目标。

  2. 它返回 java.lang.NullPointerException 因为你在 setContentView() 中放置了错误的目标布局,所以它无法找到 id 为 R.id.left_drawer 的 listViews

抱歉我的英语不好。

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多