Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot find view id when implement different layout for different screen size in android
    text
    copied!<p>I am new about fragment. i have app where implementing fragment and it has different layout. when app run in smartphone and tablet in portrait it only show one layout(with one fragment) and when in tablet in landscape it show two pane layout(with two fragments).</p> <p>i am getting error when try to get view id in fragment layout. below are activity_main_screen.xml code in res/layout and res/layout-large,</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>this is activity_main_screen.xml code in res/layout-large-land,</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/grey"&gt; &lt;fragment android:name="com.pongodev.dr.know.QuestionFragment" android:id="@+id/question_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="2dp"/&gt; &lt;fragment android:name="com.pongodev.dr.know.AnswerFragment" android:id="@+id/answer_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>this is fragment view question_view.xml containing button, edittext, and listview,</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainScreen" android:background="@drawable/app_background"&gt; &lt;LinearLayout android:id="@+id/lytSearchBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/green" android:orientation="horizontal" android:layout_alignParentTop="true" android:gravity="center_vertical" android:padding="5dp"&gt; &lt;EditText android:id="@+id/edtSearch" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="14sp" android:hint="@string/type_question" android:textColor="@android:color/widget_edittext_dark" android:layout_marginRight="5dp" android:background="@drawable/edittext_style" android:padding="12dp" android:singleLine="true"/&gt; &lt;ImageButton android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_menu_search" android:background="@drawable/button_style" android:padding="5dp"/&gt; &lt;/LinearLayout&gt; &lt;ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fadeScrollbars="true" android:fastScrollEnabled="true" android:divider="@android:color/transparent" android:dividerHeight="3dp" android:layout_below="@+id/lytSearchBar"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>and this is QuestionFragment.java code,</p> <pre><code>public class QuestionFragment extends ListFragment { OnQuestionSelectedListener mCallback; ImageButton btnSearch; QuestionListAdapter qla; public interface OnQuestionSelectedListener{ public void onQuestionSelected(int position); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.question_view, container, false); //list = (ListView) v.findViewById(R.id.list); prgLoading = (ProgressBar) v.findViewById(R.id.prgLoading); txtAlert = (TextView) v.findViewById(R.id.txtAlert); btnSearch = (ImageButton) v.findViewById(R.id.btnSearch); btnSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getActivity(), "button click", Toast.LENGTH_SHORT).show(); } }); return v; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); qla = new QuestionListAdapter(getActivity()); new getQuestionList().execute(); //setListAdapter(qla); } @Override public void onStart() { super.onStart(); // When in two-pane layout, set the listview to highlight the selected list item // (We do this during onStart because at the point the listview is available.) if (getFragmentManager().findFragmentById(R.id.answer_fragment) != null) { getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception. try { mCallback = (OnQuestionSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Notify the parent activity of selected item mCallback.onQuestionSelected(position); // Set the item as checked to be highlighted when in two-pane layout getListView().setItemChecked(position, true); } public class getQuestionList extends AsyncTask&lt;Void, Void, Void&gt;{ getQuestionList(){ if(!prgLoading.isShown()){ prgLoading.setVisibility(0); txtAlert.setVisibility(8); } } @Override protected void onPreExecute() { // TODO Auto-generated method stub } @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub //parseJSONData(); return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub prgLoading.setVisibility(8); if((Question.length &gt; 0)){ setListAdapter(qla); }else{ txtAlert.setText(R.string.no_connection_alert); txtAlert.setVisibility(0); } } } } </code></pre> <p>when i try to get button id in QuestionFragment the app get error. how to fix this error?</p> <p><strong>UPDATE</strong>: i do not know why, suddenly my button id problem solve. but, there is other problem with ProgressBar id and TextView id. here is the logcat.</p> <pre><code>01-08 21:53:34.942: E/AndroidRuntime(23579): FATAL EXCEPTION: main 01-08 21:53:34.942: E/AndroidRuntime(23579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pongodev.dr.know/com.pongodev.dr.know.MainScreen}: java.lang.NullPointerException 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.access$600(ActivityThread.java:128) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.os.Handler.dispatchMessage(Handler.java:99) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.os.Looper.loop(Looper.java:137) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.main(ActivityThread.java:4514) 01-08 21:53:34.942: E/AndroidRuntime(23579): at java.lang.reflect.Method.invokeNative(Native Method) 01-08 21:53:34.942: E/AndroidRuntime(23579): at java.lang.reflect.Method.invoke(Method.java:511) 01-08 21:53:34.942: E/AndroidRuntime(23579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 01-08 21:53:34.942: E/AndroidRuntime(23579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 01-08 21:53:34.942: E/AndroidRuntime(23579): at dalvik.system.NativeStart.main(Native Method) 01-08 21:53:34.942: E/AndroidRuntime(23579): Caused by: java.lang.NullPointerException 01-08 21:53:34.942: E/AndroidRuntime(23579): at com.pongodev.dr.know.QuestionFragment$getQuestionList.&lt;init&gt;(QuestionFragment.java:140) 01-08 21:53:34.942: E/AndroidRuntime(23579): at com.pongodev.dr.know.QuestionFragment.onCreate(QuestionFragment.java:97) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.Fragment.performCreate(Fragment.java:1437) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1137) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.Activity.performStart(Activity.java:4475) 01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1943) 01-08 21:53:34.942: E/AndroidRuntime(23579): ... 11 more </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload