带有Loader的片段(适用于Tablet,但不适用于手机 - >错误未附加到Activity)

时间:2013-10-15 18:51:11

标签: android fragment avd loader

我有两个片段应该像picture一样工作。

enter image description here

使用平板电脑AVD可以很好地工作,但是当我使用手机AVD时总会出现以下错误消息:

 10-15 07:31:46.450: E/AndroidRuntime(721): FATAL EXCEPTION: main
 10-15 07:31:46.450: E/AndroidRuntime(721): java.lang.IllegalStateException: Fragment EventsFragment{411e6440 id=0x7f070005} not attached to Activity
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.support.v4.app.Fragment.getLoaderManager(F ragment.java:823)
 10-15 07:31:46.450: E/AndroidRuntime(721): at com.example.activity.EventsFragment.updateEventsVi ew(EventsFragment.java:80)
 10-15 07:31:46.450: E/AndroidRuntime(721): at com.example.activity.EventsActivity.onYearSelected (EventsActivity.java:79)
 10-15 07:31:46.450: E/AndroidRuntime(721): at com.example.activity.YearsFragment.onListItemClick (YearsFragment.java:63)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.support.v4.app.ListFragment$2.onItemClick( ListFragment.java:58)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AdapterView.performItemClick(Adapte rView.java:298)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AbsListView.performItemClick(AbsLis tView.java:1086)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AbsListView$PerformClick.run(AbsLis tView.java:2859)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AbsListView$1.run(AbsListView.java: 3533)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.os.Handler.handleCallback(Handler.java:615 )
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.os.Handler.dispatchMessage(Handler.java:92 )
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.os.Looper.loop(Looper.java:137)
 10-15 07:31:46.450: E/AndroidRuntime(721): at android.app.ActivityThread.main(ActivityThread.jav a:4745)
 10-15 07:31:46.450: E/AndroidRuntime(721): at java.lang.reflect.Method.invokeNative(Native Method)
 10-15 07:31:46.450: E/AndroidRuntime(721): at java.lang.reflect.Method.invoke(Method.java:511)
 10-15 07:31:46.450: E/AndroidRuntime(721): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:786)
 10-15 07:31:46.450: E/AndroidRuntime(721): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553)
 10-15 07:31:46.450: E/AndroidRuntime(721): at dalvik.system.NativeStart.main(Native Method)

使用AVD: 平板电脑:平台:3.0 API级别:11 手机:平台:4.1.2 API级别:16

EventsActivity.java

public class EventsActivity extends FragmentActivity implements YearsFragment.OnYearSelectedListener{

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.years_events);

    if (findViewById(R.id.fragment_container) != null) { //Handset
        if (savedInstanceState != null) {
            return;
        }

        //Fragment A
        YearsFragment firstFragment = new YearsFragment(); 
        firstFragment.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
    }
}

@Override
public void onYearSelected(Years year) {
    EventsFragment eventFrag = (EventsFragment) getSupportFragmentManager().findFragmentById(R.id.event_fragment);

    if (eventFrag != null) { //Tablet
        //update Fragment B
        eventFrag.updateEventsView(year);
    } else { //Handset
        //start Fragment B
        EventsFragment newFragment = new EventsFragment();          
        newFragment.updateEventsView(year);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}}

EventsFragment(片段B)

public class EventsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Events>>{
EventsAdapter adapter;
private int year;

OnEventSelectedListener mCallback;
public interface OnEventSelectedListener {
    public void onEventSelected(Events event);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);


    adapter = new EventsAdapter(getActivity());
    setListAdapter(adapter);

    setEmptyText("No Data Here");
    setListShown(false);

    year = Calendar.getInstance().get(Calendar.YEAR);
    getLoaderManager().initLoader(year, null, this);
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);

    try{
        mCallback = (OnEventSelectedListener) activity;
    } catch (ClassCastException e){
        throw new ClassCastException(activity.toString() + "must implement OnEventsSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    mCallback.onEventSelected(adapter.getItem(position));
}

public void updateEventsView(Years year) {
    this.year = Integer.parseInt(year.getYear());
    getLoaderManager().initLoader(this.year, null, this); //TODO error  }

//Loader
@Override
public Loader<List<Events>> onCreateLoader(int arg0, Bundle arg1) {
    return new EventsLoader(getActivity(),this.year);
}

@Override
public void onLoadFinished(Loader<List<Events>> arg0, List<Events> data) {
    adapter.setData(data);

    if(isResumed()){
        setListShown(true);
    }else{
        setListShownNoAnimation(false);
    }
}

@Override
public void onLoaderReset(Loader<List<Events>> arg0) {
    adapter.setData(null);
}

为什么它不适用于手机?

迎接Mr.P。

0 个答案:

没有答案