I cant implement CursorLoader because of Fatal Error

Moahemd

New Member
#1
I am trying to create Fragment of ListView within Activity to interact with CursorLoader to get data from sqlite database and load it synchronously into listview which within Frgament

code of activity :
Mã:
package com.example.startup.myfriends;

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends AppCompatActivity {

FloatingActionButton floatingActionButton;

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

    floatingActionButton = findViewById(R.id.mainActivityAddFriendFloatingButton);

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getBaseContext(), AddFriend.class));
        }
    });

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.familyLayoutContainer, new FamilyListViewFragment()).commit();
}


private void setToolbar() {
    android.support.v7.widget.Toolbar toolbar = findViewById(R.id.mainActivityToolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Family");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_add_friend, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("You'll Delete All Items. Are You Sure ?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });

    builder.show();
    return super.onOptionsItemSelected(item);
}
}
Fragment CursorLoader Code :
Mã:
package com.example.startup.myfriends;

import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.startup.myfriends.data.AppContract;
import com.example.startup.myfriends.data.FamilyListCursorAdapter;

public class FamilyListViewFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
    ListView listView;
    FamilyListCursorAdapter familyListCursorAdapter;
    private Cursor mDetailCursor;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_family_list_view, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = getActivity().findViewById(R.id.listView);


        final String projection [] = {
                AppContract.Family.COLUMN_ID,
                AppContract.Family.COLUMN_NAME,
                AppContract.Family.COLUMN_LAST_NAME,
                AppContract.Family.COLUMN_AGE,
                AppContract.Family.COLUMN_GENDER
        };

        Cursor cursor = getActivity().getContentResolver().query(AppContract.Family.CONTENT_URI,
                projection, null, null, null);

        /*

        familyListCursorAdapter = new FamilyListCursorAdapter(getActivity().getBaseContext(), cursor);
        listView.setAdapter(familyListCursorAdapter);
        */

        familyListCursorAdapter = new FamilyListCursorAdapter(getActivity().getBaseContext(), cursor);
        listView.setAdapter(familyListCursorAdapter);

        getLoaderManager().initLoader(1, null, this);


    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        Uri CONTACT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        CursorLoader cursorLoader = new CursorLoader(getActivity(), CONTACT_URI, null,
                null, null, null);

        /*
        cursor.moveToFirst();

        */

        return cursorLoader;

    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
       familyListCursorAdapter.swapCursor(cursor);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        familyListCursorAdapter.swapCursor(null);
    }
}
MyApp Always is crashed like this :

ozAr6.png

Crashed App snap shot

w4wfv.png

Error Message snap shot​

Please any help am trying to fix it am trying from yesterday to fix it by myself but I quit :(
 

Admin

Administrator
Thành viên BQT
#2
it reads that fragment_family_list_view.xml (your content) must have a ListView whose id attribute is android.R.id.list; which is not the same as (package name) R.id.list.

in XML that is <ListView android:id="@android:id/list" ...
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top