Java 如何解决';void android.widget.ListView.setAdapter(android.widget.ListAdapter)和#x27;android中的空对象引用?

Java 如何解决';void android.widget.ListView.setAdapter(android.widget.ListAdapter)和#x27;android中的空对象引用?,java,android,listview,nullpointerexception,Java,Android,Listview,Nullpointerexception,我正在实现“BaseActivity”,它包含所有其他活动的工具栏。此基本活动需要在其他活动中扩展以添加工具栏。现在的问题是,我有一个名为“ElectronicsMainActivity”的活动,其中包含listview。此活动需要扩展“BaseActivity”,如 公共类电子产品不活动扩展基本活动{ 但启动此活动时,我的应用程序在运行时崩溃 错误是: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.List

我正在实现“BaseActivity”,它包含所有其他活动的工具栏。此基本活动需要在其他活动中扩展以添加工具栏。现在的问题是,我有一个名为“ElectronicsMainActivity”的活动,其中包含listview。此活动需要扩展“BaseActivity”,如

公共类电子产品不活动扩展基本活动{

但启动此活动时,我的应用程序在运行时崩溃

错误是:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”

我的“电子不活动”代码是:

请告诉我哪里出错了。我已经用另一个活动实现了此baseactivity,但该活动没有listview。“ElectronicsMainActivity”包含listview。请建议

java.lang.NullPointerException:尝试调用虚拟方法“void” android.widget.ListView.setAdapter(android.widget.ListAdapter)“”在 空对象引用

首先声明ListView

    lvComments = (ListView) findViewById(R.id.lvListview);       
    alComment = new ArrayList<>();
    mapViewHolder = new SparseArray<View>();
    comment = new ModelWomen();
    comment = (ModelWomen) getIntent().getSerializableExtra("id");

为什么要调用setContentView()两次???

以上代码的问题是,在尝试从BaseActivity中查找列表视图后,您正在设置活动的contentView 因此,在调用
super.onCreate()
方法之前设置contentView

 setContentView(R.layout.activity_electronics_main);
 super.onCreate(savedInstanceState);
这将解决问题

编辑
从BaseActivity中删除setContentView覆盖方法,并通过
onCreate()中的
findViewById(R.id.toolbar\u id)
查找工具栏,这是因为您正在调用
BaseActivity
onCreate
,并且它正在调用
initUI()
但您的
UI
尚未初始化,因此导致指针为空。您可以在
ListView
初始化后按如下方式移动
super.onCreate()

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.activity_electronics_main);
            lvComments = (ListView) findViewById(R.id.lvListview);
            alComment = new ArrayList<>();
            mapViewHolder = new SparseArray<View>();
            comment = new ModelWomen();
            comment = (ModelWomen) getIntent().getSerializableExtra("id");
            super.onCreate(savedInstanceState);
            // initUi(); <-- Redundant method invocation as it is already invoked from base
            new AsyncGetWomencategories().execute();
        }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
setContentView(R.layout.activity\u electronics\u main);
lvComments=(ListView)findViewById(R.id.lvListview);
alComment=newarraylist();
mapViewHolder=新SparseArray();
comment=newmodelwomen();
comment=(ModelWomen)getIntent().getSerializableExtra(“id”);
super.onCreate(savedInstanceState);

//initUi();试试这个:用这个替换BaseActivity

 public class BaseActivity extends AppCompatActivity {

    @Nullable
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Nullable
    @BindView(R.id.ivLogo)
    ImageView ivLogo;

    private MenuItem inboxMenuItem;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        bindViews();
    }

    protected void bindViews() {
        ButterKnife.bind(this);
        setupToolbar();
    }

    public void setContentViewWithoutInject(int layoutResId) {
        super.setContentView(layoutResId);
    }

    protected void setupToolbar() {
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setNavigationIcon(R.drawable.ic_menu_white);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        inboxMenuItem = menu.findItem(R.id.action_inbox);
        inboxMenuItem.setActionView(R.layout.menu_item_view);
        return true;
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    public MenuItem getInboxMenuItem() {
        return inboxMenuItem;
    }

    public ImageView getIvLogo() {
        return ivLogo;
    }}
使用此依赖项来创建BindView

 compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
创建新的Toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:elevation="@dimen/default_elevation"
    app:layout_scrollFlags="scroll|enterAlways"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <TextView
        android:id="@+id/ivLogo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:scaleType="center"
      />
</android.support.v7.widget.Toolbar>

在活动中使用此工具栏

 <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar.xml" />


不起作用。:(@parapathe-check-demo对我不起作用。相同的erorr。
 compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:elevation="@dimen/default_elevation"
    app:layout_scrollFlags="scroll|enterAlways"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <TextView
        android:id="@+id/ivLogo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:scaleType="center"
      />
</android.support.v7.widget.Toolbar>
 <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar.xml" />