【问题标题】:Which Mistake in Webview based Android Browser Application?基于 Webview 的 Android 浏览器应用程序中的哪些错误?
【发布时间】:2014-11-13 05:05:53
【问题描述】:

我正在制作一个 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();
  }
}

一个.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);
  }
}

【问题讨论】:

  • 能否提供错误信息?位于日志中?
  • 请注明发生崩溃的错误行。
  • 11-13 11:03:17.094: E/AndroidRuntime(740): at java.util.Timer$TimerImpl.run(Timer.java:284)

标签: java javascript android browser webview


【解决方案1】:

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

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

数字“2131230720”是 static 对资源的引用 int,在本例中是您的 Activity 中的布局视图。该数字来自 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 文件。

【讨论】:

  • 我真的是新手。我找到了 .apk 文件并将其解压缩。使用 apk 工具。但是这个源代码有很多错误。并且在源代码中找不到 R.java。所以只有 Mainactivity.java 中的 ids 如何解决这个问题。
  • 知道了。我编辑了我的答案。它应该解释如何更好地修复它。
  • ok 的意思。例如 res/leyout/activity_main.xml 代码是
  • 很多很多错误。我想我不能这样做。这是我在学校实践中重要的工作。请帮帮我,吉姆。我提供所有源代码。请你完成了这项工作。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2015-03-17
相关资源
最近更新 更多