无法实例化一个或多个类

时间:2018-10-29 23:43:55

标签: java android android-studio android-recyclerview bottomnavigationview

我的应用程序有两个问题:

我正在使用BottomNavigationView和RecyclerView。我已经创建了适配器等。

第一个问题如图所示:

enter image description here

如果我将依赖关系更改为较低的版本(例如27.1.0),该错误就会消失,但是当我要启动该应用程序时会发生其他一些情况,因此我将其重新更改了。

此外,当我启动我的应用程序时,对于RecyclerView的高度,我使用类似400dp(match_parent或wrap_content的标记)的方式,它仅在单击BottomNavigation的内容(我不理解)后才显示值-如果我使用match_parent或wrap_content,则该列表根本不会显示(即使我单击某个地方)。

这是我的RecyclerView

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main content -->

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:padding="4dp"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

这是适配器:

package com.example.enis.myapplication.Adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.enis.myapplication.Model.Event;
import com.example.enis.myapplication.R;

import java.util.ArrayList;


public class EventAdapter  extends RecyclerView.Adapter<EventAdapter.ViewHolder>{

    private ArrayList<Event> events;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        int currentItem;
        TextView date, description, location,name;
        ImageView image;

        public ViewHolder(View itemView){
            super(itemView);
            date= (TextView) itemView.findViewById(R.id.date);
            description =(TextView) itemView.findViewById(R.id.description);
            name =(TextView) itemView.findViewById(R.id.name);
            location = (TextView)itemView.findViewById(R.id.location);
            image = (ImageView) itemView.findViewById(R.id.image);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = getAdapterPosition();
                    Snackbar.make(v, "clicked on " + pos,Snackbar.LENGTH_LONG ).setAction("Action",null).show();
                }
            });
        }
    }

    public EventAdapter(ArrayList<Event> events) {
        this.events = events;
    }


    public ArrayList<Event> getEvents() {
        return events;
    }

    public void setEvents(ArrayList<Event> events) {
        this.events = events;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {


        View itemview = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_layout,viewGroup,false);
        ViewHolder v = new ViewHolder(itemview);

        return v;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

        viewHolder.date.setText(events.get(i).getDate());
        viewHolder.description.setText(events.get(i).getDescription());
        viewHolder.location.setText(events.get(i).getLocation());
        viewHolder.name.setText(events.get(i).getName());
        Log.d("Test", String.valueOf(viewHolder.name.getText()));
    }


    @Override
    public int getItemCount() {
        return events.size();
    }

}

这是实例化适配器等的方式

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() { 

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {return true;}
            };


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

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        mRef = mDatabase.getReference();

        events = new ArrayList<>();


        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(llm);    

        getEventsFromDB();

        adapter = new EventAdapter(events);
        mRecyclerView.setAdapter(adapter);

    }

等级依赖性:

    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.enis.myapplication"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1

        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    jcenter()
    mavenCentral()
    google()
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])


    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.android.support:support-vector-drawable:28.0.0'
    implementation 'com.iarcuschin:simpleratingbar:0.1.3'
    implementation 'com.google.firebase:firebase-database:11.8.0'
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

我希望有人可以帮助我:) 最好的问候

编辑:我不知道它是否有帮助,但是我已经使用ListView做到了,并且效果很好。

1 个答案:

答案 0 :(得分:0)

我实际上找到了第二个问题的解决方案:

我必须使用:

adapter.notifyDataSetChanged();

我无法确定为什么单击BottomNavigation后它确切显示了什么,但是现在它显示了内容。但是仍然存在视觉问题。