所以我创建了一个不使用片段的可扩展列表视图。在我的代码中,我无法找到错误的位置,因为它没有运行所以这是我的完整代码。
Main Activity class
package com.demo.demonavigatiodrawer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
ImageView home;
TextView appname;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fontPath = "fonts/Shadow Boxing.ttf";
setContentView(R.layout.activity_main);
home = (ImageView) findViewById(R.id.home);
home.setOnClickListener(homeOnclickListener);
appname = (TextView) findViewById(R.id.appname);
Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
appname.setTypeface(tf);
setUpDrawer();
}
/**
*
* Get the names and icons references to build the drawer menu...
*/
private void setUpDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(getResources().getColor(
android.R.color.transparent));
mDrawerLayout.setDrawerListener(mDrawerListener);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,
listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
mDrawerLayout.closeDrawer(expListView);
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
View.OnClickListener homeOnclickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(expListView)) {
mDrawerLayout.closeDrawer(expListView);
} else {
mDrawerLayout.openDrawer(expListView);
}
}
};
private OnItemClickListener mDrawerItemClickedListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
}
};
// Catch the events related to the drawer to arrange views according to this
// action if necessary...
private DrawerListener mDrawerListener = new DrawerListener() {
@Override
public void onDrawerStateChanged(int status) {
}
@Override
public void onDrawerSlide(View view, float slideArg) {
}
@Override
public void onDrawerOpened(View view) {
}
@Override
public void onDrawerClosed(View view) {
}
};
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Batsman");
listDataHeader.add("Bowler");
listDataHeader.add("All rounder");
listDataHeader.add("Wicket keeper");
// Adding child data
List<String> batsman = new ArrayList<String>();
batsman.add("V. Kohli");
batsman.add("G.J. Bailey");
batsman.add("H.M. Amla");
List<String> bowler = new ArrayList<String>();
bowler.add("D.W. Steyn");
bowler.add("J.M. Anderson");
bowler.add("M.G. Johnson");
List<String> all = new ArrayList<String>();
all.add("R.A. Jadeja");
all.add("Shakib Al Hasan");
all.add("D.J. Bravo");
List<String> wk = new ArrayList<String>();
wk.add("A.B. de Villiers");
wk.add("M.S. Dhoni");
wk.add("K.C. Sangakkara");
listDataChild.put(listDataHeader.get(0), batsman); // Header, Child data
listDataChild.put(listDataHeader.get(1), bowler);
listDataChild.put(listDataHeader.get(2), all);
listDataChild.put(listDataHeader.get(2), wk);
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context,
List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(
this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition,
childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(
this._listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
Layout
<?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:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DC3377"
android:orientation="horizontal" >
<ImageView
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/appname"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Cricket"
android:textColor="#FFFFFF"
android:textSize="26dp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<!-- Drawer Content -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"/>
<!-- The navigation menu -->
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#E6E6E6" >
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#F7819F">
<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#000000" />
</LinearLayout>
list_item.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="55dip"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
this is my logcat error
04-10 12:28:13.694: E/AndroidRuntime(6847): FATAL EXCEPTION: main
04-10 12:28:13.694: E/AndroidRuntime(6847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.demonavigatiodrawer/com.demo.demonavigatiodrawer.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.os.Looper.loop(Looper.java:137)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 12:28:13.694: E/AndroidRuntime(6847): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 12:28:13.694: E/AndroidRuntime(6847): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 12:28:13.694: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 12:28:13.694: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 12:28:13.694: E/AndroidRuntime(6847): at dalvik.system.NativeStart.main(Native Method)
04-10 12:28:13.694: E/AndroidRuntime(6847): Caused by: java.lang.RuntimeException: native typeface cannot be made
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.graphics.Typeface.<init>(Typeface.java:175)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.graphics.Typeface.createFromAsset(Typeface.java:149)
04-10 12:28:13.694: E/AndroidRuntime(6847): at com.demo.demonavigatiodrawer.MainActivity.onCreate(MainActivity.java:45)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.Activity.performCreate(Activity.java:5104)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 12:28:13.694: E/AndroidRuntime(6847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 12:28:13.694: E/AndroidRuntime(6847): ... 11 more
答案 0 :(得分:0)
是的,我发现我的错误是我自己所以只需删除这一行......
String fontPath =&#34; fonts / Shadow Boxing.ttf&#34 ;;
字体tf = Typeface.createFromAsset(this.getAssets(),fontPath); appname.setTypeface(TF);