1

사용자의 메시지, 전화 번호부 조회 및 통화 상태로 진행되는 응용 프로그램을 개발 중입니다.java.lang.RuntimeException : 수신자를 인스턴스화 할 수 없습니다.

애플리케이션이 완벽하게 실행되는 곳에서 애플리케이션 호출이 중단 될 때 애플리케이션이 다운됩니다.

내 사용자가보고 한 오류입니다. 여기

> java.lang.RuntimeException: Unable to instantiate receiver 
> me.radhakrishna.buddyreader.CustomPhoneStateListener: 
> java.lang.InstantiationException: can't instantiate class 
> me.radhakrishna.buddyreader.CustomPhoneStateListener; no empty 
> constructor at 
> android.app.ActivityThread.handleReceiver(ActivityThread.java:2105) at 
> android.app.ActivityThread.access$1500(ActivityThread.java:123) at 
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) 
> at android.os.Handler.dispatchMessage(Handler.java:99) at 
> android.os.Looper.loop(Looper.java:137) at 
> android.app.ActivityThread.main(ActivityThread.java:4450) at 
> java.lang.reflect.Method.invokeNative(Native Method) at 
> java.lang.reflect.Method.invoke(Method.java:511) at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) at 
> dalvik.system.NativeStart.main(Native Method) Caused by: 
> java.lang.InstantiationException: can't instantiate class 
> me.radhakrishna.buddyreader.CustomPhoneStateListener; no empty 
> constructor at java.lang.Class.newInstanceImpl(Native Method) at 
> java.lang.Class.newInstance(Class.java:1319) at 
> android.app.ActivityThread.handleReceiver(ActivityThread.java:2100) 
> ... 10 more 

내 TextMessageReceiver.java 내

CustomPhoneStateListener.java 

파일 여기

package me.radhakrishna.buddyreader; 

import android.content.Context; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.ContactsContract.PhoneLookup; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 

public class CustomPhoneStateListener extends PhoneStateListener { 
    final MainActivity main = new MainActivity(); 
    //private static final String TAG = "PhoneStateChanged"; 
    Context context; //Context to make Toast if required 
    public CustomPhoneStateListener(Context context) { 
     super(); 
     this.context = context; 
    } 

    public void phonePlayer(String incomingNumber){ 
     Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incomingNumber)); 
     Cursor cursor= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null); 

     if (cursor.getCount()>0){ 
      cursor.moveToFirst(); 
      String contactName = cursor.getString(0); 
      cursor.close(); 

      String enteredText = "Incoming call from "+contactName; 
      String words = enteredText.toString(); 
      main.speakWords(words); 
     }else{ 
      String enteredText = "Incoming call from "+incomingNumber; 
      String words = enteredText.toString(); 
      main.speakWords(words); 
     } 
    } 

    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
     super.onCallStateChanged(state, incomingNumber); 

     switch (state) { 
     case TelephonyManager.CALL_STATE_IDLE: 
      //when Idle i.e no call 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 
      //when Off hook i.e in call 
      //Make intent and start your service here 
      break; 
     case TelephonyManager.CALL_STATE_RINGING: 
      //when Ringing 
      main.stopReading(); 
      phonePlayer(incomingNumber); 
      break; 
     default: 
      break; 
     } 
    } 
} 

입니다

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 

내 매니페스트 파일

,536,
<receiver android:name=".CustomPhoneStateListener"> 
    <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
    </intent-filter> 
</receiver> 

아무도 도와 줄 수 있습니까? 미리 감사드립니다.

+0

내가 menifest 파일에 추가 했습니까? –

+0

그래, 내 Manifest에 대해 위에서 보았습니다 –

답변

1

PhoneStateListener는 리시버가 아니므로 매니페스트에 넣을 수 없습니다.

전화 상태가 변경되면 Android에서 android.intent.action.PHONE_STATE 브로드 캐스트를 보냅니다.

따라서해야한다 :

public class CustomPhoneStateReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 
    } 
} 

그리고 매니페스트 파일에 그 수신기를 넣어.

<receiver android:name=".CustomPhoneStateReceiver"> 
    <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
    </intent-filter> 
</receiver> 
+0

정교 할 수 있습니까? 나는 네가 한 말을 못 들었어. –

+0

CustomPhoneStateListener를 매니페스트에 넣습니다. 리시버가 아닙니다. – StarPinkER

+0

여기 CustomPhoneStateListener.java는 PhoneStateListener를 확장합니다. 어떻게 BroadcastReceiver를 확장 할 수 있습니까? 이 자바 파일을 다른 걸릴 수 있습니까? –