由于致命错误,我无法实施 CursorLoader
I cant implement CursorLoader because of Fatal Error
我正在尝试在 Activity 中创建 ListView 的片段以与 CursorLoader 交互以从 sqlite 数据库获取数据并将其同步加载到列表视图中,该列表视图在
中
activity 代码:
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);
}
}
片段 CursorLoader 代码:
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 总是这样崩溃:Crashed App snap shot
Error Message snap shot
请提供任何帮助,我正在尝试修复它,我从昨天开始尝试自己修复它,但我退出了:(
上面写着fragment_family_list_view.xml
(你的内容)必须有一个ListView
,其id属性是android.R.id.list
;这与(包名称)R.id.list
.
不同
在XML中即<ListView android:id="@android:id/list" ...
我正在尝试在 Activity 中创建 ListView 的片段以与 CursorLoader 交互以从 sqlite 数据库获取数据并将其同步加载到列表视图中,该列表视图在
中activity 代码:
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);
}
}
片段 CursorLoader 代码:
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 总是这样崩溃:Crashed App snap shot
Error Message snap shot
请提供任何帮助,我正在尝试修复它,我从昨天开始尝试自己修复它,但我退出了:(
上面写着fragment_family_list_view.xml
(你的内容)必须有一个ListView
,其id属性是android.R.id.list
;这与(包名称)R.id.list
.
在XML中即<ListView android:id="@android:id/list" ...