我有一个应用程序,在对话框中弹出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.getX(), motionEvent.getY());
}else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
drawingSurface.isDrawing = true;
currentBrush.mouseMove( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );
currentBrush.mouseMove(drawingSurface.previewPath.path, motionEvent.getX(), motionEvent.getY());
}else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
currentBrush.mouseUp(drawingSurface.previewPath.path, motionEvent.getX(), motionEvent.getY());
drawingSurface.previewPath.path = new Path();
drawingSurface.addDrawingPath(currentDrawingPath);
currentBrush.mouseUp( currentDrawingPath.path, motionEvent.getX(), 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, 0, "Request Features").setShortcut('5', 'c');
menu.add(0, ABOUT_MENU_ID, 0, "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[]{"samuels.mitch@gmail.com"});
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[] {"Samuels.mitch@gmail.com"});
// 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);
}
}
}
}