2014-04-27 13 views
0

我有一个mUsbReceiverBroadcastReceiver)和CameraActivity。接收器setContentView(R.layout.main)CameraActivity经由Intent。然后CamearActivity用这个值更新它的View。请注意,setContentView位于Broadcast接收器类中,不在CameraActivity类中。将setContentView(R.layout.main)移动到广播接收器类

public class CameraActivity extends Activity { 

private static final String TAG = "openXC::Activity"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    usbConnection(); 

} 

public void usbConnection() { 
    UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 


    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_CANCEL_CURRENT); 
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 
    registerReceiver(mUsbReceiver, filter); 
    String txt = "default"; 
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList(); 
    Log.i(TAG, "Device List: " + deviceList); 
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
    UsbDevice device = deviceIterator.next(); 
    Log.i(TAG, "Device List: " + deviceList); 
    mUsbManager.requestPermission(device, mPermissionIntent); 
} 

private static final String ACTION_USB_PERMISSION ="com.ford.openxc.webcam.USB_PERMISSION"; 
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { 

    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (ACTION_USB_PERMISSION.equals(action)) { 
      synchronized (this) { 
       UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 

       if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { 
        if(device != null){ 
         Log.d(TAG, "Displayed Comten View " + device); 
         setContentView(R.layout.main); 

        } 
       } 
       else { 
        Log.d(TAG, "permission denied for device " + device); 
       } 
      } 
     } 
    } 
}; 
} 

这工作有时正常,但有时会引发以下错误

I/openXC::Activity(5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;@421a1f50]} 
I/openXC::Activity(5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;@421a1f50]} 
I/Adreno200-EGLSUB(5609): <ConfigWindowMatch:2087>: Format RGBA_8888. 
E/  (5609): <s3dReadConfigFile:75>: Can't open file for reading 
E/  (5609): <s3dReadConfigFile:75>: Can't open file for reading 
D/openXC::Activity(5609): Displayed Comten View UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;@421d3ed0] 
D/WebcamPreview(5609): WebcamPreview constructed 
+0

有在logcat的片段也不例外。 – CommonsWare

+0

对不起引发以下错误:E /(5609)::无法打开文件以供读取 – user3573951

+0

这似乎没有任何关联。在's3dReadConfigFile'上的搜索变成https://stackoverflow.com/questions/15280681/device-specific-error-s3dreadconfigfile-cant-open-file-for-reading。 – CommonsWare

回答

0

从技术上讲,你可以调用的setContentView您在事件线程上执行任意时间。

否则,您需要使用Handler来调用它。

另外,这里有一些有用的链接,可以帮助你:

link1

link 2

link 3

+1

非常感谢您的快速回复。将研究这些链接 – user3573951

0

我没有对USB之类的事情太多EXP但因为ü说其说不能读取文件..我相信数据错误可能在USB中,所以为了调试目的,我建议将setContentView(int)从if条件移到onRecieve直接所以每当onReceive被调用时,你的contenview将会改变,这将有助于确保错误不会与setcontentview ...在dat你可以看到没有setcontentview在USB中,现在如果错误来了,那么肯定错误是在USB和不在的setContentView ....

希望工程:)

+0

非常感谢您的建议,将尝试, – user3573951

+0

plz upvote然后亲爱的... ... - – Ahmad