应用程序崩溃,空指针异常。列表视图给出了null异常

时间:2019-06-06 07:40:01

标签: android listview nullpointerexception

我的项目中的一项活动崩溃了。 LogCat说“ Null EXception”。提供空异常的简单适配器。我已经发布了feed_activity.java和feed_activity.XML文件。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FeedActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Your Feed");
    setContentView(R.layout.activity_feed);
    final   ListView listView =  findViewById(R.id.listview);
    final  List<Map<String,String>> wordData= new ArrayList<>();

               for (int i=1;i<=5;i++){
                  Map<String,String> wordInfo = new HashMap<>();
                  wordInfo.put("content","word 
                  content"+Integer.toString(i));
                  wordInfo.put("username","user"+Integer.toString(i));
                  wordData.add(wordInfo);

               }
    SimpleAdapter  simpleAdapter = new SimpleAdapter(this, 
    wordData,android.R.layout.simple_list_item_2, new String[] 
   {"content","username"}, new int[] 
   {android.R.id.text1,android.R.id.text2});
    listView.setAdapter(simpleAdapter);

           }
        }

[用于馈送活动的LogCat] [2] XML Activity_feed:-ListView用于约束布局内。

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".FeedActivity">

<ListView
    android:id="@+id/listView"
    android:layout_width="395dp"
    android:layout_height="590dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:-1)

您可以尝试以下操作-

  1. 尝试从列表声明中删除最终关键字。

  2. 尝试替换

最终ListView = findViewById(R.id.listview);

使用

最终ListView =(ListView)findViewById(R.id.listview);

  1. 再检查一次listview的ID。