在Expandable ListView

时间:2016-09-06 13:20:41

标签: android expandablelistview system-services

在我的应用中,我有SearchView,点击后会在ExpandableListView内打开Fragment,显示搜索查询的一些结果。现在我有一个问题,每次我点击searchIcon应用程序崩溃。

MainActivity.Java:

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener
{

    private ExpListAdapter listAdapter;
    private ExpandableListView expList;
    private ArrayList<parentClass> parentList = new ArrayList<>();

    private boolean b = true;
    sql sqlo = new sql();



   @Override
   protected void onCreate (Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

       Intent sIntent = getIntent();
       if (Intent.ACTION_SEARCH.equals(sIntent.getAction())) {
           String query = sIntent.getStringExtra(SearchManager.QUERY);


       }}


    public void searchClick(MenuItem item){
        Fragment frag = new Results();

        FragmentTransaction ft  = getSupportFragmentManager().beginTransaction();

        ft.replace(R.id.FragFaecher,frag).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);


        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(this);

       searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
           @Override
           public void onViewAttachedToWindow(View v) {

           }

           @Override
           public void onViewDetachedFromWindow(View v) {
            Fragment frag = new FaecherFrag();
               FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
               ft.replace(R.id.FragFaecher,frag).commit();
           }
       });



        return true;
    }


        public void displayList(ExpandableListView expList){
        if(b){populateList();}

        listAdapter = new ExpListAdapter(MainActivity.this,parentList);
        expList.setAdapter(listAdapter);
        b = false;
    }

    private void populateList(){
        String[] cThema = sqlo.getAlleThemen(this);
        String[] cFormel = sqlo.getAlleFormeln(this);
        int cThemaLength = cThema.length;
        int cFormelLength = cFormel.length;
        childClass Child;

        ArrayList<childClass> childList = new ArrayList<>();

        for (int i=0; i<cThemaLength;i++){Child = new childClass(cThema[i]);childList.add(Child);}
        parentClass Parent = new parentClass("Themen",childList);
        parentList.add(Parent);

        childList = new ArrayList<>();

        for (int i=0;i<cFormelLength;i++){Child = new childClass(cFormel[i]);childList.add(Child);}
        Parent = new parentClass("Formeln",childList);

        parentList.add(Parent);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {

        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {

        return false;
    }
}

ExpListAdapter.Java:

public class ExpListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<parentClass> parentList;
    private ArrayList<parentClass> originalList;

    public ExpListAdapter(Context context, ArrayList<parentClass> parentList){
        this.context = context;
        this.parentList = new ArrayList<>();
        this.parentList.addAll(parentList);
        this.originalList = new ArrayList<>();
        this.originalList.addAll(parentList);
    }

    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<childClass> childList = parentList.get(groupPosition).getChildList();
        return childList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<childClass> childList = parentList.get(groupPosition).getChildList();
        return childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }



    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        childClass child = (childClass) getChild(groupPosition,childPosition);
        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.child_row,null);
        }
        TextView name = (TextView) convertView.findViewById(R.id.name);
        name.setText(child.getName().trim());

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup Parent) {
        parentClass parent = (parentClass) getGroup(groupPosition);
        if(convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.parent_row,null);

        }
        TextView heading = (TextView) convertView.findViewById(R.id.heading);
        heading.setText(parent.getName().trim());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void filterData(String query){

    }
}

片段:

public class Results extends Fragment {

    ExpandableListView expList;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_results, container, false);

        return rootView;

    }

    public void onStart(){
        super.onStart();
        expList = (ExpandableListView) getView().findViewById(R.id.expList);
        MainActivity ma = new MainActivity();
        ma.displayList(expList);
    }
}

我得到错误&#34; java.lang.IllegalStateException:在onCreate()&#34;之前,活动不可用的系统服务在getGroupView()方法上。但如果我没有弄错,我已经打电话给我的MainActivity onCreate。那么为什么会出现这种错误?

  

致命的例外:主要                                                                                    处理:formelhelfer5_10.formelhelfer5_10,PID:2301                                                                                    java.lang.IllegalStateException:onCreate()之前不能用于活动的系统服务                                                                                        在android.app.Activity.getSystemService(Activity.java:5581)                                                                                        at formelhelfer5_10.formelhelfer5_10.ExpListAdapter.getGroupView(ExpListAdapter.java:86)                                                                                        在android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)                                                                                        在android.widget.AbsListView.obtainView(AbsListView.java:2360)                                                                                        在android.widget.ListView.measureHeightOfChildren(ListView.java:1326)                                                                                        在android.widget.ListView.onMeasure(ListView.java:1233)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)                                                                                        在android.widget.LinearLayout.measureVertical(LinearLayout.java:758)                                                                                        在android.widget.LinearLayout.onMeasure(LinearLayout.java:640)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:715)                                                                                        在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)                                                                                        在android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)                                                                                        在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1192)                                                                                        在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                        在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)                                                                                        在android.widget.LinearLayout.measureVertical(LinearLayout.java:758)                                                                                        在android.widget.LinearLayout.onMeasure(LinearLayout.java:640)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)                                                                                        在android.widget.LinearLayout.measureVertical(LinearLayout.java:758)                                                                                        在android.widget.LinearLayout.onMeasure(LinearLayout.java:640)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)                                                                                        在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                        在com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)                                                                                        在android.view.View.measure(View.java:19731)                                                                                        在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)                                                                                        在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)                                                                                        在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)                                                                                        在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)                                                                                        在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6301)                                                                                        在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871)                                                                                        在android.view.Choreographer.doCallbacks(Choreographer.java:683)                                                                                        在android.view.Choreographer.doFrame(Choreographer.java:619)                                                                                        在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857)                                                                                        在android.os.Handler.handleCallback(Handler.java:751)   09-06 13:13:20.688 2301-2301 / formelhelfer5_10.formelhelfer5_10 E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:95)                                                                                        在android.os.Looper.loop(Looper.java:154)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

3 个答案:

答案 0 :(得分:1)

MainActivity ma = new MainActivity();
ma.displayList(expList);

永远不要使用new实例化活动。此类实例未针对您使用Activity的任何内容进行初始化,例如用作可以访问系统服务的Context

在片段中,您可以使用getActivity()访问附加的托管活动。使用它而不是实例化新活动。

答案 1 :(得分:0)

使用此:

View.inflate(ctx, R.layout.parent_row, null);

在这里:

 @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup Parent) {
        parentClass parent = (parentClass) getGroup(groupPosition);
        if(convertView == null){
            convertView = View.inflate(ctx, R.layout.parent_row, null); 

        }
        TextView heading = (TextView) convertView.findViewById(R.id.heading);
        heading.setText(parent.getName().trim());
        return convertView;
    }

答案 2 :(得分:0)

1)如图所示laalto - 从不使用new实例化活动。

更改

 public void onStart(){
    super.onStart();
    expList = (ExpandableListView) getView().findViewById(R.id.expList);
    MainActivity ma = new MainActivity();
    ma.displayList(expList);
}

 public void onStart(){
    super.onStart();
    expList = (ExpandableListView) getView().findViewById(R.id.expList);
    MainActivity ma = (MainActivity) getActivity();
    ma.displayList(expList);
}

2)创建如此深度的依赖

并不是一个好主意