0

Android でアプリを開発中に問題が発生しました。多くの検索を行った後、解決策が見つかりませんでした。

2 つのフラグメント パネルを持つアプリ。1 つ目は、Fragmentユーザーがカテゴリまたはより具体的にはそのサブカテゴリを選択できるカテゴリとサブカテゴリのリストを表示します。次に、適切な説明が 2 番目のフラグメントに表示されます。

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>

jobcat.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());
        }
    }

}

カテゴリフィル.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 に答える