我m using the ZXing barcode scanner and I have some issues.
I
跟随this交叉点,我不明白我应该在哪里调用“onActivityResult”方法,以及我需要发送哪个参数。
我尝试将调用置于OnClickListener下,但我始终从我的intentResult中获取null。
这是我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
Button scan;
TextView t1, t2;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
scan = (Button)rootView.findViewById(R.id.bScan);
t1 = (TextView) rootView.findViewById(R.id.tv1);
t2 = (TextView) rootView.findViewById(R.id.tv2);
t1.setText("OK");
scan.setOnClickListener(this);
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if (intentResult != null ){
String scanContact = intentResult.getContents();
String scanFormat = intentResult.getFormatName();
String contact = "Contact is: " + scanContact +"\n";
String foramt = "Format is: " + scanFormat;
--- I never get to this part ---
t1.setText(contact);
t2.setText(foramt);
} else {
Toast toast = Toast.makeText(getActivity(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
t1.setText("Not OK");
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.bScan) {
IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());
intentIntegrator.initiateScan();
// Intent intent = getActivity().getIntent();
//startActivityForResult(intent,getTargetRequestCode());
//onActivityResult(getTargetRequestCode(),CONTEXT_INCLUDE_CODE,intent);
}
}
}
}
有任何帮助吗? 感谢
答案 0 :(得分:0)
托管您的片段的 MainActivity 会获得结果。你应该在MainActivity中调用 onActivityResult ,然后调用super,这样你的Fragment就有机会处理数据。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}