活动不能转换为其他活动

时间:2018-01-26 02:21:23

标签: android android-fragments

从片段中我正在调用另一个使用相机读取条形码的片段。

这是我如何调用扫描片段:

 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();
    }

相机工作正常并扫描条形码,但是我在片段scanfragment的onactivityresult方法中得到例外:

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));
        }
    }

在线:

  ScanResultReceiver parentActivity = (ScanResultReceiver) this.getActivity();

这是错误:

Caused by: java.lang.ClassCastException: com.juarezserver.sdocksdriver.activity.MainActivityDriver cannot be cast to com.juarezserver.sdocksdriver.fragment.ScanResultReceiver

ScanResultReceiver如下:

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);
}

我怎么能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

您需要在ScanResultReceiver

中实施界面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
    }

}

另外,我建议在片段中使用全局回调变量以避免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));
        }
    }
}