内容视图尚未建立 [英] Content view not yet created
本文介绍了内容视图尚未建立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从MySQL检索数据,并根据类别选项卡上的排序。当我打开活动与内容来看我的应用程序崩溃尚未建立。我想知道这是什么问题?
四月3日至10号:12:49.043:E / AndroidRuntime(2251):致命异常:主要
4月三号至10日:12:49.043:E / AndroidRuntime(2251):java.lang.RuntimeException的:无法启动的活动ComponentInfo {my.fyp.inticlassifieds / my.fyp.inticlassifieds.DisplayPostItem}:java.lang.IllegalStateException:内容查看尚未建立
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.os.Handler.dispatchMessage(Handler.java:99)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.os.Looper.loop(Looper.java:137)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.ActivityThread.main(ActivityThread.java:5103)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在java.lang.reflect.Method.invokeNative(本机方法)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在java.lang.reflect.Method.invoke(Method.java:525)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在dalvik.system.NativeStart.main(本机方法)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):致:java.lang.IllegalStateException:内容视图尚未创建
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在my.fyp.inticlassifieds.BooksFragment.onCreateView(BooksFragment.java:87)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.Activity.performStart(Activity.java:5143)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
4月三号至10日:12:49.043:E / AndroidRuntime(2251):11 ...更多
类:
公共类BooksFragment扩展ListFragment {
//进度对话框
私人ProgressDialog pDialog;
//创建JSON解析器对象
JSONParser jParser =新JSONParser();
的JSONObject JSON;
ArrayList的<产品> productsList =新的ArrayList<产品>();
// URL获得所有产品列表
私有静态字符串url_all_products =http://10.0.2.2:8000/project/display_items.php;
// JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_PRODUCTS =产品;
私有静态最后弦乐TAG_PID =PID;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_BUDGET =价格;
私有静态最后弦乐TAG_DES =说明;
私有静态最后弦乐TAG_DATE_POSTED =created_at;
私有静态最后弦乐TAG_CATEGORY =类;
私有静态最后弦乐TAG_EMAIL =电子邮件;
私有静态最后弦乐TAG_CONTACT =接触;
//产品JSONArray
JSONArray产品= NULL;
ListView的LV;
ListAdapter listadapter;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.list_fragment,集装箱,
假);
productsList =新的ArrayList<产品>();
LV = getListView();
//在选课单品
//启动编辑产品屏幕
lv.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
//从选择列表项获得价值
字符串的pid =((TextView中)view.findViewById(R.id.pid))。gettext的()
的ToString();
字符串名称=((TextView中)view.findViewById(R.id.name))
.getText()的toString()。
字符串描述=((TextView中)view.findViewById(R.id.des))
.getText()的toString()。
串价格=((TextView中)view.findViewById(R.id.budget))
.getText()的toString()。
字符串日期=((TextView中)view.findViewById(R.id.date_posted))
.getText()的toString()。
字符串类=((TextView中)view.findViewById(R.id.category))
.getText()的toString()。
字符串email =((TextView中)查看
.findViewById(R.id.email_request))的getText()的toString()。
弦接触=((TextView中)查看
.findViewById(R.id.contact_request))。gettext的()
的ToString();
//开始的新意图
意图=新的意图(getActivity(),DisplayItemInfo.class);
//将信息发送到下一个活动
in.putExtra(TAG_PID,PID);
in.putExtra(TAG_NAME,姓名);
in.putExtra(TAG_DES,说明);
in.putExtra(TAG_BUDGET,价格);
in.putExtra(TAG_DATE_POSTED,日期);
in.putExtra(TAG_CATEGORY,类);
in.putExtra(TAG_EMAIL,电子邮件);
in.putExtra(TAG_CONTACT,接触);
startActivity(在);
}
});
新LoadAllProducts()执行();
返回rootView;
}
类LoadAllProducts扩展的AsyncTask<字符串,字符串,字符串> {
/ **
*在启动后台线程显示进度对话框
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(getActivity());
pDialog.setMessage(载入产品请稍候...);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/ **
*得到所有产品的网址
* * /
保护字符串doInBackground(字符串参数... args){
//大厦参数
名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
意向意图= getIntent();
//越来越重视意图数据
字符串jsonS = intent.getStringExtra(JSON);
如果(jsonS == NULL){
//从URL获取JSON字符串
JSON = jParser.makeHtt prequest(url_all_products,GET,则params);
} 其他 {
尝试 {
JSONObject的Jsonobj =新的JSONObject(jsonS);
JSON = Jsonobj;
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
//检查你的日志猫JSON效应初探
Log.d(所有产品,json.toString());
尝试 {
//检查成功TAG
INT成功= json.getInt(TAG_SUCCESS);
如果(成功== 1){
发现//产品
//获取产品阵列
产品= json.getJSONArray(TAG_PRODUCTS);
//遍历所有产品
的for(int i = 0; I< products.length();我++){
JSONObject的C = products.getJSONObject(我);
//保存在变量中的每个JSON项目
字符串ID = c.getString(TAG_PID);
字符串名称= c.getString(TAG_NAME);
预算有限= c.getString(TAG_BUDGET);
字符串描述= c.getString(TAG_DES);
字符串类= c.getString(TAG_CATEGORY);
弦接触= c.getString(TAG_CONTACT);
字符串email = c.getString(TAG_EMAIL);
字符串日期= c.getString(TAG_DATE_POSTED);
如果(类别==书){
//创建一个新的产品Obect,设置它的价值
产品mProduct =新产品();
mProduct.pid = ID;
mProduct.name =名称;
mProduct.price =预算;
mProduct.description =描述;
mProduct.date =日期;
mProduct.category =类别;
mProduct.email =电子邮件;
mProduct.contact =接触;
//增加产品到ArrayList中
productsList.add(mProduct);
}
}
} 其他 {
//没有发现产品
//启动添加新产品活动
意图I =新的意图(getActivity(),MainMenu.class);
//关闭所有previous活动
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
}
}赶上(JSONException E){
e.printStackTrace();
}
返回null;
}
私人意图getIntent(){
// TODO自动生成方法存根
返回null;
}
/ **
*在完成后台任务之后关闭该进度对话框
* ** /
保护无效onPostExecute(字符串file_url){
让所有的产品之后//关闭该对话框
pDialog.dismiss();
如果(productsList.size()大于0){
/ **
*更新解析JSON数据到ListView控件
* * /
CustomAdapter适配器=新CustomAdapter(getActivity()
productsList); //,
如果(getListView()!= NULL){
//更新列表视图
setListAdapter(适配器);
} 其他 {
Log.d(ListView控件索引,ListView控件为空);
}
} 其他 {
Log.d(产品目录,产品列表为空);
}
}
}
}
解决方案
onCreateView
只能返回重新present片段的视图。在查看其他业务应在另一个回调来执行, onViewCreated
:
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
返回inflater.inflate(R.layout.list_fragment,集装箱,假);
}
@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
productsList =新的ArrayList<产品>();
LV = getListView();
//在选课单品
//启动编辑产品屏幕
lv.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
//从选择列表项获得价值
字符串的pid =((TextView中)view.findViewById(R.id.pid))。gettext的()
的ToString();
字符串名称=((TextView中)view.findViewById(R.id.name))
.getText()的toString()。
字符串描述=((TextView中)view.findViewById(R.id.des))
.getText()的toString()。
串价格=((TextView中)view.findViewById(R.id.budget))
.getText()的toString()。
字符串日期=((TextView中)view.findViewById(R.id.date_posted))
.getText()的toString()。
字符串类=((TextView中)view.findViewById(R.id.category))
.getText()的toString()。
字符串email =((TextView中)查看
.findViewById(R.id.email_request))的getText()的toString()。
弦接触=((TextView中)查看
.findViewById(R.id.contact_request))。gettext的()
的ToString();
//开始的新意图
意图=新的意图(getActivity(),DisplayItemInfo.class);
//将信息发送到下一个活动
in.putExtra(TAG_PID,PID);
in.putExtra(TAG_NAME,姓名);
in.putExtra(TAG_DES,说明);
in.putExtra(TAG_BUDGET,价格);
in.putExtra(TAG_DATE_POSTED,日期);
in.putExtra(TAG_CATEGORY,类);
in.putExtra(TAG_EMAIL,电子邮件);
in.putExtra(TAG_CONTACT,接触);
startActivity(在);
}
});
新LoadAllProducts()执行();
}
I am trying to retrieve the data from mysql and sort it based on the category tab. My application crash when i open the activity with content view not yet created. May I know what is the problem?
03-10 04:12:49.043: E/AndroidRuntime(2251): FATAL EXCEPTION: main
03-10 04:12:49.043: E/AndroidRuntime(2251): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.fyp.inticlassifieds/my.fyp.inticlassifieds.DisplayPostItem}: java.lang.IllegalStateException: Content view not yet created
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.os.Looper.loop(Looper.java:137)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-10 04:12:49.043: E/AndroidRuntime(2251): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 04:12:49.043: E/AndroidRuntime(2251): at java.lang.reflect.Method.invoke(Method.java:525)
03-10 04:12:49.043: E/AndroidRuntime(2251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-10 04:12:49.043: E/AndroidRuntime(2251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-10 04:12:49.043: E/AndroidRuntime(2251): at dalvik.system.NativeStart.main(Native Method)
03-10 04:12:49.043: E/AndroidRuntime(2251): Caused by: java.lang.IllegalStateException: Content view not yet created
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
03-10 04:12:49.043: E/AndroidRuntime(2251): at my.fyp.inticlassifieds.BooksFragment.onCreateView(BooksFragment.java:87)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.Activity.performStart(Activity.java:5143)
03-10 04:12:49.043: E/AndroidRuntime(2251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
03-10 04:12:49.043: E/AndroidRuntime(2251): ... 11 more
Class:
public class BooksFragment extends ListFragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
JSONObject json;
ArrayList<Products> productsList = new ArrayList<Products>();
// url to get all products list
private static String url_all_products = "http://10.0.2.2:8000/project/display_items.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "product";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_BUDGET = "price";
private static final String TAG_DES = "description";
private static final String TAG_DATE_POSTED = "created_at";
private static final String TAG_CATEGORY = "category";
private static final String TAG_EMAIL = "email";
private static final String TAG_CONTACT = "contact";
// products JSONArray
JSONArray products = null;
ListView lv;
ListAdapter listadapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_fragment, container,
false);
productsList = new ArrayList<Products>();
lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String description = ((TextView) view.findViewById(R.id.des))
.getText().toString();
String price = ((TextView) view.findViewById(R.id.budget))
.getText().toString();
String date = ((TextView) view.findViewById(R.id.date_posted))
.getText().toString();
String category = ((TextView) view.findViewById(R.id.category))
.getText().toString();
String email = ((TextView) view
.findViewById(R.id.email_request)).getText().toString();
String contact = ((TextView) view
.findViewById(R.id.contact_request)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getActivity(), DisplayItemInfo.class);
// sending information to next activity
in.putExtra(TAG_PID, pid);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DES, description);
in.putExtra(TAG_BUDGET, price);
in.putExtra(TAG_DATE_POSTED, date);
in.putExtra(TAG_CATEGORY, category);
in.putExtra(TAG_EMAIL, email);
in.putExtra(TAG_CONTACT, contact);
startActivity(in);
}
});
new LoadAllProducts().execute();
return rootView;
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
Intent intent = getIntent();
// getting attached intent data
String jsonS = intent.getStringExtra("JSon");
if (jsonS == null) {
// getting JSON string from URL
json = jParser.makeHttpRequest(url_all_products, "GET", params);
} else {
try {
JSONObject Jsonobj = new JSONObject(jsonS);
json = Jsonobj;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String budget = c.getString(TAG_BUDGET);
String description = c.getString(TAG_DES);
String category = c.getString(TAG_CATEGORY);
String contact = c.getString(TAG_CONTACT);
String email = c.getString(TAG_EMAIL);
String date = c.getString(TAG_DATE_POSTED);
if (category == "Books") {
// Create a new Product Obect, set its Values
Products mProduct = new Products();
mProduct.pid = id;
mProduct.name = name;
mProduct.price = budget;
mProduct.description = description;
mProduct.date = date;
mProduct.category = category;
mProduct.email = email;
mProduct.contact = contact;
// adding Product to ArrayList
productsList.add(mProduct);
}
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity(), MainMenu.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
if (productsList.size() > 0) {
/**
* Updating parsed JSON data into ListView
* */
CustomAdapter adapter = new CustomAdapter(getActivity(),
productsList);// ,
if (getListView() != null) {
// updating listview
setListAdapter(adapter);
} else {
Log.d("ListView-Reference", "ListView is null");
}
} else {
Log.d("Product List", "Products list is empty");
}
}
}
}
解决方案
onCreateView
must only return the view that represent the fragment. Other operations on the View should be performed in another callback, onViewCreated
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
productsList = new ArrayList<Products>();
lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String description = ((TextView) view.findViewById(R.id.des))
.getText().toString();
String price = ((TextView) view.findViewById(R.id.budget))
.getText().toString();
String date = ((TextView) view.findViewById(R.id.date_posted))
.getText().toString();
String category = ((TextView) view.findViewById(R.id.category))
.getText().toString();
String email = ((TextView) view
.findViewById(R.id.email_request)).getText().toString();
String contact = ((TextView) view
.findViewById(R.id.contact_request)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getActivity(), DisplayItemInfo.class);
// sending information to next activity
in.putExtra(TAG_PID, pid);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DES, description);
in.putExtra(TAG_BUDGET, price);
in.putExtra(TAG_DATE_POSTED, date);
in.putExtra(TAG_CATEGORY, category);
in.putExtra(TAG_EMAIL, email);
in.putExtra(TAG_CONTACT, contact);
startActivity(in);
}
});
new LoadAllProducts().execute();
}
这篇关于内容视图尚未建立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文