基于Webview的Android浏览器应用程序存在哪些错误?

时间:2014-11-13 05:05:54

标签: java javascript android browser webview

我正在制作Android浏览器应用程序。它的启动图像在加载后打开,但之后它会崩溃。我还没发现我在MainActivity.java代码中有什么错误。 当我运行代码时,我的默认AVD告诉我我的应用程序崩溃了。

MainActivity.java



package com.example.package;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity
  extends Activity
{
  private WebView a;
  @SuppressLint({"InlinedApi"})
  private BroadcastReceiver b = new a(this);
  
  public void a()
  {
    AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
    localBuilder.setTitle("Google");
    localBuilder.setMessage("Are you sure you want to Exit?");
    localBuilder.setIcon(2130837505);
    localBuilder.setPositiveButton("YES", new d());
    localBuilder.setNegativeButton("NO", new e());
    localBuilder.show();
  }
  
  public void onBackPressed()
  {
    a();
  }
  
  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(2);
    setContentView(2130903042);
    this.a = ((WebView)findViewById(2131230720));
    this.a.getSettings().setJavaScriptEnabled(true);
    this.a.setFocusableInTouchMode(true);
    this.a.getSettings().setLoadWithOverviewMode(true);
    this.a.getSettings().setUseWideViewPort(true);
    this.a.getSettings().setLoadsImagesAutomatically(true);
    this.a.loadUrl("http://www.google.com");
    this.a.setWebChromeClient(new b(this));
    this.a.setDownloadListener(new c());
    this.a.setWebViewClient(new f());
  }
  
  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    super.onCreateOptionsMenu(paramMenu);
    paramMenu.add(0, 1, 0, "Home").setIcon(2130837506);
    paramMenu.add(0, 2, 0, "Downloads").setIcon(2130837504);
    paramMenu.add(0, 3, 0, "Back").setIcon(2130837506);
    paramMenu.add(0, 4, 0, "Forward").setIcon(2130837505);
    paramMenu.add(0, 5, 0, "Refresh").setIcon(2130837509);
    return true;
  }
  
  public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    if ((paramInt == 4) && (this.a.canGoBack()))
    {
      this.a.goBack();
      return true;
    }
    return super.onKeyDown(paramInt, paramKeyEvent);
  }
  
  public boolean onOptionsItemSelected(MenuItem paramMenuItem)
  {
    super.onOptionsItemSelected(paramMenuItem);
    switch (paramMenuItem.getItemId())
    {
    default: 
      return false;
    case 1: 
      this.a.loadUrl("http://www.google.com");
      return true;
    case 3: 
      this.a.goBack();
      return true;
    case 5: 
      this.a.reload();
      return true;
    case 4: 
      this.a.goForward();
      return true;
    }
  }
  
  protected void onPause()
  {
    super.onPause();
    unregisterReceiver(this.b);
  }
  
  @SuppressLint({"InlinedApi"})
  protected void onResume()
  {
    IntentFilter localIntentFilter = new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE");
    registerReceiver(this.b, localIntentFilter);
    super.onResume();
  }
}




a.java



package com.example.package;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.widget.Toast;

class a
  extends BroadcastReceiver
{
  a(MainActivity paramMainActivity) {}
  
  public void onReceive(Context paramContext, Intent paramIntent)
  {
    Toast.makeText(paramContext, paramContext.getResources().getString(2131034114), 0).show();
  }

public static void finish() {
	// TODO Auto-generated method stub
	
}

public static void startActivity(Intent localIntent) {
	// TODO Auto-generated method stub
	
}
}




g.java



package com.example.package;

import android.app.Activity;
import android.content.Intent;
import java.util.TimerTask;

class g
  extends TimerTask
{
  private Object a;

g(splash paramsplash) {}
  
  public void run()
  {
    ((Activity) this.a).finish();
    Intent localIntent = new Intent();
    ((Activity) this.a).startActivity(localIntent);
  }
}




1 个答案:

答案 0 :(得分:0)

您的问题几乎肯定是问题所在:

this.a = ((WebView)findViewById(2131230720));

数字“2131230720”是资源的static引用int,在这种情况下是您的活动中的布局视图。这个数字来自R.java,它是在你“构建”你的项目时创建的。每次执行构建时,它都会为您在res文件夹中以XML定义的布局对象,字符串等分配一个整数。

使用整数值可能会有一段时间(或存在于源代码中),但随后您可以更改布局(或其他R对象,如字符串)或在其他计算机,Android库等上重建它不再起作用了;

该行应如下所示:

this.a = ((WebView)findViewById(R.id.my_webview_layout_id));

其中“my_webview_layout_id”来自您的布局XML文件。您应该在res/layout中使用@+id引用的XML文件中找到XML定义对象的“id”。这将跟踪对该静态引用的更改。

然后当您build或重建Android应用时,它会创建包含所有相应引用的R.java文件。