1

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

MainActivity:包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);
        }
    }
}

网络视图活动:

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.
    }
}

日志猫:

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.
4

7 回答 7

1

在执行任何其他操作之前,您应该调用 super。我仍然不明白需要将它分配给最终的 Context 对象

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = this;
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, WebViewActivity.class);
        startActivity(intent);
    }
于 2013-09-05T09:39:47.697 回答
0
<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.myfirstapp"
   android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.example.myfirstapp.WebViewActivity" />
 </application>

</manifest>

这应该是你的清单文件。检查这个

于 2013-09-05T10:02:13.077 回答
0

我在您的 logcat 中看到两个问题。首先,在访问模拟器中的磁盘缓存时,您会遇到一个奇怪的严重错误 -8。其次,你的主线程做了太多的工作。

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

用于主线程工作。您的代码中唯一能做任何事情的是loadUrl(). 如果您正在加载一个大型网页和/或在页面上使用插槽 JavaScript,则它可能是一项盆栽工作并导致 WebView 冻结。在另一个线程中加载。

于 2013-09-05T10:33:16.397 回答
0

为什么是最终的上下文上下文?你可以开始活动

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
        startActivity(intent);
    }
于 2013-09-05T09:46:12.523 回答
0
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Context context = this;
        Intent intent = new Intent(context, WebViewActivity.class);
        startActivity(intent);
    }
于 2013-09-05T09:47:30.967 回答
0

@Slawek Leh - 嘿,当您遇到这种错误时,您正在主线程上执行大量工作。

两种解决方案是:1)使用 Asynctask 进行操作 2)或在代码中添加 Acitivity: StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

于 2013-09-05T10:07:19.927 回答
0

在您的 AndroidManifest.xml 文件中添加简单的一行代码:

< application
    android:allowBackup="true"

    android:largeHeap="true"

    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Apptheme" 
>
于 2014-02-03T10:01:43.327 回答