gpt4 book ai didi

android - 方向改变使应用程序崩溃(如果在加载 SherlockFragment 时完成)

转载 作者:行者123 更新时间:2023-11-29 02:00:29 26 4
gpt4 key购买 nike

我的主 Activity 中有两个选项卡(我使用 ActionBarSherlock)。我的 fragment 加载并解析远程 xml 以填充 ListView 。

如果我在 fragment 完全加载时更改设备方向,它会再次加载新方向而不会出现问题。但是,如果我在加载 fragment 时更改它,应用程序会在加载完成时强制关闭。

我做错了什么吗?

public class PartOne extends SherlockListFragment{

// All static variables
static final String URL = "http://Y.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_CAT = "category";
static final String KEY_DESC = "description";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new BackgroundAsyncTask().execute();
}

public class BackgroundAsyncTask extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {

@Override
protected void onPreExecute() {
}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML

if (xml != null) {
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
if(i%2 != 1) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);

// data to string (to modify them)
String titre = parser.getValue(e, KEY_TITLE);
String categorie = parser.getValue(e, KEY_CAT);
String description = parser.getValue(e, KEY_DESC);

// adding each child node to HashMap key => value
map.put(KEY_TITLE, titre);
map.put(KEY_CAT, categorie);
map.put(KEY_DESC, description);

// adding HashList to ArrayList
menuItems.add(map);

}
}
}
// selecting single ListView item
ListView lv = getListView();

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.title)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.categ)).getText().toString();
String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(view.getContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, name);
in.putExtra(KEY_CAT, cost);
in.putExtra(KEY_DESC, description);
startActivity(in);

}
});
return menuItems;
}

protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

ListAdapter adapter = new SimpleAdapter(getActivity(),
result, R.layout.list_item,
new String[] {KEY_TITLE, KEY_DESC, KEY_CAT},
new int[] {R.id.title, R.id.desciption, R.id.categ});
setListAdapter(adapter);
}

}

日志:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)

最佳答案

onConfigurationChanged (Configuration newConfig) 方法中处理你的任务,像这样

    Thread th=null;
public void onCreate(bundle savedInstanceState)
{
//Initialize layout

if(th==null) //This is to check whether task is running
{
//Assign your task
th=new Thread();
th.start();
}
}

//handle the same here
public void onConfigurationChanged (Configuration newConfig)
{

//Do the layout changes
if(th==null)//This is to check whether task is running
{
//Assign your task
th=new Thread();
th.start();
}

}

关于android - 方向改变使应用程序崩溃(如果在加载 SherlockFragment 时完成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com