Reputation: 91
I keep having an issue with my recipe book app where it says I am doing too much work in the main thread. I have attached my code. Can someone tell me why this is the issue?? I have never gotten it before and other posts don't help. If someone has an answer, can you please explain in detail?? I am new to this. I need you to explain it to me as if I was 5, as condescending as possible...
package com.example.recipebook;
import com.example.recipebook.util.SystemUiHider;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
public class MainPage extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
Button searchButton = (Button) findViewById(R.id.triggerSearch);
Button submitButton = (Button) findViewById(R.id.triggerSubmit);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
openSearchPage();
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
openSubmitPage();
}
});
}
public void openSearchPage(){
Intent openSearchPage = new Intent(this,SearchPage.class);
startActivity(openSearchPage);
}
public void openSubmitPage(){
Intent openSubmitPage = new Intent(this,SumbitRecipePage.class);
startActivity(openSubmitPage);
}
}
package com.example.recipebook;
import android.app.Activity;
import android.os.Bundle;
public class SumbitRecipePage extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.submitrecipepage);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffd100">
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/triggerSearch"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/triggerSubmit"
android:layout_alignEnd="@+id/triggerSubmit" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/triggerSubmit"
android:layout_below="@+id/triggerSearch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:id="@+id/button3"
android:layout_below="@+id/triggerSubmit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Favourites"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button5"
android:layout_alignEnd="@+id/button5" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shopping List"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button6"
android:layout_alignEnd="@+id/button6" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pantry"
android:id="@+id/button6"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button7"
android:layout_alignEnd="@+id/button7" />
<Button
android:layout_weight="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random Recipe"
android:id="@+id/button7"
android:layout_below="@+id/button6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recipebook" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.recipebook.MainPage"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is the error output whenever I click on one of the buttons to bring me to the other pages. What is going on?? It just says "Unfortunately, RecipeBook has stopped." and quits.
03-09 14:37:00.329 1145-1145/com.example.recipebook I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
03-09 14:37:02.909 372-387/system_process I/Choreographer﹕ Skipped 68 frames! The application may be doing too much work on its main thread.
03-09 14:37:03.299 372-386/system_process I/Choreographer﹕ Skipped 31 frames! The application may be doing too much work on its main thread.
03-09 14:37:03.509 372-386/system_process I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
03-09 14:37:03.719 372-386/system_process I/Choreographer﹕ Skipped 31 frames! The application may be doing too much work on its main thread.
03-09 14:37:03.789 372-387/system_process I/Choreographer﹕ Skipped 116 frames! The application may be doing too much work on its main thread.
03-09 14:37:03.999 372-386/system_process I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
03-09 14:37:04.209 372-386/system_process I/Choreographer﹕ Skipped 41 frames! The application may be doing too much work on its main thread.
03-09 14:37:04.429 372-387/system_process I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
03-09 14:37:04.909 372-387/system_process I/Choreographer﹕ Skipped 63 frames! The application may be doing too much work on its main thread.
03-09 14:37:04.929 528-528/com.android.launcher I/Choreographer﹕ Skipped 297 frames! The application may be doing too much work on its main thread.
03-09 14:37:06.199 372-386/system_process I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
03-09 14:37:06.909 372-386/system_process I/Choreographer﹕ Skipped 33 frames! The application may be doing too much work on its main thread.
03-09 14:37:07.299 372-386/system_process I/Choreographer﹕ Skipped 98 frames! The application may be doing too much work on its main thread.
03-09 14:37:07.529 372-386/system_process I/Choreographer﹕ Skipped 37 frames! The application may be doing too much work on its main thread.
03-09 14:37:07.849 372-386/system_process I/Choreographer﹕ Skipped 61 frames! The application may be doing too much work on its main thread.
03-09 14:37:08.039 372-386/system_process I/Choreographer﹕ Skipped 47 frames! The application may be doing too much work on its main thread.
03-09 14:37:09.789 372-387/system_process I/Choreographer﹕ Skipped 77 frames! The application may be doing too much work on its main thread.
Upvotes: 0
Views: 6141
Reputation: 826
This is because Choreographer of android is not getting required resource for performing proper display hence skipping frames.
this mainly happens when user perform heavy operations in main thread, remember Android is single threaded system, there is Single main thread where all the other subsystem hooks upon to perform task such as Activities,Services,Content Providers. thats why when you perform network operation directly on main thered you get Application not responding message.
To elaborate upon your situation, do the following.
If you have any network operation being done in main thread, move them to a separate thread, in Android native we have AsyncTask for that, try finding thread mechanism in Phonegap
If you have any heavy operations such as Compression of Bitmap etc, handle them efficiently by threads or post runnable logic, this will make sure your main thread is free for trivial task such as generation of display processing of events etc.
In the sense, any logic which might take some time, move it to threads, this will make sure your display run smooth.
Hope it help.
Upvotes: 2