Viewpager在空对象引用上设置适配器

时间:2018-10-07 08:25:12

标签: java android listview nullpointerexception

我正在尝试创建一个介绍洛杉矶景点和洛杉矶餐厅的应用程序。主页上是景点和餐厅的图像,当您单击任一页面时,它将打开更多景点或餐厅供人们阅读有关详细信息。我正在实现listview,但是我的setadapter无法正常工作。请查看我的代码并帮助新手!谢谢!错误代码如下,之后是我程序的代码。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

MainActivity.java:

        package com.example.android.tourguide;

    import android.content.Context;
    import android.content.Intent;
    import android.media.Image;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);



        }
        public void attractionList(View v) {
            Intent intent = new Intent(this, attractionList.class);
            startActivity(intent);
        }

    }

attractionList.java:

    package com.example.android.tourguide;

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

import java.util.ArrayList;

public class attractionList extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attraction_list);

        ArrayList<Attraction> attractionArrayList = new ArrayList<>();
        attractionArrayList.add(new Attraction(R.drawable.griffith, R.raw.attraction, "Griffith Observatory"));

        AttractionAdapter attractionAdapter = new AttractionAdapter(this, attractionArrayList);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(attractionAdapter);
    }
}

AttractionAdapter.java:

    package com.example.android.tourguide;

import android.content.Context;
import android.content.res.AssetManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class AttractionAdapter extends ArrayAdapter<Attraction> {

    private ArrayList<Attraction> mArrayList = new ArrayList<>();
    private Context mContext;
    private String eachline;

    public AttractionAdapter(Context context, ArrayList<Attraction> arrayList){
        super(context,0, arrayList);
        mArrayList = arrayList;
        mContext = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listView = convertView;
        if (listView == null) {
            listView = LayoutInflater.from(mContext).inflate(R.layout.activity_attraction_list, parent, false);
        }
        Attraction attraction = mArrayList.get(position);

        TextView textView = (TextView) listView.findViewById(R.id.attraction_name);
        textView.setText(attraction.getmAttractionName());

        ImageView attractionImage = (ImageView) listView.findViewById(R.id.attraction_image);
        attractionImage.setImageResource(attraction.getImageResourceId());
        try {
            InputStream inputStream = getContext().getResources().openRawResource(attraction.getTextFile());
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String eachline;
            while ((eachline = br.readLine()) != null) {
                TextView attractionDetails = (TextView) listView.findViewById(R.id.attraction_details);
                attractionDetails.setText(eachline);
                eachline = br.readLine();


                // `the words in the file are separated by space`, so to get each words
                // eachline = bufferedReader.readLine();
            }br.close();
        }
        catch(IOException ioe){
            // TODO Auto-generated catch block
            ioe.printStackTrace();
        }



    TextView attractionName = (TextView) listView.findViewById(R.id.attraction_name);
        attractionName.setText(attraction.getmAttractionName());

        TextView attractionDetails = (TextView) listView.findViewById(R.id.attraction_details);
        attractionDetails.setText(attraction.getTextFile());
        return listView;
    }
}

listView.xml:

    <?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list">

</ListView>

activity_attraction_list.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    tools:context=".attractionList">

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/image"
        android:orientation="vertical">


        <TextView
            android:id="@+id/attraction_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="@android:color/black"
             />

        <TextView
            android:id="@+id/attraction_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:paddingTop="16dp"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Read more"
            android:textAllCaps="false" />

    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  

您尝试查找activity_attraction_list.xml中不存在的列表视图,因此您的列表视图为空。

更改此

public class attractionList extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_attraction_list); //Change this line

收件人

setContentView(R.layout.listview);