2013-04-26 2 views
1

매니페스트 파일에서 수신기 사용을 등록 취소하려고합니다. 나는 ComponentName 객체브로드 캐스트 수신기를 등록 해제하는 동안 NullpointerException이 발생했습니다.

여기

public class MainActivity extends Activity { 
    Button StopB; 
    //Context context; 

오류는 아래 라인에

메인 클래스를 코드에게의를 시작하려고 할 때 나는 link에 주어진 방법을 사용하지만 나에게 널 포인터 예외를 제공합니다 ,

ComponentName component =new ComponentName(getApplicationContext(), BRR.class); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StopB=(Button) findViewById(R.id.button1); 
StopB.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Log.e("Pressed", "UNRGSTR"); 

     } 
    }) ; 

    } 

} 

BRR 클래스

오류 4월 4일에서 26일까지 : 23 : 28.911 : E/AndroidRuntime (516) FATAL EXCEPTION : 메인 Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.deletemessages" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.deletemessages.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> 
     <receiver android:name="BRR"> 
      <intent-filter android:priority="999"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
    </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

Stactrace 여기에서

04-26 04:23:28.911: E/AndroidRuntime(516): FATAL EXCEPTION: main 
04-26 04:23:28.911: E/AndroidRuntime(516): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.deletemessages/com.example.deletemessages.MainActivity}: java.lang.NullPointerException 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.os.Looper.loop(Looper.java:137) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-26 04:23:28.911: E/AndroidRuntime(516): at java.lang.reflect.Method.invokeNative(Native Method) 
04-26 04:23:28.911: E/AndroidRuntime(516): at java.lang.reflect.Method.invoke(Method.java:511) 
04-26 04:23:28.911: E/AndroidRuntime(516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-26 04:23:28.911: E/AndroidRuntime(516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-26 04:23:28.911: E/AndroidRuntime(516): at dalvik.system.NativeStart.main(Native Method) 
04-26 04:23:28.911: E/AndroidRuntime(516): Caused by: java.lang.NullPointerException 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101) 
04-26 04:23:28.911: E/AndroidRuntime(516): at com.example.deletemessages.MainActivity.<init>(MainActivity.java:17) 
04-26 04:23:28.911: E/AndroidRuntime(516): at java.lang.Class.newInstanceImpl(Native Method) 
04-26 04:23:28.911: E/AndroidRuntime(516): at java.lang.Class.newInstance(Class.java:1319) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
04-26 04:23:28.911: E/AndroidRuntime(516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 
04-26 04:23:28.911: E/AndroidRuntime(516): ... 11 more 
+2

logcat에서 stacktrace하시기 바랍니다. –

답변

0

:

ComponentName component =new ComponentName(getApplicationContext(), BRR.class);//<< 

onCreate을 호출하거나 만들기 전에 컨텍스트에 액세스하려고합니다. 활동. 그래서 onCreate 안에 들어가십시오 :

ComponentName component; //<< declare here... 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // initialize here.. 
     component =new ComponentName(getApplicationContext(), BRR.class); 
+0

@prosper 고마워요. – dreamer1989

관련 문제