简体   繁体   English

使用Android上的意图问题

[英]Issue using intents on Android

I have an activity which behaves like this: A barcode scanner is called via an IntentIntegrator object, it reads an URI. 我的活动表现如下:条形码扫描程序通过IntentIntegrator对象调用,它读取一个URI。 Then I want to pass the yielded URI into an object called JunctionInterface via its method connect(). 然后我想通过它的方法connect()将生成的URI传递给一个名为JunctionInterface的对象。

private URI uri=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.initiateScan();
    if(uri!=null) {
        JunctionInterface.connect(JunctionInterface.JX,this, uri);
    }
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
  if (scanResult != null) {
     try {
        uri=new URI(scanResult.getContents());
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
  }
}

When I debug I can see that the uri is clearly retrieved when calling uri=new URI(scanResult.getContents()). 当我调试时,我可以看到在调用uri = new URI(scanResult.getContents())时可以清楚地检索到uri。 Unfortunately, JunctionInterface.connect() is not triggered. 不幸的是,JunctionInterface.connect()没有被触发。 Even worse, when I try to put something as simple as int i=0 after integrator.initiateScan() (which mainly uses activity.startActivityForResult(intentScan, REQUEST_CODE)), the application forces close. 更糟糕的是,当我尝试在integrator.initiateScan()(主要使用activity.startActivityForResult(intentScan,REQUEST_CODE))之后放入像int i = 0这样简单的东西时,应用程序强制关闭。

What is the matter with this code ? 这段代码有什么问题? Is there a way to fix it and make it behave like I want ? 有没有办法解决它并使其表现得像我想要的? Thanks. 谢谢。

Try this also. 试试这个吧。 I simply moved a part of your code in other place. 我只是将代码的一部分移到了其他地方。

private URI uri=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
  if (scanResult != null) {
     try {
        uri=new URI(scanResult.getContents());
        if(uri!=null) {
            JunctionInterface.connect(JunctionInterface.JX,this, uri);
        }
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
  }
}

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

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