我正在構建一個應用程序,它通過自定義contentprovider獲取帶有cursorloader的數據。數據是通過我正在構建的其他應用程序插入的。但是我得到了必須獲取數據的應用程序中的NullPointerException。異常發生在遊標返回null的onLoadFinished()方法中。這真的很奇怪,因爲我按照教程做了完全相同的步驟。如果我使用教程的URI,它將檢索插入的數據,並且遊標不會返回null。但是如果我使用我自己的URI,則遊標返回null。有人可以幫我嗎?在loader中的NullPointerException:onLoadFinished()遊標爲空Android
這裏是應用程序的代碼來檢索數據MainActivity.class:
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
TextView resultView=null;
CursorLoader cursorLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultView= (TextView) findViewById(R.id.res);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClickDisplayNames(View view) {
getSupportLoaderManager().initLoader(1, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] column = {"name"};
cursorLoader= new CursorLoader(this, Uri.parse("content://com.ah.wholesalers.MyProvider/brand"),column, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
cursor.moveToFirst();
StringBuilder res=new StringBuilder();
while (!cursor.isAfterLast()) {
res.append("\n"+ cursor.getString(1));
cursor.moveToNext();
}
resultView.setText(res);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
佈局activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnRetrieve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickDisplayNames"
android:text="Display names" />
<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:clickable="false"
android:ems="10" >
</TextView>
</LinearLayout>
這裏是我的logcat與所有的異常:
11-05 17:23:13.672: E/AndroidRuntime(2044): FATAL EXCEPTION: main
11-05 17:23:13.672: E/AndroidRuntime(2044): Process: com.example.contentprovideruser, PID: 2044
11-05 17:23:13.672: E/AndroidRuntime(2044): java.lang.NullPointerException
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.example.contentprovideruser.MainActivity.onLoadFinished(MainActivity.java:46)
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.example.contentprovideruser.MainActivity.onLoadFinished(MainActivity.java:1)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.Loader.deliverResult(Loader.java:103)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:81)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.os.Looper.loop(Looper.java:136)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-05 17:23:13.672: E/AndroidRuntime(2044): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 17:23:13.672: E/AndroidRuntime(2044): at java.lang.reflect.Method.invoke(Method.java:515)
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-05 17:23:13.672: E/AndroidRuntime(2044): at dalvik.system.NativeStart.main(Native Method)