adriankroeger adriankroeger - 3 months ago 26
Android Question

System services not available to Activities before onCreate() in Expandable ListView

In my app i have

SearchView
which, when clicked, opens an
ExpandableListView
inside a
Fragment
that displays some results for the searchquery. Now i have the problem that everytime i click the searchIcon the app crashes.

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){

}
}


Fragment:

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);
}
}


I get the Error "java.lang.IllegalStateException: System services not available to Activities before onCreate()" on the getGroupView() method. But if i don't get it wrong, i have already called the onCreate of my MainActivity. So why does this Error occur?


FATAL EXCEPTION: main
Process: formelhelfer5_10.formelhelfer5_10, PID: 2301
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5581)
at formelhelfer5_10.formelhelfer5_10.ExpListAdapter.getGroupView(ExpListAdapter.java:86)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
at android.widget.AbsListView.obtainView(AbsListView.java:2360)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
at android.widget.ListView.onMeasure(ListView.java:1233)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19731)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1192)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19731)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19731)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at 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)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Answer

Use this:

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

inside this:

 @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;
    }