我的应用程序只能运行一次,第二次运行时(以及其他运行...),我得到了“不幸的是,myapp已停止”。我已经尝试了很多,但是对我没有任何影响。这是代码:

主要活动:
    包com.example.myfirstapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;



public class MainActivity extends Activity {


    public void onCreate(Bundle savedInstanceState) {
        final Context context = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(context, WebViewActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case R.id.exit:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}


WebViewActivity:

package com.example.myfirstapp;





import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity {

    private WebView webView;

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

        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);

        webView.loadUrl("http://mysite.com");

    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
}


Logcat:

09-05 05:53:27.080: I/Choreographer(815): Skipped 76 frames!  The application may be doing too much work on its main thread.
09-05 05:53:27.391: D/dalvikvm(815): GC_FOR_ALLOC freed 88K, 6% free 2654K/2808K, paused 136ms, total 139ms
09-05 05:53:27.760: I/Choreographer(815): Skipped 88 frames!  The application may be doing too much work on its main thread.
09-05 05:53:27.771: D/gralloc_goldfish(815): Emulator without GPU emulation detected.
09-05 05:53:27.931: I/Choreographer(815): Skipped 38 frames!  The application may be doing too much work on its main thread.
09-05 05:53:28.630: E/chromium_net(815): external/chromium/net/disk_cache/backend_impl.cc:1107: [0905/055328:ERROR:backend_impl.cc(1107)] Critical error found -8
09-05 05:53:28.641: W/chromium_net(815): external/chromium/net/disk_cache/storage_block-inl.h:119: [0905/055328:WARNING:storage_block-inl.h(119)] Failed data load.
09-05 05:53:28.651: D/chromium(815): Unknown chromium error: -401
09-05 05:53:28.670: W/chromium_net(815): external/chromium/net/disk_cache/storage_block-inl.h:119: [0905/055328:WARNING:storage_block-inl.h(119)] Failed data load.
09-05 05:53:29.210: E/cutils-trace(815): Error opening trace file: No such file or directory (2)
09-05 05:53:29.271: D/TilesManager(815): Starting TG #0, 0x2a2a6710
09-05 05:53:58.921: W/IInputConnectionWrapper(815): showStatusIcon on inactive InputConnection
09-05 05:54:00.280: I/Choreographer(815): Skipped 44 frames!  The application may be doing too much work on its main thread.

最佳答案

我在您的logcat中看到两个问题。首先,结合访问仿真器中的磁盘缓存,您会遇到一个奇怪的严重错误-8。其次,您的主线程执行了太多工作。

对于磁盘问题:有很多可能的原因。您的主机磁盘可能已满,损坏等。检查您的PC并使用新磁盘重新创建虚拟设备。如果该问题导致磁盘上的IO变慢,则第二个问题可能会立即消失。

用于主线程工作。代码中唯一不执行任何操作的是loadUrl()。如果您正在加载大型网页和/或在页面上使用插槽JavaScript,则可能会花费很多精力,并导致WebView冻结。在另一个线程中进行加载。

09-27 21:34