Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Listview中使用自定义适配器和自定义类时,应用程序崩溃_Java_Android_Firebase Realtime Database - Fatal编程技术网

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

Java 在Listview中使用自定义适配器和自定义类时,应用程序崩溃,java,android,firebase-realtime-database,Java,Android,Firebase Realtime Database,在我们的应用程序中,日志列表应该包含一个日志图片,后跟日志名称,我们使用自定义类和自定义适配器。但应用程序崩溃了 //This is Word class (custom class) public class Word { String dname; public Word() { //default constructor } public Word(String dname) { this.dname=dname; } /*public void setDiaryN

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

//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;
}
//这是Word类(自定义类)
公共类词
{
字符串dname;
公共词()
{
//默认构造函数
}
公共字(字符串dname){
this.dname=dname;
}
/*public void setDiaryName(字符串dname)
{
this.dname=dname;
}*/
公共字符串getDiaryName()
{
返回dname;
}
}
//自定义适配器
公共类ContentAdapter扩展了ArrayAdapter
{
显示d;
公共内容适配器(活动上下文,ArrayListtitle)
{
超级(上下文,0,标题);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.contents,父项,false);
}
Word w=获取项目(位置);
TextView mt=(TextView)listItemView.findViewById(R.id.exp);
mt.setText(w.getDiaryName());
mt.SETVISION(视图可见);
返回listItemView;
}
}
//显示日记列表的片段
公共类HistoryFragment扩展了片段
{
单词di;
ListView lv;
数据库参考;
FirebaseAuth mAuth=FirebaseAuth.getInstance();
活动语境;
公共历史片段(){
//默认构造函数
}
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState)
{
最终视图=充气机。充气(R.layout.scrolllist,容器,错误);
字符串uid=FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef=FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef=rootRef.child(“图像”).child(uid);
ValueEventListener ValueEventListener=新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//List cities=new ArrayList();
最终ArrayList城市=新ArrayList();
对于(DataSnapshot ds:DataSnapshot.getChildren()){
字符串cityName=ds.getKey();
添加(新词(cityName));
}
ListView ListView=(ListView)view.findViewById(R.id.list\u of\u ds);
ContentAdapter arrayAdapter=新的ContentAdapter(上下文,城市);
setAdapter(arrayAdapter);
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
//Log.d(标记,databaseError.getMessage());//不要忽略错误!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
返回视图;
}
}

当应用程序运行时,在选择历史片段时,会发生以下错误- java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”

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


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

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

</RelativeLayout>





//contents.xml (the custom view of diary pic and diary name)
<?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="match_parent">

<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="88dp">



    <ImageView

        android:id="@+id/image_of_user"

        android:layout_width="88dp"

        android:layout_height="88dp"

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



    <RelativeLayout

        android:id="@+id/text_container"

        android:layout_width="match_parent"

        android:layout_height="88dp"

        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

        android:layout_toRightOf="@id/image_of_user"

        android:orientation="vertical"

        android:paddingLeft="16dp"

        android:layout_marginTop="20dp">



        <TextView

            android:id="@+id/exp"

            android:layout_width="match_parent"

            android:layout_height="44dp"

            android:layout_weight="1"

            android:gravity="bottom"

            android:textAppearance="?android:textAppearanceMedium"

            android:textColor="@android:color/background_dark"

            android:textStyle="bold"

            android:text="lutti" />

    </RelativeLayout>
</RelativeLayout>
</RelativeLayout>
//scrolllist.xml(用于listview)
//contents.xml(日志图片和日志名称的自定义视图)

您的活动上下文为空。您尚未初始化它

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;
}
}
公共类单词
{
字符串dname;
公共词()
{
//默认构造函数
}
公共字(字符串dname){
this.dname=dname;
}
/*public void setDiaryName(字符串dname)
{
this.dname=dname;
}*/
公共字符串getDiaryName()
{
返回dname;
}
}
//自定义适配器
公共类ContentAdapter扩展了ArrayAdapter
{
显示d;
公共ContentAdapter(上下文,ArrayListtitle)
{
超级(上下文,0,标题);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.contents,父项,false);
}
Word w=获取项目(位置);
TextView mt=(TextView)listItemView.findViewById(R.id.exp);
mt.setText(w.getDiaryName());
mt.SETVISION(视图可见);
返回listItemView;
}
}
//显示日记列表的片段
公共类HistoryFragment扩展了片段
{
语境;
单词di;
ListView lv;
数据库参考;
FirebaseAuth mAuth=FirebaseAuth.getInstance();
公共历史片段(){
//默认构造函数
}
//下面的方法将从片段所附加到的活动中获取上下文
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
this.context=context;
}
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState)
{
最终视图=充气机。充气(R.layout.scrolllist,容器,错误);
字符串uid=FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef=FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef=rootRef.child(“图像”).child(uid);
ValueEventListener ValueEventListener=新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//List cities=new ArrayList();
最终ArrayList城市=新ArrayList();
对于(DataSnapshot ds:DataSnapshot.getChildren()){
字符串cityName=ds.getKey();
添加(新词(cityName));
}
ListView ListView=(ListView)view.findViewById(R.id.list\u of\u ds);
ContentAdapter arrayAdapter=新的ContentAdapter(上下文,城市);
setAdapter(arrayAdapter);
}
@凌驾
公共空间