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

时间:2015-03-30 11:35:28

标签: java android

我正在开发扫描信标的应用程序。 当应用程序扫描信标时,应用程序将更改收到BR(BroadcastReceiver)消息的UI文本。

应用启动后,如果单击按钮,则启动服务。 如果扫描信标,活动类从服务类接收BR消息,并且更改Ui文本。 但是一个或两秒钟后的应用程序将关闭并显示错误日志。


代码: 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);
}

}

代码: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

3 个答案:

答案 0 :(得分:2)

根据您的代码和错误,您似乎没有com.example.beaconTest.BeaconReceiver类。您在Java代码中声明的BeaconReceiverMainActivity的私有内部类,因此Android无法创建它的实例。您需要使用registerReceiver()代替清单来注册BeaconReceiver

答案 1 :(得分:0)

  • 我不知道接收器的初始化位置。代码已注释掉//receiver = new BeaconReceiver();
  • 您在onCreate中注册了接收者,但是您没有在销毁时取消注册。如果您的活动关闭,其接收方仍然可能会收到数据并尝试更新gui,这是无效的。这会使你的应用程序崩溃。
  • 由于您(自己)自行注册接收器,请将其从清单中删除。

答案 2 :(得分:0)

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