0

在android中开发应用程序时遇到问题。经过大量搜索后,我没有找到解决方案。

一个有两个片段面板的应用程序。第一个,Fragment显示类别和子类别的列表,用户可以在其中选择一个类别或更具体地说是它的子类别。然后其适当的描述将显示在第二个片段上。

我正在使用可扩展view的类别列表。但是我在运行时遇到了错误。

系统服务在 onCreate() 之前对活动不可用

以下是我的应用程序中使用的 xml 和代码

activity_main_base.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:gravity="top"
    android:orientation="horizontal"
    tools:context=".MainBase" >
   <LinearLayout
       style="@style/FullscreenActionBarStyle"
        android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="50dp">

   </LinearLayout>
    <LinearLayout
        android:id="@+id/categoryfragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
   </LinearLayout>

MainBase.java

package my.newapp.x;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


public class MainBase extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_base);
        Fragment f=new Jobcat();
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.categoryfragment,f);
        ft.commit();

    }
}

catlayout.xml

<?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"
    android:orientation="vertical" 
    android:scrollbars="vertical">

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:childIndicatorRight="30dp"
        >        
    </ExpandableListView>

</LinearLayout>

作业猫.java

package my.newapp.x;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Jobcat extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.catlayout,container,false);
        return v;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        try{
         categoryfill j =new categoryfill();
         j.fillme();

        }
        catch(Exception e){
            System.out.println("Errrr +++ " + e.getMessage());
        }
    }

}

categoryfill.java

package my.newapp.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class Jobcategoryfill extends ExpandableListActivity {

    private SimpleExpandableListAdapter listadapter;
    ExpandableListView expand;

    public  void fillme()
    {
      List<Map<String,String>> category=new ArrayList<Map<String,String>>();
      List<List<Map<String, String>>> subcategory=new ArrayList<List<Map<String,String>>>();

      Map<String,String> cat1=new HashMap<String, String>();
      cat1.put("Parent","A");
      category.add(cat1);

      List<Map<String,String>> sublist1=new ArrayList<Map<String,String>>();

      Map<String,String> sub11=new HashMap<String, String>();
      sub11.put("child","a1");
      sublist1.add(sub11);
      Map<String,String> sub12=new HashMap<String, String>();
      sub12.put("child","a2");
      sublist1.add(sub12);
      Map<String,String> sub13=new HashMap<String, String>();
      sub13.put("child","a3");
      sublist1.add(sub13);
      Map<String,String> sub14=new HashMap<String, String>();
      sub14.put("child","a4");
      sublist1.add(sub14);
              sublist1.add(sub115);
              subcategory.add(sublist1);


      listadapter=new SimpleExpandableListAdapter(this, category, R.layout.categoryparent, new String[] { "Parent" },new int[]{R.id.parentview}, 
                    subcategory, R.layout.categorychild, new String[]{"child"},new int[]{R.id.childview});
      setListAdapter(listadapter);
    }


}

我无法找到解决方案。请任何人帮助我解决这个问题或建议我另一种方法来实现这个概念。

4

1 回答 1

0

下面这行应该可以很好地解释这个问题。

System service not available to activities before onCreate()

您应该使用该方法onCreate()而不是onCreateView().

于 2013-03-28T07:11:23.013 回答