简体   繁体   English

java.lang.runtimeexception无法实例化接收器

[英]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. 当应用程序扫描信标时,应用程序将更改接收BR(BroadcastReceiver)消息的UI文本。

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. 如果扫描信标,活动类从服务类接收BR消息,并且更改Ui文本。 but a one or two second later app is closed with error log. 但是一个或两秒钟后的应用程序将关闭并显示错误日志。


CODE : MainActivity.java 代码: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 代码: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 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 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. 根据您的代码和错误,您似乎没有com.example.beaconTest.BeaconReceiver类。 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. 您在Java代码中声明的BeaconReceiverMainActivity的私有内部类,因此Android无法创建它的实例。 You would need to use registerReceiver() , instead of the manifest, to register your BeaconReceiver . 您需要使用registerReceiver()而不是清单来注册BeaconReceiver

  • i donot see where the receiver is initialized. 我不知道接收器的初始化位置。 the code is commented out //receiver = new BeaconReceiver(); 代码被注释掉//receiver = new BeaconReceiver();
  • you register the receiver in onCreate but you donot unregister it in on destroy. 你在onCreate注册接收器,但你不要在销毁时注销它。 if your activity closes, its receiver still may receive data and tries to update the gui, that is not valid anymore. 如果您的活动关闭,其接收方仍然可能会收到数据并尝试更新gui,这是无效的。 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是一个内部类。你得到这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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