我想隐藏标题栏,无论是背景还是前景。我刚刚试过这段代码但是有错误。有人说我的代码出了什么问题吗?
这是错误:
FATAL EXCEPTION: AsyncTask #1
Process: com.example.amadey.myapplication3, PID: 21769
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
at android.app.Activity.requestWindowFeature(Activity.java:3298)
at com.example.amadey.myapplication3.Activity$AsyncCaller.doInBackground(Activity.java:31)
at com.example.amadey.myapplication3.Activity$AsyncCaller.doInBackground(Activity.java:28)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
我的代码:
public class Activity extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
new AsyncCaller().execute();
}
private class AsyncCaller extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Activity.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
return null;
}
}
}
答案 0 :(得分:2)
调用setContentView后无法调用requestWindowFeature。它必须在之前调用。它根本不应该从AsyncTask中调用。
答案 1 :(得分:1)
除主 UI线程外,您无法在任何线程中执行UI操作。
Activity.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
此行必须位于onCreate()
类的Activity
方法中。
此外,您最好更改活动的名称。