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

Which Mistake in Webview based Android Browser Application?

本文关键字:错误 浏览器 Webview Android 基于 应用程序      更新时间:2023-09-26

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

主要活动.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);
  }
}

您的问题几乎可以肯定是问题:

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中的XML文件中找到XML定义对象的"id"—使用该@+id引用。这将跟踪对该静态引用的更改。

然后,当你build或重建你的Android应用程序时,它会创建带有所有适当引用的R.java文件。