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);
}
}
我無法找到解決方案。請有人幫我解決這個問題,或者建議我採用另一種方法來實現這個概念。
我如何創建oncreate創建視圖的片段instad – sam 2013-03-28 07:21:10
你可以發佈你的stacktrace?可能你是在MainActivity而不是你的Fragment中做到這一點的? – midhunhk 2013-03-28 07:25:05
但我需要在一個片段中顯示這個......然後只有它對於大屏幕尺寸會很有用 – sam 2013-03-28 07:32:53