Android-在Webview中启用javascript会使应用程序强制关闭

问题描述

|| 我有一个应用程序,其中在对话框中弹出Webview。我需要启用Javascript,但这会导致应用强制关闭。 我尝试将其放置在许多位置,包括OnCreate以及按下打开Webview对话框的按钮时。仍然会导致应用强制关闭。有什么想法吗? 代码和日志如下:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Path;
//import android.graphics.PorterDuff;
//import android.graphics.PorterDuffXfermode;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
 //import android.widget.Button;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.almondmendoza.R;
import com.almondmendoza.drawings.brush.Brush;
//import com.almondmendoza.drawings.brush.CircleBrush;
import com.almondmendoza.drawings.brush.PenBrush;

import java.io.File;
import java.io.FileOutputStream;



public class DrawingActivity extends Activity implements View.OnTouchListener{
    private DrawingSurface drawingSurface;
    private DrawingPath currentDrawingPath;
    private Paint currentPaint;

    private ImageButton redobtn;
    private ImageButton undobtn;
    WebView webview;
    private Brush currentBrush;





    private File APP_FILE_PATH = new File(\"/sdcard/TutorialForAndroidDrawings\");

LinearLayout calendarLayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawing_activity);

      calendarLayout = (LinearLayout) findViewById(R.id.calendarLayout);


webview.getSettings().setJavaScriptEnabled(true);      



        setCurrentPaint();
        currentBrush = new PenBrush();

        drawingSurface = (DrawingSurface) findViewById(R.id.drawingSurface);
        drawingSurface.setonTouchListener(this);
        drawingSurface.previewPath = new DrawingPath();
        drawingSurface.previewPath.path = new Path();
        drawingSurface.previewPath.paint = getPreviewPaint();

        redobtn = (ImageButton) findViewById(R.id.redobtn);
        undobtn = (ImageButton) findViewById(R.id.undobtn);

        redobtn.setEnabled(false);
        undobtn.setEnabled(false);


        //SEEKBAR THICKnesS CHANGE
        //
        //
        SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
        final TextView seekBarValue = (TextView)findViewById(R.id.seekValue);

        seekBar.setonSeekBarchangelistener(new SeekBar.OnSeekBarchangelistener(){

            @Override
            public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){
                seekBarValue.setText(String.valueOf(progress));
                currentPaint.setstrokeWidth(progress);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar){

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar){

            }
        });
        }
    //END SEEKBAR THICKnesS





    private void setCurrentPaint(){
        currentPaint = new Paint();
        currentPaint.setDither(true);
        currentPaint.setColor(0xFFFFFFFF);
        currentPaint.setStyle(Paint.Style.stroke);
        currentPaint.setstrokeJoin(Paint.Join.ROUND);
        currentPaint.setstrokeCap(Paint.Cap.ROUND);
        currentPaint.setstrokeWidth(3);


    }

    private Paint getPreviewPaint(){
        final Paint previewPaint = new Paint();
        previewPaint.setColor(0xFFC1C1C1);
        previewPaint.setStyle(Paint.Style.stroke);
        previewPaint.setstrokeJoin(Paint.Join.ROUND);
        previewPaint.setstrokeCap(Paint.Cap.ROUND);
        previewPaint.setstrokeWidth(3);
        return previewPaint;
    }




    public boolean onTouch(View view,MotionEvent motionEvent) {
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
            drawingSurface.isDrawing = true;

            currentDrawingPath = new DrawingPath();
            currentDrawingPath.paint = currentPaint;
            currentDrawingPath.path = new Path();
            currentBrush.mouseDown(currentDrawingPath.path,motionEvent.getX(),motionEvent.getY());
            currentBrush.mouseDown(drawingSurface.previewPath.path,motionEvent.getY());


        }else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
            drawingSurface.isDrawing = true;
            currentBrush.mouseMove( currentDrawingPath.path,motionEvent.getY() );
            currentBrush.mouseMove(drawingSurface.previewPath.path,motionEvent.getY());


        }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){


            currentBrush.mouseUp(drawingSurface.previewPath.path,motionEvent.getY());
            drawingSurface.previewPath.path = new Path();
            drawingSurface.addDrawingPath(currentDrawingPath);

            currentBrush.mouseUp( currentDrawingPath.path,motionEvent.getY() );

            undobtn.setEnabled(true);
            redobtn.setEnabled(false);

        }

        return true;
    }




    public void onClick(View view){
        switch (view.getId()){


        //CALENDAR
        case R.id.calendarBtn:
            calendarLayout.setVisibility(View.VISIBLE);
            break;

        case R.id.closeCalBtn:
            calendarLayout.setVisibility(View.INVISIBLE);
            break;

        case R.id.calculatorBtn:
             Dialog dialog = new Dialog(DrawingActivity.this);
             LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             View vi=inflater.inflate(R.layout.webviewdialog,null);
             dialog.setContentView(vi);
             dialog.setTitle(\"Calculator\");
             dialog.setCancelable(true);
             WebView wb = (WebView) vi.findViewById(R.id.webview);
             wb.loadUrl(\"http://mitchsamuels.com/calculator.html\");
             System.out.println(\"..loading url..\");
             dialog.show();

            break;






            //COLORS
            case R.id.colorRedBtn:
                SeekBar seekBarRed = (SeekBar)findViewById(R.id.seekBar);
                currentPaint = new Paint();
                currentPaint.setDither(true);
                currentPaint.setColor(0xCC990000);
                currentPaint.setStyle(Paint.Style.stroke);
                currentPaint.setstrokeJoin(Paint.Join.ROUND);
                currentPaint.setstrokeCap(Paint.Cap.ROUND);
                currentPaint.setstrokeWidth(seekBarRed.getProgress());
            break;
            case R.id.colorBlueBtn:
                SeekBar seekBarBlue = (SeekBar)findViewById(R.id.seekBar);
                currentPaint = new Paint();
                currentPaint.setDither(true);
                currentPaint.setColor(0xCC3333FF);
                currentPaint.setStyle(Paint.Style.stroke);
                currentPaint.setstrokeJoin(Paint.Join.ROUND);
                currentPaint.setstrokeCap(Paint.Cap.ROUND);
                currentPaint.setstrokeWidth(seekBarBlue.getProgress());
            break;
            case R.id.colorGreenBtn:
                SeekBar seekBarGreen = (SeekBar)findViewById(R.id.seekBar);
                currentPaint = new Paint();
                currentPaint.setDither(true);
                currentPaint.setColor(0xCC00CC00);
                currentPaint.setStyle(Paint.Style.stroke);
                currentPaint.setstrokeJoin(Paint.Join.ROUND);
                currentPaint.setstrokeCap(Paint.Cap.ROUND);
                currentPaint.setstrokeWidth(seekBarGreen.getProgress());
            break;
            case R.id.colorWhiteBtn:
                SeekBar seekBarWhite = (SeekBar)findViewById(R.id.seekBar);
                currentPaint = new Paint();
                currentPaint.setDither(true);
                currentPaint.setColor(0xCCFFFFFF);
                currentPaint.setStyle(Paint.Style.stroke);
                currentPaint.setstrokeJoin(Paint.Join.ROUND);
                currentPaint.setstrokeCap(Paint.Cap.ROUND);
                currentPaint.setstrokeWidth(seekBarWhite.getProgress());
            break;
            case R.id.eraserBtn:
                currentPaint = new Paint();
                currentPaint.setDither(true);
                currentPaint.setColor(0xFF000000);
                currentPaint.setStyle(Paint.Style.stroke);
                currentPaint.setstrokeJoin(Paint.Join.ROUND);
                currentPaint.setstrokeCap(Paint.Cap.ROUND);
               currentPaint.setstrokeWidth(70);
            break;



            //UNDO/REDO/SAVE

            case R.id.undobtn:
                drawingSurface.undo();
                if( drawingSurface.hasMoreUndo() == false ){
                    undobtn.setEnabled( false );
                }
                redobtn.setEnabled( true );
            break;

            case R.id.redobtn:
                drawingSurface.redo();
                if( drawingSurface.hasMoreRedo() == false ){
                    redobtn.setEnabled( false );
                }

                undobtn.setEnabled( true );
            break;
            case R.id.saveBtn:
                final Activity currentActivity  = this;
                Handler saveHandler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                        alertDialog.setTitle(\"Image Saved!\");
                        alertDialog.setMessage(\"Your note has been saved in your gallery.\");
                        alertDialog.setButton(\"OK\",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which) {
                                return;
                            }
                        });
                        alertDialog.show();
                    }
                } ;


               new ExportBitmapToFile(this,saveHandler,drawingSurface.getBitmap()).execute();
            break;

        }
    }





    private static final int REQUEST_MENU_ID = Menu.FirsT + 4;
    private static final int ABOUT_MENU_ID = Menu.FirsT + 5;
    private static final String CONTENT_TYPE_EMAIL = null;
    //private static final TextView TextView = null;



//CREATE MENU
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0,REQUEST_MENU_ID,\"Request Features\").setShortcut(\'5\',\'c\');
        menu.add(0,ABOUT_MENU_ID,\"About\").setShortcut(\'4\',\'s\');

        return true;
    }




    //MENU SELECTIONS
    @Override
    public boolean onoptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {



            case ABOUT_MENU_ID:
                Dialog dialog = new Dialog(DrawingActivity.this);
                dialog.setContentView(R.layout.maindialog);
                dialog.setTitle(\"About Jot Notes\");
                dialog.setCancelable(true);



                dialog.show();

                return true;
            case REQUEST_MENU_ID:
                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType(\"plain/text\");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{\"[email protected]\"});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,\"Jot Notes\");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,\"Dear Jot developer,\");
                //context.startActivity(Intent.createChooser(emailIntent,\"Send mail...\"));
                  //Intent sendIntent = new Intent(Intent.ACTION_SEND);
                  //sendIntent.putExtra(Intent.EXTRA_SUBJECT,\"Jot Notes\");
                  //sendIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {\"[email protected]\"});
                 // sendIntent.putExtra(Intent.EXTRA_TEXT,\"Dear Jot Developer,\");
                  //sendIntent.setType(CONTENT_TYPE_EMAIL);
                  startActivity(Intent.createChooser(emailIntent,\"Please Select an Application\"));

                  return true;

        }
        return super.onoptionsItemSelected(item);
    }









    private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> {
        private Context mContext;
        private Handler mHandler;
        private Bitmap nBitmap;

        public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap) {
            mContext = context;
            nBitmap = bitmap;
            mHandler = handler;
        }

        @Override
        protected Boolean doInBackground(Intent... arg0) {
            try {
                if (!APP_FILE_PATH.exists()) {
                    APP_FILE_PATH.mkdirs();
                }

                final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH + \"/myAwesomeDrawing.png\"));
                nBitmap.compress(Bitmap.CompressFormat.PNG,90,out);
                out.flush();
                out.close();
                return true;
            }catch (Exception e) {
                e.printstacktrace();
            }
            //mHandler.post(completeRunnable);
            return false;
        }


        @Override
        protected void onPostExecute(Boolean bool) {
            super.onPostExecute(bool);
            if ( bool ){
                mHandler.sendEmptyMessage(1);
            }
        }
    }
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)