Reputation: 55
My application works only once, on the second run (and others...) I've got "Unfortunately, myapp has stopped". I've tried a lot, but there is no effects for me. Here's the code:
MainActivity: package 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.
Upvotes: 1
Views: 6849
Reputation: 593
Add simple one line code in your AndroidManifest.xml file:
< application
android:allowBackup="true"
android:largeHeap="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Apptheme"
>
Upvotes: 0
Reputation: 11912
I see two issues in your logcat. First you get a strange, critical error -8 in combination with accessing the disk cache in the emulator. Second, your main thread does too much work.
For the disk problem: There're many possible reasons. Your Host disk might be full, broken or such. Check your Pc and recreate the virtual device with a new disk. If the issue causes slow IO on the disk, your second problem might go away immediately.
For the main thread work. The only thing in your code that does anything is the loadUrl()
. If you're loading a large web page and/or use slot JavaScript on the page it can be a pot work and cause the WebView to freeze. Do the loading in another thread.
Upvotes: 0
Reputation: 444
@Slawek Leh - hey when u get this kind of error that your are performing much work on main thread.
Two solution are :
1) use Asynctask for doing operations
2) or Add in your Code Above Acitivity:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Upvotes: 0
Reputation: 635
<?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>
this should be your manifest file.Check this
Upvotes: 0
Reputation: 11188
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);
}
Upvotes: 0
Reputation: 735
Why final Context context?? You can just start activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
startActivity(intent);
}
Upvotes: 0
Reputation: 157457
You should call the super before you perform any other operation. Still I do not understand the need to assign this to a final Context object
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);
}
Upvotes: 1