0

私のアプリ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);
    }
}

getGroupView() メソッドで「java.lang.IllegalStateException: System services not available to Activities before onCreate()」というエラーが表示されます。しかし、間違っていなければ、既に MainActivity の onCreate を呼び出しています。では、なぜこのエラーが発生するのでしょうか?

致命的な例外: メイン プロセス: formelhelfer5_10.formelhelfer5_10、PID: 2301 java.lang.IllegalStateException: formelhelfer5_10.formelhelfer5_10.ExpListAdapter で android.app.Activity.getSystemService(Activity.java:5581) で onCreate() の前にアクティビティでシステム サービスを利用できません.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. android.widget の android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120) の view.View.measure(View.java:19731)。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.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. android.view の com.android.internal.policy.DecorView.onMeasure(DecorView.java:687) の widget.FrameLayout.onMeasure(FrameLayout.java:185)。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: android.os.Handler.dispatchMessage(Handler.java:95) で android.os.Looper.loop(Looper.java:154) で android.app.ActivityThread.main(ActivityThread.java:6077) でjava.lang.reflect.Method.invoke(ネイティブ メソッド) で com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) で com.android.internal.os.ZygoteInit.main(ZygoteInit.ジャワ:755)android.os.Looper.loop(Looper.java:154) での dispatchMessage(Handler.java:95)メソッド) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) で com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) でandroid.os.Looper.loop(Looper.java:154) での dispatchMessage(Handler.java:95)メソッド) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) で com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) で95) android.os.Looper.loop(Looper.java:154) で android.app.ActivityThread.main(ActivityThread.java:6077) で 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) で95) android.os.Looper.loop(Looper.java:154) で android.app.ActivityThread.main(ActivityThread.java:6077) で 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) で154) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 865) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755) で154) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 865) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755) でcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) で (ネイティブ メソッド) を呼び出し、com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) でcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) で (ネイティブ メソッド) を呼び出し、com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) で

4

3 に答える 3

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

でアクティビティをインスタンス化しないでくださいnew。このようなインスタンスは、システム サービスへのアクセスでActivityとして使用されるなど、 for を使用するものに対しては初期化されません。Context

フラグメントでは、添付されたホスティング アクティビティにgetActivity(). 新しいアクティビティをインスタンス化する代わりにそれを使用してください。

于 2016-09-06T13:31:06.593 に答える
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) 深い依存関係を作成するのは得策ではありません

于 2016-09-06T13:37:58.440 に答える