Al intentar adaptar mi aplicación a otros dispositivos (tablets) me esta dando error, por lo siguiente, mi layout (que he llamado activity_wine_list) lo tengo adaptado para diferentes situaciones, lo he creado segun la situacion de una forma u otra, de esta forma:
Esto para el modo movil.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_dark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="10dp"
android:id="@+id/toolbar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list">
</FrameLayout>
</LinearLayout>
Este es para tablet modo vertical:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_dark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="10dp"
android:id="@+id/toolbar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/winery">
</FrameLayout>
</LinearLayout>
Y este último tambien para tablets pero modo horizontal:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_dark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="10dp"
android:id="@+id/toolbar"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/list"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/winery"/>
</LinearLayout>
</LinearLayout>
Utilizan el id "list" y el id "winery", bien pues, el problema es que al hacer correr la aplicacion me da error indicandome que no encuentra el id "list" no me dice nada sobre "winery" entiendo porque mi primera condicion es verificar el list.
Aqui el codigo para la actividad:
public class WineListActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This here, will choose one of the three options I have in R.layout.activity_wine_list.
setContentView(R.layout.activity_wine_list);
//Visible Toolbar.
setSupportActionBar((Toolbar)findViewById(R.id.toolbar));
FragmentManager fm = getSupportFragmentManager();
//I ask if they have already the listFragment
if (findViewById(R.id.list) != null) {
Fragment listFragment = fm.findFragmentById(R.id.list);
//If no exist, I created it.
if (listFragment == null) {
listFragment = new WineListFragment();
//Once is created, now add the fragmentManager to it
fm.beginTransaction().add(R.id.list, listFragment).commit();
}
}
//Them, I goona do something like that but, with the winery.
if (findViewById(R.id.winery) != null) {
Fragment wineryFragment = fm.findFragmentById(R.id.winery);
if (wineryFragment == null){
//Set newInstance because my WineryFragment have it has as an argument
wineryFragment = WineryFragment.newInstance(0);
//And them, just the same.
fm.beginTransaction().add(R.id.list, wineryFragment).commit();
}
}
}
}
Aqui el codigo Fragment:
public WineListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_wine_list, container, false);
//Reference to ListView
ListView listView = (ListView) root.findViewById(android.R.id.list);
//Acceding Winery
Winery winery = Winery.getInstance();
//List adapter
ArrayAdapter<Wine> adapter = new ArrayAdapter<Wine>(getActivity(), android.R.layout.simple_list_item_1, winery.getWineList());
//Saying the adapter to the listView
listView.setAdapter(adapter);
//What to do when the user press one item on the list.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent wineryIntent = new Intent(getActivity(), WineryActivity.class);
wineryIntent.putExtra(WineryActivity.EXTRA_WINE_INDEX, i);
startActivity(wineryIntent);
}
});
return root;
}
}
Y por último el codigo de donde saco las constantes:
public class WineryFragment extends Fragment implements ViewPager.OnPageChangeListener {
public static final String ARG_WINE_INDEX ="jhon.casique.baccus.controller.fragment.WineryFragment.ARG_WINE_INDEX";
private ViewPager mPager = null;
private ActionBar mActionBar = null;
private Winery mWinery = null;
//Creating new method to access him from WineryActivity. (newInstance)
public static WineryFragment newInstance(int wineIndex){
Bundle arguments = new Bundle();
arguments.putInt(ARG_WINE_INDEX, wineIndex);
WineryFragment fragment = new WineryFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.fragment_winery, container, false);
mPager = (ViewPager) root.findViewById(R.id.pager);
mPager.setAdapter(new WineryPagerAdapter(getFragmentManager()));
mWinery = Winery.getInstance();
//Getting reference about the ActionBar
mActionBar = (ActionBar) ((AppCompatActivity)getActivity()).getSupportActionBar();
//Listener.
mPager.addOnPageChangeListener(this);
//This value is for rotate the pageView and the ActionBar
int initialWineIndex = getArguments().getInt(ARG_WINE_INDEX);
//I ask to update my ActionBar and pageView to charge the wine it is pass as a argument.
mPager.setCurrentItem(initialWineIndex);
updateActionBar(initialWineIndex);
return root;
}
//Creating method to update my data page wine (I gonna need a Winery)
public void updateActionBar (int index) {
mActionBar.setTitle(mWinery.getWine(index).getName());
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//Changing by position in the PageView name by name.
updateActionBar(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_winery, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean superValue = super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.menu_next && mPager.getCurrentItem() < mWinery.getWineCount() -1) {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
return true;
}
else if (item.getItemId() == R.id.menu_prev && mPager.getCurrentItem() > 0) {
mPager.setCurrentItem(mPager.getCurrentItem() -1);
return true;
}
else {
return superValue;
}
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuNext = menu.findItem(R.id.menu_next);
MenuItem menuPrev = menu.findItem(R.id.menu_prev);
//Setting when they gonna be enabled.
menuNext.setEnabled(mPager.getCurrentItem() < mWinery.getWineCount() - 1);
menuPrev.setEnabled(mPager.getCurrentItem() > 0);
}
}
Si alguien me puede ayudar, se lo agradeceria mucho.
Gracias.
Actualizado 28/10
Agradezco por su ayuda pero, aun así esto me sigue dando error, en concreto este: FATAL EXCEPTION: main Process: jhon.casique.baccus, PID: 2238 java.lang.RuntimeException: Unable to start activity ComponentInfo{jhon.casique.baccus/jhon.casique.baccus.controller.activity.WineListActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0b0057 (jhon.casique.baccus:id/list) for fragment WineryFragment{a50a4b98 #0 id=0x7f0b0057}
Por algun motivo el id "list" no lo reconoce y da igual si le hago refactor o haga lo que haga, no quiere funcionar. No entiendo que demonios pasa.