java.lang.NullPointerException:尝试调用虚方法' java.lang.String android.net.Uri.getScheme()'在空对象引用上
我想知道如何在列表视图中只删除一个元素 我添加alertDialog,当用户在列表中的元素上单击long时,会出现alertdialog
删除方法中的错误
private void deleteAlert() {
int rowsDeleted = getActivity().getContentResolver().delete(mCurrentPetUri, null, null); //error
if (rowsDeleted == 0) {
Toast.makeText(getActivity(), getString(R.string.editor_delete_alert_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), getString(R.string.editor_delete_alert_successful),
Toast.LENGTH_SHORT).show();
}
}
这个片段
package com.amira.amira.amira.ChatCalendarProfile.Calendar;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.content.ContentUris;
import android.content.ContentValues;
import android.widget.Toast;
import com.amira.amira.amira.R;
import static com.amira.amira.amira.ChatCalendarProfile.Calendar.data.AlertContract.*;
public class CalendarFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int ALERT_LOADER = 0;
AlertCursorAdapter mCursorAdapter;
private Uri mCurrentPetUri;
public CalendarFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_calendar_fragment, container, false);
com.github.clans.fab.FloatingActionButton addAlert = rootView.findViewById(R.id.add_alert);
addAlert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), EditorActivity.class);
startActivity(intent);
}
});
ListView AlertListView = (ListView) rootView.findViewById(R.id.list);
View emptyView = rootView.findViewById(R.id.empty);
AlertListView.setEmptyView(emptyView);
Intent intent = getActivity().getIntent();
mCurrentPetUri = intent.getData();
AlertListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, final View arg1, final int pos, long id) {
showDeleteConfirmationDialog();
return true;
}
});
mCursorAdapter = new AlertCursorAdapter(getActivity(), null);
AlertListView.setAdapter(mCursorAdapter);
AlertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(getActivity(), EditorActivity.class);
Uri currentAlertUri = ContentUris.withAppendedId(AlertEntry.CONTENT_URI, id);
intent.setData(currentAlertUri);
startActivity(intent);
}
});
getLoaderManager().initLoader(ALERT_LOADER, null, this);
return rootView;
}
private void showDeleteConfirmationDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.delete_dialog_msg);
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
deletePet();
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
AlertEntry._ID,
AlertEntry.COLUMN_ALERT_TITLE,
AlertEntry.COLUMN_ALERT_LOCATION,
AlertEntry.COLUMN_ALERT_OCCASION_DATE,
AlertEntry.COLUMN_ALERT_OCCASION_TIME};
return new CursorLoader(getActivity(), // Parent activity context
AlertEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null);
}
private void deletePet() {
int rowsDeleted = getActivity().getContentResolver().delete(mCurrentPetUri, null, null);
if (rowsDeleted == 0) {
Toast.makeText(getActivity(), getString(R.string.editor_delete_alert_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), getString(R.string.editor_delete_alert_successful),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mCursorAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}
答案 0 :(得分:0)
在您开始活动时检查您使用setData
方法设置的数据,因为intent.getData()
现在是null