ArrayAdapter无法从Firebase读取数据(我的应用关闭)

时间:2017-07-29 13:31:29

标签: android firebase firebase-realtime-database android-arrayadapter

我正在使用Firebase创建一个Android应用程序,并且在数据库中写入数据正在运行,但是通过读取我在数据库中写入的数据,我被卡住了(我对ArrayAdapter,Firebase没有太多经验,而不是甚至很多Android)。

当我从手机打开这个包含来自Firebase读取的片段时(注意:写入数据在另一个片段中),我的应用程序关闭。不确定它是否与之相关 - 我如何实现和使用ArrayAdapter - 我如何实现Firebase读取 - 或者我已经更新到Android工作室的事实(最新版本SDK 26.0.0) - 或者我没看到的任何其他原因。

我收到一些错误: - 我得到下面的错误,我用下面的代码修改(configurations.all ...)。当我从Firebase添加依赖项时,我得到了这个错误。

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0) from [com.android.support:design:26.0.0] AndroidManifest.xml:28:13-35
    is also present at [com.android.support:cardview-v7:25.4.0] AndroidManifest.xml:25:13-35 value=(25.4.0).
    Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.0'
            }
        }
    }
}
  • 当我在调试模式下运行并且我只是传递inflater.inflate方法时,我进入我的调试器f.mIsNewlyAdded - 找不到本地变量&#39; f&#39; ( - 不确定这是否正常?
  • 有时我在返回v后也会得到一个空指针错误。它可能与上面的链接有关。

所以这是片段中需要显示读取数据的代码:

public class NewFlinderFragment extends Fragment {

    private DatabaseReference mMyFlindersDatabaseReference;
    private ChildEventListener mChildEventListener;
    private MyFlinderAdapter mMyFlinderAdapter;
    private ListView mMyFlinderListView;
    private MyFlinder myFlinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.new_flinder, container, false);
        mMyFlinderListView = (ListView) v.findViewById(R.id.my_flinders_list_view);

        //instantiate the reference to MyFlinders in Firebase
        mMyFlindersDatabaseReference = MainActivity.mFirebaseDatabase.getReference().child("MyFlinders");

        List<MyFlinder> myFlindersList = new ArrayList<>();
        mMyFlinderAdapter = new MyFlinderAdapter(getContext(), R.layout.my_flinder, myFlindersList);

        MyFlinder myFlinder = new MyFlinder(1,"flinderName",100,1,"Url");
        mMyFlinderAdapter.add(myFlinder);


        mMyFlinderListView.setAdapter(mMyFlinderAdapter);
        mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Toast.makeText(getActivity(), "Boo", Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(), "difszmfcresg", Toast.LENGTH_SHORT).show();
                MyFlinder myFlinder = dataSnapshot.getValue(MyFlinder.class);
                mMyFlinderAdapter.add(myFlinder);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        mMyFlindersDatabaseReference.addChildEventListener(mChildEventListener);

        return v;
    }

}

我在MainActivity中为整个应用程序初始化的数据库(再次注意写入数据库的工作原理并且在此处未列出的另一个片段中)。

public class MainActivity extends AppCompatActivity  {
    public static FirebaseDatabase mFirebaseDatabase;
    // variables for authentication with Firebase
    public static final int RC_SIGN_IN = 1;
    static FirebaseAuth mFirebaseAuth;
    static FirebaseAuth.AuthStateListener mAuthStateListener;

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

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        //instantiate the authentication from Firebase
        mFirebaseAuth = FirebaseAuth.getInstance();
    .....

这是包含listview的膨胀布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="24dp"
        android:paddingTop="24dp"
        android:text="@string/choose_a_flinder_to_grow"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ListView
            android:id="@+id/my_flinders_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@android:color/transparent"
            android:stackFromBottom="true"
            android:transcriptMode="alwaysScroll"
            tools:listitem="@layout/my_flinder" />
    </LinearLayout>

</LinearLayout>

这是列表视图(my_flinder)的个别部分:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_flinder_linear_layout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="24dp"
    android:gravity="center">

    <ImageView
        android:id="@+id/my_flinder_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_no_text_white"
        android:scaleType="centerInside" />
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/my_flinder_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/Blue"
            android:paddingStart="18dp"
            android:paddingTop="20dp"
            android:paddingEnd="6dp"
            android:paddingBottom="6dp"
            android:textSize="20dp"/>

        <TextView
            android:id="@+id/my_flinder_points"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:paddingStart="18dp"
            android:paddingTop="6dp"
            android:paddingEnd="6dp"
            android:paddingBottom="6dp"
            android:textSize="20dp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/my_flinder_money"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/Pink"
        android:paddingStart="20dp"
        android:paddingTop="20dp"
        android:paddingEnd="6dp"
        android:paddingBottom="6dp"
        android:textSize="20dp"/>

</LinearLayout>

这是我的MyFlinderAdapter类:

public class MyFlinderAdapter extends ArrayAdapter<MyFlinder>{
    public MyFlinderAdapter (Context context, int resource, List<MyFlinder> objects){
        super(context, resource, objects);
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.my_flinder, parent, false);
        }

        ImageView myFlinderImage = (ImageView) convertView.findViewById(R.id.my_flinder_image);
        TextView myFlinderName = (TextView) convertView.findViewById(R.id.my_flinder_name);
        TextView myFlinderPoints = (TextView) convertView.findViewById(R.id.my_flinder_points);
        TextView myFlinderMoney = (TextView) convertView.findViewById(R.id.my_flinder_money);

        MyFlinder myFlinder = getItem(position);

        myFlinderImage.setVisibility(View.VISIBLE);
        myFlinderName.setVisibility(View.VISIBLE);
        myFlinderPoints.setVisibility(View.VISIBLE);
        myFlinderMoney.setVisibility(View.VISIBLE);

        myFlinderName.setText(myFlinder.getFlinderName());
        myFlinderPoints.setText(myFlinder.getFlinderRequiredPoints());
        myFlinderMoney.setText((int) myFlinder.getFlinderMoneyValue());

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

myFlinderMoney.setText((int) myFlinder.getFlinderMoneyValue());
那个片段可能会引起问题。由于您提供int,因此setText方法假定您提供的int是资源ID。所以它寻找该资源但无法找到。你应该把String放到那里。因此,它会出现以下错误:

  

07-29 14:27:42.552 4631 4631 E AndroidRuntime:android.content.res.Resources $ NotFoundException:String resource ID#0x64

希望有所帮助!