如何添加要在各个活动文件中引用的 ListView 元素


一旦

启动任何列表活动,应用程序就会崩溃:

NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter' on a null object reference

我的导师说这是因为布局配置不正确。目前,list_item被夸大为"活动"布局。请创建一个单独的布局,例如activity_list,并在单个活动文件中添加要引用的 ListView 元素

这是代码

主活动

package com.example.tourguide;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button event = (Button) findViewById(R.id.events);
        Button mall = (Button) findViewById(R.id.malls);
        Button resturant = (Button) findViewById(R.id.resturants);
        Button university = (Button) findViewById(R.id.uinversities);
        event.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent event = new Intent(MainActivity.this, events.class);
                startActivity(event); }
        });
        mall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mall = new Intent(MainActivity.this, malls.class);
                startActivity(mall);
            }
        });
        resturant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent resturant = new Intent(MainActivity.this, resturants.class);
                startActivity(resturant);
            }
        });
        university.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent university = new Intent(MainActivity.this, universities.class);
                startActivity(university);
            }
        });
    }}

字适配器.java

package com.example.tourguide;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
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 java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<listitem> {
    public WordAdapter(Activity context, ArrayList<listitem> listitems) {
        super(context, 0, listitems);
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }
        listitem currentItem = getItem(position);
        TextView name = (TextView) listItemView.findViewById(R.id.text1);
        name.setText(currentItem.getName());
        ImageView image = (ImageView) listItemView.findViewById(R.id.image1);
        image.setImageResource(currentItem.getImage());
        TextView dist = (TextView) listItemView.findViewById(R.id.text2);
        dist.setText(currentItem.getDist());
        TextView price = (TextView) listItemView.findViewById(R.id.text2);
        price.setText(currentItem.getPrice());
        return listItemView;
    }
}

列表项.java

package com.example.tourguide;
import android.widget.ImageView;
public class listitem {
    private  String name="";
    private int image;
    private  String dist="";
    private  String price="";
    public listitem(String namea, int imagea, String dista, String pricea){
        name = namea;
        imagea = image;
        dist= dista;
        price=pricea;
    }
    /*********** Set Methods ******************/
    public void setName(String name)
    {
        this.name = name;
    }
    public void setImage(int image)
    {
        this.image = image;
    }
    public void setDist(String dist)
    {
        this.dist = dist;
    }
    public void setPrice(String price)
    {
        this.price = price;
    }
    /*********** Get Methods ****************/
    public String getName()
    {
        return this.name;
    }
    public int getImage()
    {
        return this.image;
    }
    public String getDist()
    {
        return this.dist;
    }
    public String getPrice()
    {
        return this.price;
    }
}

大学.java

package com.example.tourguide;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
public class universities extends Activity {
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.list_item);
        ArrayList<listitem> listitemArrayList = new ArrayList<listitem>();
        listitemArrayList.add(new listitem("Prince Sultan University", R.drawable.u1, "AlNarjes Dist", "$"));
        listitemArrayList.add(new listitem("Princess Nourah University", R.drawable.u2, "AlNarjes Dist", "$"));
        listitemArrayList.add(new listitem("King Saud University", R.drawable.u3, "AlNarjes Dist", "$"));
        WordAdapter adapter = new WordAdapter(this, listitemArrayList);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    tools:context="com.example.tourguide.MainActivity"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="29sp"
        android:textColor="#3DC195"
        android:textAlignment="center"
        android:text="Tour Guide App" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#6AA13F"
        android:textSize="18sp"
        android:text="Tour Guide App is designed to help you discover Riyadh city sightseeings including events, malls, resturants and universities.the app will be developed periodically to add more features... stay tuned" />
    <Button
        android:id="@+id/events"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Events"
        android:onClick="onClickEvent"/>
    <Button
        android:id="@+id/malls"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Malls"
        android:onClick="onClickMall"/>
    <Button
        android:id="@+id/resturants"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Resturants"
        android:onClick="onClickRest"/>
    <Button
        android:id="@+id/uinversities"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Universities"
        android:onClick="onClickUniv"/>
</LinearLayout>

activity_list.xml

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

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <ImageView
                    android:id="@+id/image1"
                    android:layout_width="163dp"
                    android:layout_height="96dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="15dp"
                    />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="68dp"
                    android:layout_marginTop="30dp"
                    android:orientation="vertical">
                    <TextView
                        android:id="@+id/text1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#DB0BA4"
                        android:textSize="20sp"
                   />
                    <TextView
                        android:id="@+id/text2"
                        android:layout_width="113dp"
                        android:layout_height="wrap_content"
                        android:textColor="#456233"
                        android:textSize="15sp"
                   />
                    <TextView
                        android:id="@+id/text3"
                        android:layout_width="113dp"
                        android:layout_height="wrap_content"
                        android:textColor="#C1B03D"
                        android:textSize="15sp"
                   />
            </LinearLayout>
            </LinearLayout>
            </LinearLayout>

在文件Univerisities.java中,您应该将布局文件更改为activity_list.xml

线

setContentView(R.layout.list_item);

必须更改为 :

setContentView(R.layout.activity_list);

兄弟,

首先,您尝试将 ListView 项膨胀到您的活动中,这是错误的:将setContentView(R.layout.list_item)更改为setContentView(R.layout.activity_list);

另外,请查看ListView的ViewHolder模式,因为适配器的getView方法应该通过它进行改进,以使滚动更加流畅:

  View listItemView = convertView;
    if(listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

如果列表中没有出现任何项目,则还应覆盖 getItemCount on adatpter以返回列表的大小。

相关内容

  • 没有找到相关文章

最新更新