Content view not yet created

前端 未结 3 1416
小蘑菇
小蘑菇 2021-02-07 14:51

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 wh

相关标签:
3条回答
  • 2021-02-07 14:58

    You can't call getListView() in the onCreateView() because the ListView doesn't exist yet. You need to put that code inside onViewCreated().

    0 讨论(0)
  • 2021-02-07 15:05

    You can avoid crash by using check

    if(this.isAdded())   
    

    if you debug that part BooksFragment.getListView int the moment of exception - you will notice it's not exist.

    0 讨论(0)
  • 2021-02-07 15:19

    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();
     }
    
    0 讨论(0)
提交回复
热议问题