有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序在Listview中使用自定义适配器和自定义类时崩溃

在我们的应用程序中,日志列表应该包含一个日志图片,后跟日志名称,我们使用自定义类和自定义适配器。但应用程序崩溃了

//This is Word class (custom class)

public class Word
{
String dname;

public Word()
{
    //default constructor
}

public Word(String dname) {
    this.dname=dname;
}

/*public void setDiaryName(String dname)
{
    this.dname=dname;
}*/

public String getDiaryName()
{
    return dname;
}
}



//the custom adapter

public class ContentAdapter extends ArrayAdapter<Word>
{
    Display d;
    public ContentAdapter(Activity context, ArrayList<Word>title)

{
    super(context, 0, title);
}

public View getView(int position, View convertView, ViewGroup parent) {

    // Check if the existing view is being reused otherwise inflate the view

    View listItemView = convertView;

    if (listItemView == null) {

        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.contents, parent, false);

    }

    Word w = getItem(position);

    TextView mt = (TextView) listItemView.findViewById(R.id.exp);

    mt.setText(w.getDiaryName());

    mt.setVisibility(View.VISIBLE);

    return listItemView;
}
}




//the fragment in which list of diaries is displayed
public class HistoryFragment extends Fragment 
{
    Word di;
    ListView lv;
    DatabaseReference reference;
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    Activity context;
    public HistoryFragment(){
    //default constructor
    }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    final View view = inflater.inflate(R.layout.scrolllist, container, false);

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("image").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //List<String> cities = new ArrayList<>();
            final ArrayList<Word> cities = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String cityName = ds.getKey();
                cities.add(new Word(cityName));
            }
            ListView listView = (ListView)view.findViewById(R.id.list_of_ds);
            ContentAdapter arrayAdapter = new ContentAdapter(context,cities);
            listView.setAdapter(arrayAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            //Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);
    return view;
}

}

当应用程序运行时,在选择历史片段时,会发生以下错误- JAVAlang.NullPointerException:尝试调用虚拟方法“java”。lang.对象安卓。所容纳之物上下文空对象引用上的getSystemService(java.lang.String)“”

    //scrolllist.xml (for listview)    
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@drawable/drawable_gradient">
<ListView
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/list_of_ds"
    安卓:orientation="vertical"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">
</ListView>


<Button
    安卓:id="@+id/add_images"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Add Images"
    安卓:layout_alignParentBottom="true"
    安卓:layout_alignParentRight="true"/>

<Button
    安卓:id="@+id/new_diary"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="New Diary?"
    安卓:layout_alignParentBottom="true"
    安卓:layout_alignParentLeft="true"/>

</RelativeLayout>





//contents.xml (the custom view of diary pic and diary name)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">

<RelativeLayout

    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"

    xmlns:tools="http://schemas.安卓.com/tools"

    安卓:layout_width="match_parent"

    安卓:layout_height="88dp">



    <ImageView

        安卓:id="@+id/image_of_user"

        安卓:layout_width="88dp"

        安卓:layout_height="88dp"

        安卓:src="@drawable/diarypic"
        安卓:layout_marginTop="20dp"/>



    <RelativeLayout

        安卓:id="@+id/text_container"

        安卓:layout_width="match_parent"

        安卓:layout_height="88dp"

        安卓:layout_alignParentBottom="true"

        安卓:layout_alignParentRight="true"

        安卓:layout_alignParentTop="true"

        安卓:layout_toRightOf="@id/image_of_user"

        安卓:orientation="vertical"

        安卓:paddingLeft="16dp"

        安卓:layout_marginTop="20dp">



        <TextView

            安卓:id="@+id/exp"

            安卓:layout_width="match_parent"

            安卓:layout_height="44dp"

            安卓:layout_weight="1"

            安卓:gravity="bottom"

            安卓:textAppearance="?安卓:textAppearanceMedium"

            安卓:textColor="@安卓:color/background_dark"

            安卓:textStyle="bold"

            安卓:text="lutti" />

    </RelativeLayout>
</RelativeLayout>
</RelativeLayout>

共 (1) 个答案

  1. # 1 楼答案

    您的活动上下文为空。你还没有初始化它

    public class Word
    {
    String dname;
    
    public Word()
    {
        //default constructor
    }
    
    public Word(String dname) {
        this.dname=dname;
    }
    
    /*public void setDiaryName(String dname)
    {
        this.dname=dname;
    }*/
    
    public String getDiaryName()
    {
        return dname;
    }
    }
    
    
    
    //the custom adapter
    
    public class ContentAdapter extends ArrayAdapter<Word>
    {
        Display d;
        public ContentAdapter(Context context, ArrayList<Word>title)
    
    {
        super(context, 0, title);
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        // Check if the existing view is being reused otherwise inflate the view
    
        View listItemView = convertView;
    
        if (listItemView == null) {
    
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.contents, parent, false);
    
        }
    
        Word w = getItem(position);
    
        TextView mt = (TextView) listItemView.findViewById(R.id.exp);
    
        mt.setText(w.getDiaryName());
    
        mt.setVisibility(View.VISIBLE);
    
        return listItemView;
    }
    }
    
    
    
    
    //the fragment in which list of diaries is displayed
    public class HistoryFragment extends Fragment 
    {
     Context context;
    
        Word di;
        ListView lv;
        DatabaseReference reference;
        FirebaseAuth mAuth = FirebaseAuth.getInstance();
        public HistoryFragment(){
        //default constructor
        }
    
    //The following method will get the context from the activity to which the fragment is attached 
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            this.context=context;
        }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        final View view = inflater.inflate(R.layout.scrolllist, container, false);
    
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference uidRef = rootRef.child("image").child(uid);
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //List<String> cities = new ArrayList<>();
                final ArrayList<Word> cities = new ArrayList<>();
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    String cityName = ds.getKey();
                    cities.add(new Word(cityName));
                }
                ListView listView = (ListView)view.findViewById(R.id.list_of_ds);
                ContentAdapter arrayAdapter = new ContentAdapter(context,cities);
                listView.setAdapter(arrayAdapter);
            }
    
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                //Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
            }
        };
        uidRef.addListenerForSingleValueEvent(valueEventListener);
        return view;
    }
    }