위해 나는이 TutorialPoint Link과 나는이 Tutorial의 지시를 따랐다 아직 내가 실행할 수 아니에요 불구하고 찾을 수 있습니다 내 컨텐트 프로 프로젝트와 함께 컨텐트 리졸버를 사용하려고 해요 프로젝트, ContentProvider의 전체 저장된 데이터가 textview에 나열 될 것으로 기대하고 있습니다.
java.lang.NullPointerException이는 컨텐트 리졸버
MainActivity.java 컨텐트 리졸버의 내용 리졸버의
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtView = (TextView) findViewById(R.id.textView1);
/* Content Resolver Declaration */
ContentResolver resolver = getContentResolver();
String[] projection = new String[]{BaseColumns._ID,UserDictionary.Words.WORD};
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI,
projection, null, null, null);
if (cursor.moveToFirst()){
do{
long id = cursor.getLong(0);
String word = cursor.getString(1);
txtView.setText(id+"--"+word);
}while(cursor.moveToNext());
}
}
}
AndoridManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycontentresolver"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.example.mycontentprovider" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mycontentresolver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mycontentresolver.MainActivity" tools:ignore="MergeRootFrame" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/largeTxt" android:textAppearance="?android:attr/textAppearanceLarge" /> </FrameLayout>
컨텐트 프로 패키지 + 제공자
package com.example.mycontentprovider;
public class StudentsProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.example.provider.College";
static final String URL = "content://" + PROVIDER_NAME + "/students";
스택 트레이스
06-11 01:01:50.001: E/AndroidRuntime(4790): FATAL EXCEPTION: main
06-11 01:01:50.001: E/AndroidRuntime(4790): Process: com.example.mycontentresolver, PID: 4790
06-11 01:01:50.001: E/AndroidRuntime(4790): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mycontentresolver/com.example.mycontentresolver.MainActivity}: java.lang.NullPointerException
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.ActivityThread.access$800(ActivityThread.java:156)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.os.Looper.loop(Looper.java:157)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.ActivityThread.main(ActivityThread.java:5872)
06-11 01:01:50.001: E/AndroidRuntime(4790): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 01:01:50.001: E/AndroidRuntime(4790): at java.lang.reflect.Method.invoke(Method.java:515)
06-11 01:01:50.001: E/AndroidRuntime(4790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
06-11 01:01:50.001: E/AndroidRuntime(4790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
06-11 01:01:50.001: E/AndroidRuntime(4790): at dalvik.system.NativeStart.main(Native Method)
06-11 01:01:50.001: E/AndroidRuntime(4790): Caused by: java.lang.NullPointerException
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.Activity.findViewById(Activity.java:1952)
06-11 01:01:50.001: E/AndroidRuntime(4790): at com.example.mycontentresolver.MainActivity.<init>(MainActivity.java:14)
06-11 01:01:50.001: E/AndroidRuntime(4790): at java.lang.Class.newInstanceImpl(Native Method)
06-11 01:01:50.001: E/AndroidRuntime(4790): at java.lang.Class.newInstance(Class.java:1208)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
06-11 01:01:50.001: E/AndroidRuntime(4790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
06-11 01:01:50.001: E/AndroidRuntime(4790): ... 11 more
내가 제공 많은 솔루션은 시도를 변경 시도하지만 난 내 코드에없는 뭔가가있어 믿고 고정해야 , 당신의 응답을 감사하십시오.
업데이트 : 아래 해결 방법에 따라 첫 번째 문제가 수정되었으며 새로운 TrackTrace가 수정되었으며 + ContentProvider의 이름이 변경되었거나 변경해야하는 경우 + 및 내 라이브러리에 AppCombat v7_6이 있음 기본적으로 지원됩니다.
포스트 – dymmeh
잊지 사과 전체 스택 트레이스 덧붙였다. –
MainActivity의 14 번 줄은 무엇입니까? – elbaulp