Reputation: 5101
From a Fragment I am calling another fragment that reads barcodes using the camera.
This is how am I calling the scan fragment:
public void scanNow(View view){
// add fragment
ScanFragment firstFragment = new ScanFragment();
((MainActivityDriver)getActivity()).getSupportFragmentManager().beginTransaction()
.add(R.id.frame, firstFragment).commit();
}
public void scanResultData(String codeFormat, String codeContent){
// display it on screen
txtCode.setText("CONTENT: " + codeContent);
}
public void scanResultData(NoScanResultException noScanData) {
Toast toast = Toast.makeText(getActivity(),noScanData.getMessage(), Toast.LENGTH_SHORT);
toast.show();
}
The camera is working fine and scans the barcode, but then I am getting an exception at onactivityresult method at fragment scanfragment:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
ScanResultReceiver parentActivity = (ScanResultReceiver) this.getActivity();
if (scanningResult != null) {
//we have a result
codeContent = scanningResult.getContents();
codeFormat = scanningResult.getFormatName();
// send received data
parentActivity.scanResultData(codeFormat,codeContent);
}else{
// send exception
parentActivity.scanResultData(new NoScanResultException(noResultErrorMsg));
}
}
at line:
ScanResultReceiver parentActivity = (ScanResultReceiver) this.getActivity();
This is the error:
Caused by: java.lang.ClassCastException: com.juarezserver.sdocksdriver.activity.MainActivityDriver cannot be cast to com.juarezserver.sdocksdriver.fragment.ScanResultReceiver
ScanResultReceiver is as follows:
public interface ScanResultReceiver {
/**
* function to receive scanresult
* @param codeFormat format of the barcode scanned
* @param codeContent data of the barcode scanned
*/
public void scanResultData(String codeFormat, String codeContent);
public void scanResultData(NoScanResultException noScanData);
}
How could I get it working?
Upvotes: 0
Views: 1339
Reputation: 1221
You need to implement the interface ScanResultReceiver
in MainActivityDriver
public static class MainActivityDriver extends Activity
implements ScanResultReceiver{
...
public void scanResultData(String codeFormat, String codeContent) {
//handle result
}
public void scanResultData(NoScanResultException noScanData) {
//handle exception
}
}
Also, I will recommend using a global callback variable in your fragment to avoid NullPointerException
,
public class ScanFragment{
ScanResultReceiver resultCallback;
public interface ScanResultReceiver {
public void scanResultData(String codeFormat, String codeContent);
public void scanResultData(NoScanResultException noScanData);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
resultCallback = (ScanResultReceiver) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ScanResultReceiver");
}
}
...
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
codeContent = scanningResult.getContents();
codeFormat = scanningResult.getFormatName();
// send received data
resultCallback.scanResultData(codeFormat,codeContent);
}else{
// send exception
resultCallback.scanResultData(new NoScanResultException(noResultErrorMsg));
}
}
}
Upvotes: 1