2013-03-28 99 views
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> 

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

} 

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); 
    } 


} 

我无法找到解决方案。请有人帮我解决这个问题,或者建议我采用另一种方法来实现这个概念。

回答

0

下面这行应该说明问题。

System service not available to activities before onCreate()

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

+0

我如何创建oncreate创建视图的片段instad – sam 2013-03-28 07:21:10

+0

你可以发布你的stacktrace?可能你是在MainActivity而不是你的Fragment中做到这一点的? – midhunhk 2013-03-28 07:25:05

+0

但我需要在一个片段中显示这个......然后只有它对于大屏幕尺寸会很有用 – sam 2013-03-28 07:32:53