我的食谱书应用程序一直存在问题,它说我在主线程中做了太多工作。我附上了我的代码。谁能告诉我为什么这是问题?我以前从来没有得过它,其他帖子也无济于事。如果有人有答案,你能详细解释一下吗?我是新来的。我需要你向我解释,好像我是5岁,尽可能地居高临下......
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>
每当我点击其中一个按钮将我带到其他页面时,这就是错误输出。到底是怎么回事??它只是说“不幸的是,RecipeBook已经停止了。”然后退出。
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.
答案 0 :(得分:2)
这是因为android的编排者没有获得执行正确显示因此跳过帧所需的资源。
这主要发生在用户在主线程中执行大量操作时,记住Android是单线程系统,有单个主线程,其他所有子系统都挂钩执行任务,如活动,服务,内容提供者。这就是为什么当你直接在main thered上执行网络操作时,你得到的应用程序没有响应消息。
要详细说明您的情况,请执行以下操作。
如果您在主线程中进行了任何网络操作,请将它们移动到一个单独的线程,在Android本机中我们有AsyncTask,尝试在Phonegap中查找线程机制
如果您有任何繁重的操作,如压缩位图等,通过线程或发布可运行的逻辑有效地处理它们,这将确保您的主线程可以执行琐碎的任务,例如生成事件的显示处理等。
从某种意义上说,任何可能需要一些时间的逻辑,将其移动到线程,这将确保您的显示顺利运行。
希望有所帮助。