0
我m using the ZXing barcode scanner and I have some issues. I
m以下this交集,我不明白我應該在哪裏調用「onActivityResult」方法,以及我需要發送哪個參數。Android條碼掃描器 - 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);
}
}
}
}
任何幫助嗎? 謝謝
謝謝@Marko解決了我的問題,只是爲了理解,回調行爲總是將她的結果返回給mainActivity而不是我稱之爲她的類? –
是的,當使用* getActivity()*從片段提供上下文時,如果活動可以處理回調,它將獲得處理它的第一個機會。 – Marko