2016-04-14 11 views
-9

上の仮想メソッドfindViewById(int)を呼び出すための試み」私はGoogle official tutorial
に応じフラグメントについてのコードを書いて、両者がほぼ同じですが、私はまだできませんでしたエラーを得た理由TextViewを見つけます。 これは4つのJavaクラスと3つのレイアウトファイルで構成されています。私はランドスケープで作業したいかもしれませんが、難しい部分はランドスケープにあります。にNullPointerException:nullのオブジェクト参照

activity_main.xml(土地)

デモがするように、私のレイアウト風景が表示テキスト

<TextView 
    android:id="@+id/container_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp" 
    android:textSize="12dp" 
    android:textStyle="bold" 
    xmlns:android="http://schemas.android.com/apk/res/android" /> 
ため

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     android:id="@+id/list_fragment" 
     class="com.example.jimjay.bookapp.List_fragment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <fragment 
     android:id="@+id/content_fragment" 
     class="com.example.jimjay.bookapp.ContenFragment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="3" /> 
</LinearLayout> 

content_view.xml

このまま

MainActivity.java

public class MainActivity extends AppCompatActivity implements List_fragment.OnListSelectedListener{ 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      if (findViewById(R.id.container) != null) { 

       if (savedInstanceState != null) { 
        return; 
       } 

       List_fragment firstFragment = new List_fragment(); 
       firstFragment.setArguments(getIntent().getExtras()); 
       getSupportFragmentManager().beginTransaction().add(R.id.container, firstFragment).commit(); 
      } 
     } 

     @Override 
     public void onChapterSelected(int position) { 

      ContenFragment contenFragment=(ContenFragment)getSupportFragmentManager().findFragmentById(R.id.content_fragment); 
      if (contenFragment != null){ 
       contenFragment.updateContent(position); 
      }else { 

       ContenFragment newFragment = new ContenFragment(); 
       Bundle args = new Bundle(); 
       args.putInt(ContenFragment.ARG_POSITION, position); 
       newFragment.setArguments(args); 
       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
       transaction.replace(R.id.container, newFragment); 
       transaction.addToBackStack(null); 

       transaction.commit(); 
      } 

     } 
    } 

List_fragment.java

public class List_fragment extends ListFragment { 

     OnListSelectedListener mCallBack; 

     interface OnListSelectedListener{ 
      void onChapterSelected(int position); 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      int layout=android.R.layout.simple_list_item_activated_1; 

      setListAdapter(new ArrayAdapter<>(getActivity(),layout,new Book_content().chapters)); 

     } 

     @Override 
     public void onStart() { 
      super.onStart(); 

      if (getFragmentManager().findFragmentById(R.id.list_fragment) != null){ 
       getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
      } 
     } 

     @Override 
     public void onAttach(Context context) { 
      super.onAttach(context); 

      try { 
       mCallBack=(OnListSelectedListener) context; 
      }catch (ClassCastException e){ 
       throw new ClassCastException(context.toString()+" must implement OnListSelectedListener"); 
      } 
     } 

     @Override 
     public void onListItemClick(ListView l, View v, int position, long id) { 
      mCallBack.onChapterSelected(position); 

      getListView().setItemChecked(position,true); 
     } 
    } 

ContenFragment.java

public class ContenFragment extends Fragment { 

final static String ARG_POSITION="position"; 
int mCurrentPosition=-1; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    if (savedInstanceState != null){ 
     mCurrentPosition=savedInstanceState.getInt(ARG_POSITION); 
    } 

    return inflater.inflate(R.layout.content_view,container,false); 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    Bundle args=getArguments(); 
    if (args!= null){ 
     updateContent(args.getInt(ARG_POSITION)); 
    }else if (mCurrentPosition != -1){ 
     updateContent(mCurrentPosition); 
    } 
} 

public void updateContent(int position) { 
    TextView textView = (TextView) getActivity().findViewById(R.id.container_content); 
    if (textView != null) { 
     textView.setText((new Book_content().contents_string[position])); 
    }else { 
     Log.e("LOG", "The textView is null"); 
    } 
    mCurrentPosition = position; 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    outState.putInt(ARG_POSITION,mCurrentPosition); 
} 

}

そして最後のJavaファイルはBook_content.classで、文字列だけです。 それは肖像画で働いていましたが、風景ではなく、わかりません。TextViewContenFragment.classには例外が発生します。これはnullです。私はcontent_viewTextViewを持っていました。何故ですか?ヘルプ

答えて

0

自分でそれを理解しようとすると、今私はします。 私はクラスの中で公式のサンプルコードを直接貼り付けたとしても、EXCEPTIONに遭遇するのは奇妙でした。私は他のインスタンス化をする必要があります。フラグメントを右側に変更してレイアウトします。ポートレートのレイアウトとして。

activity_main.xml(土地)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     android:id="@+id/list_fragment" 
     class="com.example.jimjay.bookapp.List_fragment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <FrameLayout 
     android:id="@+id/content_fragment_container" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2"></FrameLayout> 
</LinearLayout> 

だからcontentFragmentを追加するためにFragmentTransactionを使用しています。

関連する問題