I wrote codes about fragment according to the Google official tutorial
and both of them are almost the same,but why I still got error that couldn't find the TextView. It consists of four java class, and three layout file.I may want to work on the landscape,the tricky part is in the landscape,for the portrait works find.Therefor,I would only pressent the landscape code for well readability.
activity_main.xml(land)
As the demo does,my layout-landscape remains
<?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
This for display text
<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" /> 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); } }
And the last Java file is Book_content.class, which just contain strings. So it worked in portrait, but not in landscape, I can't figure it out, and the problem I found is the TextView in ContenFragment.class raises the exception, which is null. but I did have a TextView in the content_view. Why is that? HELP
No comments:
Post a Comment