在AlertDialog崩溃期间从asyncTask启动新活动

时间:2016-03-09 18:19:39

标签: android android-asynctask

正如这个问题的标题所说,我的代码如下:

protected void PayMessageBox(final String Type) {

    AlertDialog alertbox = new AlertDialog.Builder(context)
            .setMessage("are you sure?")
            .setPositiveButton("yes",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                            new AsyncGetAccountInfo().execute(Type);
                        }
                    })
            .setNegativeButton("no",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                        }
                    }).show();


}

在AsyncGetAccountInfo()中,我有以下代码:

protected class AsyncGetAccountInfo extends AsyncTask<String, Void, String> {

    // private Boolean Havemoney = true;

    @Override
    protected String doInBackground(String... params) {
        try {

            String allPlace = null;
            RestAPI api = new RestAPI();
            try {
                SharedPreferences sharedPreferences = getSharedPreferences(
                        "MyData", context.MODE_PRIVATE);
                String E_MSISDN = sharedPreferences.getString("MSISDN",
                        "N/A");

                EncryptDecryptWithDESUsingPassPhrase security = new EncryptDecryptWithDESUsingPassPhrase();

                // E_MSISDN=security.encrypt(E_MSISDN);
                String E_ACTIVATIONCODE = sharedPreferences.getString(
                        "ACTIVATIONCODE", "N/A");
                Long ReqId = sharedPreferences.getLong("REQID", -1);

                String Key = ReqId + E_ACTIVATIONCODE;
                security.setKey(Key);
                String E_IMEI = security.encrypt(IMEI);
                String E_TYPE = security.encrypt(params[0]);

                JSONObject jsonObj = api.ReduceCharge(E_IMEI,E_MSISDN,
                        E_TYPE);
                JSONParser parser = new JSONParser();
                allPlace = parser.parseReduceCharge(jsonObj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                // Log.d("AsyncUserDetails", e.getMessage());
                return null;
            }

            return allPlace;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            // Log.d("AsyncUserDetails", e.getMessage());
            return null;
        }

        // return Havemoney;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        if (result != null) {

            SharedPreferences sharedPreferences = getSharedPreferences(
                    "MyData", context.MODE_PRIVATE);
            Long ReqId = sharedPreferences.getLong("REQID", -1);

            EncryptDecryptWithDESUsingPassPhrase security = new EncryptDecryptWithDESUsingPassPhrase();
            String E_ACTIVATIONCODE = sharedPreferences.getString(
                    "ACTIVATIONCODE", "N/A");
            ReqId = sharedPreferences.getLong("REQID", -1);
            String Key = ReqId + E_ACTIVATIONCODE;
            security.setKey(Key);

            if (security.decrypt(result).equals("1")) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putLong("REQID", ++ReqId);
                editor.commit();
                // new AsyncSaveMobileAndActivationCode().execute(result);
                dialog.dismiss();
                new AsyncPersonIsPaid().execute();
                if(true)
                    {
                    //new AsyncUserPackagepaid().execute();

                    }

                if (SingleVideos.size() > 0) {
                    // progressBar.startAnimation(set);
                    DownloadVideo(SingleVideos.get(0).VIDEOURL);
                }

            } else if (security.decrypt(result).equals("-2")) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putLong("REQID", ++ReqId);
                editor.commit();

                Toast.makeText(context,
                        "low balance",
                        Toast.LENGTH_LONG).show();
            } else {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putLong("REQID", ++ReqId);
                editor.commit();

                Toast.makeText(
                        context,
                        "try again",
                        Toast.LENGTH_LONG).show();
            }

        } else {
            Toast.makeText(
                    context,
                    "db down, try later",
                    Toast.LENGTH_LONG).show();
            // LL_Register_Button.setClickable(true);
        }

    }

}

代码到达以下点后:

if (SingleVideos.size() > 0) {
                    // progressBar.startAnimation(set);
                    DownloadVideo(SingleVideos.get(0).VIDEOURL);
                }

调用DownloadVideo,其代码如下:

private void DownloadVideo(final String VIDEO_URL) {

    try {

        if (VIDEO_URL != null && VIDEO_URL.length() > 0) {

            File file = new File(Environment
                    .getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_MOVIES).getAbsolutePath());

            VideoPath = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_MOVIES).getAbsolutePath()
                    + "/" + Uri.parse(VIDEO_URL).getLastPathSegment();

            getfile(file);

            int countVideo = 0;
            for (File video_name : fileList) {
                if (video_name.getName().equals(
                        Uri.parse(VIDEO_URL).getLastPathSegment()))
                    countVideo++;
            }
            if (countVideo > 0) {

                PersonInfo.mMediaPlayer.pause();
                Intent intent = new Intent(
                        android.content.Intent.ACTION_VIEW);
                Uri myUri = Uri.parse(VideoPath);
                String VideoExtention = VideoPath.substring(0,
                        VideoPath.lastIndexOf('.'));
                String extentions = "video/"
                        + VideoPath.substring(VideoExtention.length() + 1);
                intent.setDataAndType(myUri, extentions);
                startActivity(intent);

            } else {

                if (IsOnline) {// stream video
                    Intent intent = new Intent(context,
                            PlayVideoActivity.class);
                    intent.putExtra("VIDEOPATH", VIDEO_URL);
                    intent.putExtra("ISONLINE", true);
                    startActivity(intent);
                }

                if (IsOnline == false) {// donwload video
                    float VideoSize = 0f;
                    String last2 = (TV_VideoSize.getText() == null
                            || TV_VideoSize.length() < 2 ? "0"
                            : ((String) TV_VideoSize.getText())
                                    .substring(TV_VideoSize.getText()
                                            .length() - 2));

                    last2 = last2.trim();
                    String NewString = ((String) TV_VideoSize.getText())
                            .replaceAll(last2, "");
                    if (last2.equals("KB")) {
                        VideoSize = Float.valueOf(NewString) * 1024f;
                    } else if (last2.equals("MB")) {
                        VideoSize = Float.valueOf(NewString) * 1024f * 1024f;
                    } else if (last2.equals("GB")) {
                        VideoSize = Float.valueOf(NewString) * 1024f * 1024f * 1024f;
                    } else {
                        VideoSize = Float.valueOf(NewString);// byte
                    }

                    StatFs stat = new StatFs(Environment
                            .getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_MOVIES)
                            .getAbsolutePath());

                    float bytesAvailable = (float) stat.getBlockSize()
                            * (float) stat.getAvailableBlocks();

                    if (bytesAvailable > VideoSize) {
                        myTask = new MyTask();
                        myTask.execute(VIDEO_URL);
                        // myTask.cancel(true);
                    } else {

                    }
                }

            }

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(context, "Connection Error", Toast.LENGTH_SHORT)
                .show();
        e.printStackTrace();
    }
}

代码进入上述代码的这一部分:

if (IsOnline) {// stream video
                    Intent intent = new Intent(context,
                            PlayVideoActivity.class);
                    intent.putExtra("VIDEOPATH", VIDEO_URL);
                    intent.putExtra("ISONLINE", true);
                    startActivity(intent);
                }

问题在于上面代码中的startActivity(intent),它基本上是VideoView的活动,甚至是它的onCreate被调用,当它想要执行videoView.start()方法时,它会崩溃。

出了什么问题?

0 个答案:

没有答案