Reputation: 8408
For my master detail flow app I'm trying to inflate my Continents list view on my tablet but it won't do so and I end up with these errors in my log cat. Does anyone know what I'm doing wrong?
Continents List xml
<LinearLayout 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" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal"
android:showDividers="middle" tools:context=".ItemListActivity">
<fragment android:id="@+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout android:id="@+id/item_detail_container" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="3" />
</LinearLayout>
ContinentsListActivity.java
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class ContinentsListActivity extends ActionBarActivity
implements ContinentsListFragment.Callbacks {
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_continents_list);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ContinentsListFragment) getSupportFragmentManager()
.findFragmentById(R.id.continents_list))
.setActivateOnItemClick(true);
}
}
@Override
public void onItemSelected(String id) {
if("1".equals(id)){
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.continents_list, fragment)
.commit();
} else {
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
startActivity(detailIntent);
}
}
}
}
ContinentsListFragment.java
package com.apptacularapps.md;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContinentsListFragment extends ListFragment {
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private Callbacks mCallbacks = sContinentsCallbacks;
private int mActivatedPosition = ListView.INVALID_POSITION;
public interface Callbacks {
public void onItemSelected(String id);
}
private static Callbacks sContinentsCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
public ContinentsListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
setListAdapter(new ArrayAdapter<ContinentsContent.ContinentsItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
ContinentsContent.ITEMS));
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the Continents implementation.
mCallbacks = sContinentsCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
mCallbacks.onItemSelected(ContinentsContent.ITEMS.get(position).id);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
public void setActivateOnItemClick(boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
Logcat
05-20 20:29:42.523 32338-32338/com.apptacularapps.md E/libprocessgroup﹕ failed to make and chown /acct/uid_10056: Read-only file system
05-20 20:29:42.531 32338-32338/com.apptacularapps.md W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
05-20 20:29:42.532 32338-32338/com.apptacularapps.md I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-20 20:29:43.002 32338-32352/com.apptacularapps.md I/art﹕ Background sticky concurrent mark sweep GC freed 3104(261KB) AllocSpace objects, 0(0B) LOS objects, 26% free, 829KB/1135KB, paused 11.699ms total 50.909ms
05-20 20:29:43.006 32338-32359/com.apptacularapps.md D/OpenGLRenderer﹕ Render dirty regions requested: true
05-20 20:29:43.034 32338-32338/com.apptacularapps.md D/﹕ HostConnection::get() New Host Connection established 0xa693a490, tid 32338
05-20 20:29:43.070 32338-32338/com.apptacularapps.md D/Atlas﹕ Validating map...
05-20 20:29:43.227 32338-32359/com.apptacularapps.md D/﹕ HostConnection::get() New Host Connection established 0xa693a8a0, tid 32359
05-20 20:29:43.246 32338-32359/com.apptacularapps.md I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-20 20:29:43.376 32338-32359/com.apptacularapps.md D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 20:29:43.397 32338-32359/com.apptacularapps.md W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-20 20:29:43.397 32338-32359/com.apptacularapps.md W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa696eba0, error=EGL_SUCCESS
05-20 20:29:46.874 32338-32338/com.apptacularapps.md D/AndroidRuntime﹕ Shutting down VM
05-20 20:29:46.875 32338-32338/com.apptacularapps.md E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.apptacularapps.md, PID: 32338
java.lang.IllegalStateException: Activity must implement fragment's callbacks.
at com.apptacularapps.md.ContinentsListFragment.onAttach(ContinentsListFragment.java:89)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Upvotes: 0
Views: 719
Reputation: 157437
your implementation of onAttach
, says
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
To fix you have to add implements Callbacks
to the Activity
hosting the Fragment.
Edit:
in the xml of your activity, replace
<fragment android:id="@+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
tools:layout="@android:layout/list_content" />
with
<FrameLayout android:id="@+id/continents_list_contaniner"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
and onCreate() of the Activity, after setContentView
do:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();
Edit2:
in your Activity replace
import android.app.FragmentManager;
import android.app.FragmentTransaction;
with
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction
Upvotes: 3