0

對於我的主細節流應用程序,我試圖在我的平板電腦上充氣我的Continents列表視圖,但它不會這樣做,並且最終在我的日誌貓中出現這些錯誤。有誰知道我做錯了什麼?片段活動不充氣

大洲列表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) 

enter image description here

+0

檢查活動中進口像[有](http://stackoverflow.com/questions/9196823/add- listfragment-with-fragmenttransaction-is-supported-supported) –

回答

3

您的onAttach實現,說

if (!(activity instanceof Callbacks)) { 
    throw new IllegalStateException("Activity must implement fragment's callbacks."); 
} 

要解決必須添加implements CallbacksActivity託管的片段。

編輯:

在活動的XML

,更換

<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/continents_list_contaniner" 
     android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> 

和活動的onCreate(),setContentView後做:

FragmentManager fm = getSupportFragmentManager(); 
FragmentTransaction ft = fm.beginTransaction(); 
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment()); 
ft.commit(); 

EDIT2

在活動

更換

import android.app.FragmentManager; 
import android.app.FragmentTransaction; 

import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction 
+0

仍然崩潰。你可以嘗試從頭開始創建,看看你是否有任何錯誤。我試圖實現的目標可以在這個鏈接中看到:[master detail objective](http://pastebin.com/czcUtEya) – MacaronLover

+0

我會明天嘗試,但不是承諾。 – Blackbelt

+0

好的,很酷。 BFN。 – MacaronLover