In einer App mit mehreren Tabs, ich eine erweiterbare Listview und eine Suchansicht in einer der Registerkarten (Tab2) hinzugefügt. Ich hatte einen Fehler auf getComponentName() als nicht für Fragment unterstützt .So habe ich getActivity(). GetComponentName() hinzugefügt. Es gibt keine Fehler aber NullpointerException, wenn es in dieser Zeile ausgeführt wird. Die App stürzt ab. Irgendwelche Ideen?
Hier sind die wichtigen Linien Code:getActivity(). GetComponentName() in Fragment Nullpointerexception
public class Tab2 extends Fragment implements SearchView.OnQueryTextListener,SearchView.OnCloseListener{
private SearchView search;
private MylistAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Continent> continentList = new ArrayList<Continent>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2, container, false);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) getActivity().findViewById(R.id.search);
search.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
displayList();
expandAll();
return rootView;
}
private void expandAll(){
int count = listAdapter.getGroupCount();
for (int i=0; i<count; i++)
{
myList.expandGroup(i);
}
}
private void displayList() {
// display the list
loadSomeData();
// get reference to the ExpandableListView
myList = (ExpandableListView) getView().findViewById(R.id.expandableList);
// create the adapter by passing your ArrayList data
listAdapter = new MylistAdapter(getActivity(), continentList);
// attach the adapter to the list
myList.setAdapter(listAdapter);
}
und die Aktivität:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<SearchView
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<ExpandableListView
android:id="@+id/expandableList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/search" >
</ExpandableListView>
Mögliches Duplikat von [Was ist eine NullPointerException und wie behebe ich sie?] (Http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –
können Sie Absturzprotokolle – Sanjeet
java.lang.NullPointerException bei com.example.orestis.myapp.Tab2.onCreateView (Tab2.java:34) bei android.support.v4.app.Fragment.performCreateView (Fragment.java hinzufügen : 2080) Zeile 34 ist search.setSearchableInfo (searchManager.getSearchableInfo (getActivity(). GetComponentName())); – Orestis