Fragment da erro
Prezados Boa tarde,
Minha aplicação da erro ao abrir em um tablet e não consigo identificar o erro ele informa que tenho que habilitar: Unable to start activity ComponentInfo{com.example.everton.horoscopo/com.example.everton.horoscopo.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class fragment.
Não entendi o erro segue os código se alguém pode me ajudar agradeço esse projeto é para o fim desse mês.
MaindActivity:
package com.example.everton.horoscopo;
public class MainActivity extends AppCompatActivity {
HoroscopoDetailFragment horoscopoDetailFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(istablet()){
Log.d("Fragment", "entrou!!");
Fragment fragment = new Fragment();
HoroscopoDetailFragment horoscopoDetailFragment = new HoroscopoDetailFragment();
/* FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frag_detalhe, fragment );
ft.commit();*/
HoroscopoDetailFragment horoscopoDetailFragment1 = new HoroscopoDetailFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frag_detalhe, fragment)
.commit();
}else{
Toast.makeText(this, "não é Tablet", Toast.LENGTH_SHORT).show();
}
}
private boolean istablet(){
return getResources().getBoolean(R.bool.tablet);
}
private boolean isSmartphone(){
return getResources().getBoolean(R.bool.smartphone);
}
}
Layout do tablet
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.everton.horoscopo.MainActivity">
<fragment
android:layout_width="409dp"
android:layout_height="match_parent"
android:name="com.example.everton.horoscopo.fragments.HoroscopoListFragment"
android:id="@+id/fragmentLista"/>
<fragment
android:layout_width="409dp"
android:layout_height="match_parent"
android:name="com.example.everton.horoscopo.fragments.HoroscopoDetailFragment"
android:id="@+id/fragmentDetail"/>
</RelativeLayout>
Activity do detalhe
package com.example.everton.horoscopo;
public class HoroscopoDetailActivity extends AppCompatActivity {
HoroscopoRepositorio horoscopoRepositorio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horoscopo_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
horoscopoRepositorio = new HoroscopoRepositorio(this);
Intent intent = getIntent();
String id = intent.getStringExtra("Horosc");
Cursor cursor = horoscopoRepositorio.consulta(HoroscopoContract._ID + " =" + id);
cursor.moveToFirst();
int icone = cursor.getInt(cursor.getColumnIndex(HoroscopoContract.COL_ICONE));
String signo = cursor.getString(cursor.getColumnIndex(HoroscopoContract.COL_SUNSIGN));
ImageView img = (ImageView) findViewById(R.id.detail_icone);
CollapsingToolbarLayout collapser = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
Resources res = this.getResources();
TypedArray logos = res.obtainTypedArray(R.array.logos);
img.setImageDrawable(logos.getDrawable(icone));
collapser.setTitle(signo);
}
}
Fragment do detail
/**
* Created by everton.vasconcelos on 23/09/2016.
*/
public class HoroscopoDetailFragment extends Fragment {
HoroscopoRepositorio horoscopoRepositorio;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail, container, false);
horoscopoRepositorio = new HoroscopoRepositorio(getActivity());
Intent intent = getActivity().getIntent();
String id = intent.getStringExtra("Horosc");
Cursor cursor = horoscopoRepositorio.consulta(HoroscopoContract._ID + " =" + id);
cursor.moveToFirst();
String msg = cursor.getString(cursor.getColumnIndex(HoroscopoContract.COL_MESSAGEM));
String data = cursor.getString(cursor.getColumnIndex(HoroscopoContract.COL_DATA));
TextView txtData = (TextView) view.findViewById(R.id.frag_item_data);
TextView msge = (TextView) view.findViewById(R.id.frag_item_msg);
msge.setText(msg);
txtData.setText(data);
return view;
}
}
Layout do detail fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HoroscopoDetailActivity"
android:background="@color/black"
tools:showIn="@layout/activity_horoscopo_item">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/horosc_detalhe"
android:padding="10dp">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.everton.horoscopo.fragments.HoroscopoDetailFragment"
android:id="@+id/frag_detalhe"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
No smartphone funciona só da erro ao chamar o layout do tablet.
Minha aplicação da erro ao abrir em um tablet e não consigo identificar o erro ele informa que tenho que habilitar: Unable to start activity ComponentInfo{com.example.everton.horoscopo/com.example.everton.horoscopo.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class fragment.
Não entendi o erro segue os código se alguém pode me ajudar agradeço esse projeto é para o fim desse mês.
MaindActivity:
package com.example.everton.horoscopo;
public class MainActivity extends AppCompatActivity {
HoroscopoDetailFragment horoscopoDetailFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(istablet()){
Log.d("Fragment", "entrou!!");
Fragment fragment = new Fragment();
HoroscopoDetailFragment horoscopoDetailFragment = new HoroscopoDetailFragment();
/* FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frag_detalhe, fragment );
ft.commit();*/
HoroscopoDetailFragment horoscopoDetailFragment1 = new HoroscopoDetailFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frag_detalhe, fragment)
.commit();
}else{
Toast.makeText(this, "não é Tablet", Toast.LENGTH_SHORT).show();
}
}
private boolean istablet(){
return getResources().getBoolean(R.bool.tablet);
}
private boolean isSmartphone(){
return getResources().getBoolean(R.bool.smartphone);
}
}
Layout do tablet
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.everton.horoscopo.MainActivity">
<fragment
android:layout_width="409dp"
android:layout_height="match_parent"
android:name="com.example.everton.horoscopo.fragments.HoroscopoListFragment"
android:id="@+id/fragmentLista"/>
<fragment
android:layout_width="409dp"
android:layout_height="match_parent"
android:name="com.example.everton.horoscopo.fragments.HoroscopoDetailFragment"
android:id="@+id/fragmentDetail"/>
</RelativeLayout>
Activity do detalhe
package com.example.everton.horoscopo;
public class HoroscopoDetailActivity extends AppCompatActivity {
HoroscopoRepositorio horoscopoRepositorio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horoscopo_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
horoscopoRepositorio = new HoroscopoRepositorio(this);
Intent intent = getIntent();
String id = intent.getStringExtra("Horosc");
Cursor cursor = horoscopoRepositorio.consulta(HoroscopoContract._ID + " =" + id);
cursor.moveToFirst();
int icone = cursor.getInt(cursor.getColumnIndex(HoroscopoContract.COL_ICONE));
String signo = cursor.getString(cursor.getColumnIndex(HoroscopoContract.COL_SUNSIGN));
ImageView img = (ImageView) findViewById(R.id.detail_icone);
CollapsingToolbarLayout collapser = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
Resources res = this.getResources();
TypedArray logos = res.obtainTypedArray(R.array.logos);
img.setImageDrawable(logos.getDrawable(icone));
collapser.setTitle(signo);
}
}
Fragment do detail
/**
* Created by everton.vasconcelos on 23/09/2016.
*/
public class HoroscopoDetailFragment extends Fragment {
HoroscopoRepositorio horoscopoRepositorio;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail, container, false);
horoscopoRepositorio = new HoroscopoRepositorio(getActivity());
Intent intent = getActivity().getIntent();
String id = intent.getStringExtra("Horosc");
Cursor cursor = horoscopoRepositorio.consulta(HoroscopoContract._ID + " =" + id);
cursor.moveToFirst();
String msg = cursor.getString(cursor.getColumnIndex(HoroscopoContract.COL_MESSAGEM));
String data = cursor.getString(cursor.getColumnIndex(HoroscopoContract.COL_DATA));
TextView txtData = (TextView) view.findViewById(R.id.frag_item_data);
TextView msge = (TextView) view.findViewById(R.id.frag_item_msg);
msge.setText(msg);
txtData.setText(data);
return view;
}
}
Layout do detail fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HoroscopoDetailActivity"
android:background="@color/black"
tools:showIn="@layout/activity_horoscopo_item">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/horosc_detalhe"
android:padding="10dp">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.everton.horoscopo.fragments.HoroscopoDetailFragment"
android:id="@+id/frag_detalhe"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
No smartphone funciona só da erro ao chamar o layout do tablet.
Everton
Curtidas 0
Respostas
Everton
09/10/2016
Resolvi o problema. O problema era que no main activity não estava passando o Bundle para a classe do detail fragment segue a solução:
Main activity
inicialmente passei o Bundle pela main mas depois criei um metodo e passei por la.
DetailFragment
Main activity
frag = findViewById(R.id.fragmentDetail); HoroscopoDetailFragment horoscopoDetailFragment= HoroscopoDetailFragment.newInstance(id); horoscopoDetailFragment.teste(true); getSupportFragmentManager() .beginTransaction() .replace(R.id.fragmentDetail, horoscopoDetailFragment) .show(horoscopoDetailFragment) .commit();
inicialmente passei o Bundle pela main mas depois criei um metodo e passei por la.
DetailFragment
public static HoroscopoDetailFragment newInstance(long id) { Bundle args = new Bundle(); args.putString("Horosc", String.valueOf(id)); HoroscopoDetailFragment horoscopoDetailFragment= new HoroscopoDetailFragment(); horoscopoDetailFragment.setArguments(args); return horoscopoDetailFragment; }
GOSTEI 0