我在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 :(得分:0)
以下这行应该可以解释这个问题。
System service not available to activities before onCreate()
您应该使用方法onCreate()
代替onCreateView()
。