我 m using the ZXing barcode scanner and I have some issues. I
我沿着这个交叉点走,我不知道应该在哪里调用“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);
}
}
}
}
有什么帮助吗?谢谢
1条答案
按热度按时间ax6ht2ek1#
托管片段的主活动将获得结果。您应该在mainactivity中调用onactivityresult,并调用super,这样您的片段就有机会处理数据。