-1

複数のタブのアプリケーションで、タブ(Tab2)の1つに拡張可能なリストビューとsearchviewを追加しました。getComponentName()でエラーが発生しました。私はgetActivity()。getComponentName()を追加しました。この行で実行するとエラーはありませんが、NullpointerExceptionが発生します。アプリケーションがクラッシュします。何か案は?getActivity()。getComponentName()into 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); 

} 

と活動:断片ライフサイクルgetActivityで

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

+0

[NullPointerExceptionとは何か、それを修正する方法は?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)の可能な複製) –

+0

クラッシュログを追加できますか? – Sanjeet

+0

java.lang.NullPointerException com.example.orestis.myapp.Tab2.onCreateView(Tab2.java:34) android.support.v4.app.Fragment.performCreateView(Fragment.java :2080) 34行目はsearch.setSearchableInfo(searchManager.getSearchableInfo(getActivity()。getComponentName()))です。 – Orestis

答えて

4

()活動が作成された後に呼び出すことができ
は、ここでは、コードの重要な行です。 getActivityは、onActivityCreatedの後で使用できます。

onCreateViewコードでgetActivityを使用することはできません。

+0

まずはお返事ありがとうございます:)感謝!だから私はそれを使用することができない場合は、正しく動作するためにgetComponentName()を実装する必要がありますか? OnCreateViewではなくonActivityCreatedをオーバーライドする必要がありますか? – Orestis

+0

あなたのコードはアクティビティ情報を必要とするので、コードをonActivityCreatedに移動する方がよいでしょう。 –

0

よLOresolví詐欺エステartículohttps://es.stackoverflow.com/questions/108085/agregar-menu-de-accion-en-un-fragmento-android-studio MeはquedóのASI: //エン・エルのonCreate・デ・マイルfragmentoagregué

setHasOptionsMenu(true); 

///エン・エルonCreateOptionsMenuデルfragmento LOreemplazé詐欺

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


    // Associate searchable configuration with the SearchView 
    SearchManager searchManager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE); 
    searchView = (SearchView) menu.findItem(R.id.action_search) 
      .getActionView(); 
    searchView.setSearchableInfo(searchManager 
      .getSearchableInfo(getActivity().getComponentName())); 
    searchView.setMaxWidth(Integer.MAX_VALUE); 

    // listening to search query text change 
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      // filter recycler view when query submitted 
      mAdapter.getFilter().filter(query); 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String query) { 
      // filter recycler view when text is changed 
      mAdapter.getFilter().filter(query); 
      return false; 
     } 
    }); 


} 

//Yañadíen el framento

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_search) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
+0

英語の返信が必要です。 –

+0

//私のフラグメントのonCreateに私は を追加しました///フラグメントonCreateOptionsMenuで私はそれを と置き換えました//そしてフラグメントに追加しました –

関連する問題