我正在尝试在我的Windows视图中设置进度状态,但每次运行我的应用时都会导致错误
package example.asynctaskdemo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class asynctaskdemo extends ActionBarActivity {
private ListView mainList;
private String[] texts = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_asynctaskdemo);
mainList = (ListView)findViewById(R.id.my_list);
mainList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()));
new myAsync().execute();
}
class myAsync extends AsyncTask<Void, String, Void>{
private ArrayAdapter<String> adapter;
private int counter = 0;
@Override
protected void onPreExecute() {
adapter = (ArrayAdapter<String>)mainList.getAdapter();
setProgressBarIndeterminate(false);
setProgressBarVisibility(true);
}
@Override
protected Void doInBackground(Void... params) {
for(String item: texts){
publishProgress(item);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
adapter.add(values[0]);
counter++;
setProgress((int)(((double)counter/texts.length)*10000));
}
@Override
protected void onPostExecute(Void aVoid) {
setProgressBarVisibility(false);
Debug_It.s(asynctaskdemo.this, "Task has run successfully");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_asynctaskdemo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ERROR:
03-31 19:34:18.960 1058-1058/example.asynctaskdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: example.asynctaskdemo, PID: 1058
java.lang.RuntimeException: Unable to start activity ComponentInfo{example.asynctaskdemo/example.asynctaskdemo.asynctaskdemo}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
编译器还建议我使用supportRequestWindowFeature();
所以我需要帮助解决此错误,我也想知道supportRequestWindowFeature()
和requestWindowFeature();
答案 0 :(得分:0)
使用requestWindowFeature(Window.FEATURE_PROGRESS);
之前super.onCreate(savedInstanceState);
它会起作用。即使我也遇到了这个问题。
答案 1 :(得分:0)
正如我所看到的,它已经在setContentView
之前被调用,所以问题不在于此。
您可能正在使用支持库而不是requestWindowFeature()
。您需要使用supportRequestWindowFeature()
。这将消除崩溃,但进度条仍然无法显示。