简体   繁体   中英

java.lang.runtimeexception unable to instantiate receiver

I'm developing an appliacation that scans the beacon. when application scans the beacon, application will change the UI Text that received the BR(BroadcastReceiver) message.

After app starts, If Button is clicked, service is started. and If beacon is scanned, activity class received BR message from service class and Ui Text is changed. but a one or two second later app is closed with error log.


CODE : MainActivity.java

public class MainActivity extends Activity {
Button start;
TextView text;
private final String SERVER_ADDRESS = "";
Handler handler;
BeaconReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start = (Button) findViewById(R.id.start);
    text = (TextView)findViewById(R.id.text);
    receiver = new BeaconReceiver();
    IntentFilter filter = new IntentFilter("com.example.beaconTEST.TEST");
    registerReceiver(receiver, filter);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    phpconnect();
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent;
            intent = new Intent(MainActivity.this, BeaconService.class);
            startService(intent);
        }
    });
}
private class BeaconReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
            Log.i("receive", "success");
            String result = intent.getStringExtra("result");
            text.setText(result);
    }

}

public void phpconnect(){
    runOnUiThread(new Runnable() {
        @Override public void run() {
            try{                        
                URL url = new URL(SERVER_ADDRESS + "/Beacon_Infor.php?");
                Log.i("url","url : "+url);
                url.openStream();
                Log.i("stream","success");
            }catch(Exception e){
                Log.e("Error", "Error : " + e.getMessage());
            }   

        }       
    });

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void onDestroy() {
    super.onDestroy();
    Intent intent;
    intent = new Intent(MainActivity.this, BeaconService.class);
    stopService(intent);
}

}

CODE : BeaconService.java

public class BeaconService extends Service {
CentralManager centralManager;
Handler handler;
Thread t;   
String result;
int count=0;
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
public void onCreate(){
    super.onCreate();
    setCentralManager();
    handler = new Handler(Looper.getMainLooper());
    t = new Thread(new Runnable() { 
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    centralManager.startScanning();
                }
            });
        }
    });
    Log.i("Service", "Start");
    Toast.makeText(this, "Service Start", Toast.LENGTH_SHORT).show();
    XmlParser xmlGetter = new XmlParser();
    result = xmlGetter.getXmlData("result.xml", "uuid");
    Log.i("Beacon", "name : " + result );
}
public void onDestroy(){
    Toast.makeText(this, "Service End", Toast.LENGTH_SHORT).show();
    if(centralManager.isScanning()) {
        centralManager.stopScanning();
    }
    centralManager.close();
    super.onDestroy();
}
public int onStartCommand(Intent intent, int flags, int startId){
    Log.i("onStartCommand", "Start");
    t.start();
    return START_STICKY;
}

public void setCentralManager() {
    centralManager = CentralManager.getInstance();
    centralManager.init(getApplicationContext());
    centralManager.setPeripheralScanListener(new PeripheralScanListener() {         
        @Override
        public void onPeripheralScan(Central central, final Peripheral peripheral) {
            if(result.equals(peripheral.getProximityUUID()))
            {
                Log.i("count","count : "+ count);
                if(count ==0)
                {
                Log.i("uuid", "result : "+result);
                Intent resultIntent = new Intent("com.example.beaconTEST.TEST");
                resultIntent.putExtra("result", result);
                //sendBroadcast(resultIntent);
                count++;
                }


            }
        }           
    });
}

}

AndroidManifest.xml

<receiver android:exported="false" android:name="com.example.beaconTest.BeaconReceiver">
        <intent-filter >
            <action android:name="com.example.beaconTEST.TEST"/>
        </intent-filter>
    </receiver>
    <service android:name="com.example.becaontest.BeaconService">
    </service>

LOG

enter code here03-30 20:25:22.088: I/ViewRootImpl(22750): ViewRoot's Touch   
               Event : Touch Down
               03-30 20:25:22.148: I/ViewRootImpl(22750): ViewRoot's Touch Event : Touch UP
               03-30 20:25:22.178: I/Service(22750): Start
               03-30 20:25:22.218: I/Beacon(22750): name : d5756247-57a2-4344-915d-9599497940a7
               03-30 20:25:22.218: I/onStartCommand(22750): Start
               03-30 20:25:22.258: D/BluetoothAdapter(22750): stopLeScan()
               03-30 20:25:22.258: D/BluetoothAdapter(22750): startLeScan(): null
               03-30 20:25:22.258: D/BluetoothAdapter(22750): onClientRegistered() - status=0 clientIf=6
               03-30 20:25:23.268: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-88
               03-30 20:25:23.278: I/count(22750): count : 0
               03-30 20:25:23.278: I/uuid(22750): result : d5756247-57a2-4344-915d-9599497940a7
               03-30 20:25:23.278: I/receive(22750): success
               03-30 20:25:23.278: I/result(22750): result : d5756247-57a2-4344-915d-9599497940a7
               03-30 20:25:24.228: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-62
               03-30 20:25:24.238: I/count(22750): count : 1
               03-30 20:25:25.228: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-53
               03-30 20:25:25.238: I/count(22750): count : 1
               03-30 20:25:26.238: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-53
               03-30 20:25:26.248: I/count(22750): count : 1
               03-30 20:25:27.248: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-63
               03-30 20:25:27.248: I/count(22750): count : 1
               03-30 20:25:28.248: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-63
               03-30 20:25:28.268: I/count(22750): count : 1
               03-30 20:25:28.268: D/AndroidRuntime(22750): Shutting down VM
               03-30 20:25:28.268: W/dalvikvm(22750): threadid=1: thread exiting with uncaught exception (group=0x4188de48)
               03-30 20:25:28.278: E/AndroidRuntime(22750): FATAL EXCEPTION: main
               03-30 20:25:28.278: E/AndroidRuntime(22750): Process: com.example.becaontest, PID: 22750
               03-30 20:25:28.278: E/AndroidRuntime(22750): java.lang.RuntimeException: Unable to instantiate receiver com.example.beaconTest.BeaconReceiver: java.lang.ClassNotFoundException: Didn't find class "com.example.beaconTest.BeaconReceiver" on path: DexPathList[[zip file "/data/app/com.example.becaontest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.becaontest-2, /vendor/lib, /system/lib]]
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2408)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.app.ActivityThread.access$1700(ActivityThread.java:142)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.os.Handler.dispatchMessage(Handler.java:102)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.os.Looper.loop(Looper.java:136)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.app.ActivityThread.main(ActivityThread.java:5120)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at java.lang.reflect.Method.invokeNative(Native Method)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at java.lang.reflect.Method.invoke(Method.java:515)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at dalvik.system.NativeStart.main(Native Method)
               03-30 20:25:28.278: E/AndroidRuntime(22750): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.beaconTest.BeaconReceiver" on path: DexPathList[[zip file "/data/app/com.example.becaontest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.becaontest-2, /vendor/lib, /system/lib]]
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2403)
               03-30 20:25:28.278: E/AndroidRuntime(22750):     ... 10 more
               03-30 20:25:29.258: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-65
               03-30 20:25:29.268: I/count(22750): count : 1
               03-30 20:25:30.278: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-66
               03-30 20:25:30.278: I/count(22750): count : 1
               03-30 20:25:32.288: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-58
               03-30 20:25:32.298: I/count(22750): count : 1
               03-30 20:25:33.298: D/BluetoothAdapter(22750): onScanResult() - Device=D0:39:72:A4:96:95 RSSI=-57
               03-30 20:25:33.308: I/count(22750): count : 1

Based on your code and error, you do not appear to have a com.example.beaconTest.BeaconReceiver class. The BeaconReceiver that you declare in your Java code is a private inner class of MainActivity , and therefore Android cannot create an instance of it. You would need to use registerReceiver() , instead of the manifest, to register your BeaconReceiver .

  • i donot see where the receiver is initialized. the code is commented out //receiver = new BeaconReceiver();
  • you register the receiver in onCreate but you donot unregister it in on destroy. if your activity closes, its receiver still may receive data and tries to update the gui, that is not valid anymore. This will crash your app.
  • since you are (un-)registering your receiver yourself, remove it from the manifest.

在你的Android Manifest文件中,你已经在com.example.beaconTest包中声明了com.example.beaconTest.BeaconReceiver,但BeaconReceiver的实际位置是com.example.beaconTest.MainActivity $ BeaconReceiver,因为BeaconReceiver是一个内部类。你得到这个错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM