我一直在试图夸大我的 MenuAdapter
类视图得到一个InflateException。我已经包围了麻烦code在try-catch块,并收到错误消息:
链接整个项目:
https://docs.google.com/file/d/ 0B2Iwl4UysxOMa3E5a1l2SHZwOG8 /编辑?USP =共享
二进制XML文件行#1:错误充气类
这里是code:
包com.example.sidemenututorial;进口android.content.Context;进口android.view.LayoutInflater;进口android.view.View;进口android.view.ViewGroup;进口android.widget.BaseAdapter;进口android.widget.ImageView;进口android.widget.TextView;公共类MenuListAdapter延伸BaseAdapter { //字段----------------------------------------------- ------------------ 私人上下文的背景下; 私有String []标题; 私有String []字幕; 私人诠释[]图标; 私人LayoutInflater吹气; //构造----------------------------------------------- ------------- 公共MenuListAdapter( 上下文的背景下, 的String []冠军, 的String []字幕, INT []图标){ this.context =背景; this.titles =职称; this.subtitles =字幕; this.icons =图标; 吹气=(LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE); } //访问器----------------------------------------------- --------------- @覆盖 公众诠释的getCount(){ 返回titles.length; } @覆盖 公共对象的getItem(INT位置){ 回到标题[位置] } @覆盖 众长getItemId(INT位置){ 返回的位置; } // 方法 - - - - - - - - - - - - - - - - - - - - - - - - ----------------- 公共查看getView(INT位置,查看convertView,父母的ViewGroup){ ViewHolder viewHolder; //只有膨胀的观点,如果convertView为null 如果(convertView == NULL){ viewHolder =新ViewHolder(); convertView = inflater.inflate( R.layout.drawer_list_item,父母,假); viewHolder.txtTitle =(TextView中)convertView.findViewById( R.id.title); viewHolder.txtSubtitle =(TextView中)convertView.findViewById( R.id.subtitle); viewHolder.imgIcon =(ImageView的)convertView.findViewById( R.id.icon); //这是第一次这种观点已经被夸大, //这样的观点持有者存储在其标签领域 convertView.setTag(viewHolder); }其他{ viewHolder =(ViewHolder)convertView.getTag(); } //根据需要设置的意见领域 viewHolder.txtTitle.setText(标题[位置]); viewHolder.txtSubtitle.setText(字幕[位置]); viewHolder.imgIcon.setImageResource(图标[位置]); 返回convertView; } //类----------------------------------------------- ----------------- 静态类ViewHolder { TextView的txtTitle; TextView的txtSubtitle; ImageView的imgIcon; }}这里是它试图膨胀时,drawer_list_item.xml的观点: <的LinearLayout 的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:背景=机器人:彩色/ darker_gray 机器人:方向=横向 风格=ATTR / DROPDOWNLIST preferredItemHeight?> < ImageView的 机器人:ID =@ + ID /图标 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =match_parent 机器人:adjustViewBounds =真/> <的LinearLayout 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =match_parent 机器人:重力=center_vertical |左 机器人:方向=垂直> <的TextView 机器人:ID =@ + ID /标题 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:ellipsize =结束 机器人:单线=真 风格= /&GTATTR / spinnerDropDownItemStyle? <的TextView 机器人:ID =@ + ID /副标题 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:ellipsize =结束 机器人:单线=真 机器人:textAppearance =?ATTR / textAppearanceSmall 风格= /&GTATTR / spinnerDropDownItemStyle? < / LinearLayout中> < / LinearLayout中>
![android View.inflate 空指针异常](https://p.xsw88.cn/allimgs/daicuo/20230907/6695.png)
这里是客户端code调用该菜单适配器:
包com.example.sidemenututorial; 进口android.content.res.Configuration; 进口android.os.Bundle; 进口android.support.v4.app.ActionBarDrawerToggle; 进口android.support.v4.app.Fragment; 进口android.support.v4.app.FragmentTransaction; 进口android.support.v4.view.GravityCompat; 进口android.support.v4.widget.DrawerLayout; 进口android.view.View; 进口android.widget.AdapterView; 进口android.widget.ListView; 进口com.actionbarsherlock.app.SherlockFragmentActivity; 进口com.actionbarsherlock.view.MenuItem; 公共类MainActivity扩展SherlockFragmentActivity { //字段----------------------------------------------- ------------------ 私人DrawerLayout drawerLayout; 私人的ListView drawerList; 私人ActionBarDrawerToggle drawerToggle; 私人MenuListAdapter menuAdapter; 私人诠释[]图标; 私人片段片段1; 私人片段fragment2; 私人片段fragment3; 私人CharSequence的drawerTitle; 私人CharSequence的称号; 私人最终的String []标题=新的String [] { 标题片段#1, 标题片段#2, 标题片段#3 }; 私人最终的String [] =字幕新的String [] { 字幕片段#1, 字幕片段#2, 字幕片段#3 }; //生命周期回调---------------------------------------------- ------ @覆盖 保护无效的onCreate(捆绑savedInstanceState){ //基地实行 super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main); //实例片段 片段1 =新片段1(); fragment2 =新Fragment2(); fragment3 =新Fragment3(); //这项活动获得冠军 标题= drawerTitle =的getTitle(); //从绘制资源文件夹的图标 图标= INT新[] { R.drawable.action_about, R.drawable.action_settings, R.drawable.collections_cloud }; //从XML文件中的抽屉布局,里面的ListView控件 drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout); drawerList =(ListView控件)findViewById(R.id.listview_drawer); //设置自定义阴影的覆盖的主要内容 //当抽屉打开 drawerLayout.setDrawerShadow( R.drawable.drawer_shadow,GravityCompat.START); //传递字符串数组的MenuListAdapter,设置抽屉 //名单适配器,并设置其点击监听器 menuAdapter =新MenuListAdapter( MainActivity.this,标题,副标题,图标); drawerList.setAdapter(menuAdapter); drawerList.setOnItemClickListener(新DrawerItemClickListener()); //启用操作栏中有向上导航 getSupportActionBar()setHomeButtonEnabled(真)。 getSupportActionBar()setDisplayHomeAsUpEnabled(真)。 //允许的操作键切换抽屉 drawerToggle =新ActionBarDrawerToggle( 这个, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close){ 公共无效onDrawerClosed(查看视图){ super.onDrawerClosed(视图); } 公共无效onDrawerOpened(查看视图){ 。getSupportActionBar()的setTitle(drawerTitle); super.onDrawerOpened(视图); } }; drawerLayout.setDrawerListener(drawerToggle); //如果这是第一次打开这个活动, //使用装载片段#1开始 如果(savedInstanceState == NULL){ 选择信息(0); } } // 方法 - - - - - - - - - - - - - - - - - - - - - - - - ----------------- @覆盖 公共布尔onOptionsItemSelected(菜单项项){ //如果用户有pressed操作栏图标 如果(item.getItemId()== android.R.id.home){ //如果抽屉打开,关闭它;反之亦然 如果(drawerLayout.isDrawerOpen(drawerList)){ drawerLayout.closeDrawer(drawerList); }其他{ drawerLayout.openDrawer(drawerList); } } //完成通过让超类做休息 返回super.onOptionsItemSelected(项目); } @覆盖 保护无效onPostCreate(捆绑savedInstanceState){ //调用超级实施和同步抽屉 super.onPostCreate(savedInstanceState); drawerToggle.syncState(); } @覆盖 公共无效onConfigurationChanged(配置NEWCONFIG){ //调用超上实行本次活动 //和抽屉切换对象 super.onConfigurationChanged(NEWCONFIG); drawerToggle.onConfigurationChanged(NEWCONFIG); } 私人无效选择信息(INT位置){ //创建一个新片段事务并启动它 FragmentTransaction fragTran = getSupportFragmentManager() .beginTransaction(); //找到选定的位置更换内容视图 用数字的选择的片段// 开关(位置){ 情况下0:{ fragTran.replace(R.id.content_frame,片段1); 打破; } 情况1:{ fragTran.replace(R.id.content_frame,fragment2); 打破; } 案例2:{ fragTran.replace(R.id.content_frame,fragment3); 打破; } } //提交事务,并关上抽屉 fragTran.commit(); drawerList.setItemChecked(位置,真正的); drawerLayout.closeDrawer(drawerList); } 公共无效的setTitle(CharSequence的标题){ //保存在标题中传递,并设置操作栏标题 this.title =称号; getSupportActionBar()的setTitle(职称)。 } //类----------------------------------------------- ----------------- 私有类DrawerItemClickListener 实现ListView.OnItemClickListener { @覆盖 公共无效onItemClick( 适配器视图<>父母, 视图来看, INT位置, 长ID){ //点击后,选择打开相应的片段 选择信息(位置); } } }
以及日志崩溃的:
09-26 16:45:43.081:D / AndroidRuntime(1121):关闭VM09-26 16:45:43.081:W / dalvikvm(1121):主题ID = 1:螺纹未捕获的异常(组= 0x409961f8)退出09-26 16:45:45.561:E / AndroidRuntime(1121年):致命异常:主要09-26 16:45:45.561:E / AndroidRuntime(1121年):显示java.lang.NullPointerException09-26 16:45:45.561:E / AndroidRuntime(1121年):在com.example.sidemenututorial.MenuListAdapter.getView(MenuListAdapter.java:73)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.AbsListView.obtainView(AbsListView.java:2033)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.ListView.makeAndAddView(ListView.java:1772)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.ListView.fillDown(ListView.java:672)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.ListView.fillFromTop(ListView.java:732)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.ListView.layoutChildren(ListView.java:1625)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.AbsListView.onLayout(AbsListView.java:1863)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.View.layout(View.java:11180)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewGroup.layout(ViewGroup.java:4203)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.View.layout(View.java:11180)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewGroup.layout(ViewGroup.java:4203)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.FrameLayout.onLayout(FrameLayout.java:431)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.View.layout(View.java:11180)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewGroup.layout(ViewGroup.java:4203)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.LinearLayout.onLayout(LinearLayout.java:1399)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.View.layout(View.java:11180)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewGroup.layout(ViewGroup.java:4203)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.widget.FrameLayout.onLayout(FrameLayout.java:431)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.View.layout(View.java:11180)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewGroup.layout(ViewGroup.java:4203)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.os.Handler.dispatchMessage(Handler.java:99)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.os.Looper.loop(Looper.java:137)09-26 16:45:45.561:E / AndroidRuntime(1121年):在android.app.ActivityThread.main(ActivityThread.java:4340)09-26 16:45:45.561:E / AndroidRuntime(1121年):在java.lang.reflect.Method.invokeNative(本机方法)09-26 16:45:45.561:E / AndroidRuntime(1121年):在java.lang.reflect.Method.invoke(Method.java:511)09-26 16:45:45.561:E / AndroidRuntime(1121年):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)09-26 16:45:45.561:E / AndroidRuntime(1121年):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)09-26 16:45:45.561:E / AndroidRuntime(1121年):在dalvik.system.NativeStart.main(本机方法)09-26 16:45:45.593:D / dalvikvm(1121):GC_CONCURRENT释放191K,免费3%14296K / 14599K,暂停12毫秒+ 8ms的
解决方案
使用一个ViewHolder
http://developer.android.com/training/improving-布局/平滑scrolling.html
您getView改为
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ ViewHolder VH; 如果(convertView == NULL) { VH =新ViewHolder(); convertView = inflater.inflate( R.layout.drawer_list_item,父母,假); vh.txtTitle =(TextView中)convertView.findViewById(R.id.title); vh.txtSubtitle =(TextView中)convertView.findViewById(R.id.subtitle); vh.imgIcon =(ImageView的)convertView.findViewById(R.id.icon); convertView.setTag(VH); }其他{ VH =(ViewHolder)convertView.getTag(); } vh.txtTitle.setText(标题[位置]); vh.txtSubtitle.setText(字幕[位置]); vh.imgIcon.setImageResource(图标[位置]); 返回convertView; }静态类ViewHolder{TextView的txtTitle,txtSubtitle;ImageView的imgIcon;}
另外移动下面的构造。这个声明为一个类成员 LayoutInflater吹气
在你的构造
=吹气(LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
编辑:
在 drawer_list_item.xml
修改
的android:背景=机器人:彩色/ darker_gray
到
的android:背景=@机器人:彩色/ darker_gray
I keep getting an InflateException upon trying to inflate a view in my MenuAdapter
class. I've surrounded the troublesome code in a try-catch block and get the error Message:
link to the entire project:
https://docs.google.com/file/d/0B2Iwl4UysxOMa3E5a1l2SHZwOG8/edit?usp=sharing
Binary XML file line #1: Error inflating class
Here is the code:
package com.example.sidemenututorial;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MenuListAdapter extends BaseAdapter {
// Fields -----------------------------------------------------------------
private Context context;
private String[] titles;
private String[] subtitles;
private int[] icons;
private LayoutInflater inflater;
// Constructor ------------------------------------------------------------
public MenuListAdapter(
Context context,
String[] titles,
String[] subtitles,
int[] icons){
this.context = context;
this.titles = titles;
this.subtitles = subtitles;
this.icons = icons;
inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
// Accessors --------------------------------------------------------------
@Override
public int getCount(){
return titles.length;
}
@Override
public Object getItem(int position){
return titles[position];
}
@Override
public long getItemId(int position){
return position;
}
// Methods ----------------------------------------------------------------
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
// Only inflate the view if convertView is null
if (convertView == null){
viewHolder = new ViewHolder();
convertView = inflater.inflate(
R.layout.drawer_list_item, parent, false);
viewHolder.txtTitle = (TextView)convertView.findViewById(
R.id.title);
viewHolder.txtSubtitle = (TextView)convertView.findViewById(
R.id.subtitle);
viewHolder.imgIcon = (ImageView)convertView.findViewById(
R.id.icon);
// This is the first time this view has been inflated,
// so store the view holder in its tag fields
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
// Set the views fields as needed
viewHolder.txtTitle.setText(titles[position]);
viewHolder.txtSubtitle.setText(subtitles[position]);
viewHolder.imgIcon.setImageResource(icons[position]);
return convertView;
}
// Classes ----------------------------------------------------------------
static class ViewHolder {
TextView txtTitle;
TextView txtSubtitle;
ImageView imgIcon;
}
}
And here is the view it is trying to inflate, the drawer_list_item.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:color/darker_gray"
android:orientation="horizontal"
style="?attr/dropdownListPreferredItemHeight" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
style="?attr/spinnerDropDownItemStyle"/>
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceSmall"
style="?attr/spinnerDropDownItemStyle"/>
</LinearLayout>
</LinearLayout>
And here is the client code that is calling the menu adapter:
package com.example.sidemenututorial;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
public class MainActivity extends SherlockFragmentActivity {
// Fields -----------------------------------------------------------------
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
private MenuListAdapter menuAdapter;
private int[] icons;
private Fragment fragment1;
private Fragment fragment2;
private Fragment fragment3;
private CharSequence drawerTitle;
private CharSequence title;
private final String[] titles = new String[]{
"Title Fragment #1",
"Title Fragment #2",
"Title Fragment #3"
};
private final String[] subtitles = new String[]{
"Subtitle Fragment #1",
"Subtitle Fragment #2",
"Subtitle Fragment #3"
};
// Lifecycle Callbacks ----------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
// Base implemenation
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the fragments
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
// Get the title from this activity
title = drawerTitle = getTitle();
// Get the icons from the drawables folder
icons = new int[]{
R.drawable.action_about,
R.drawable.action_settings,
R.drawable.collections_cloud
};
// Get the drawer layout from the XML file and the ListView inside it
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerList = (ListView)findViewById(R.id.listview_drawer);
// Set a custom shadow over that overlays the main content
// when the drawer opens
drawerLayout.setDrawerShadow(
R.drawable.drawer_shadow, GravityCompat.START);
// Pass the string arrays to the MenuListAdapter, set the drawer
// list adapter to it and set up its click listener
menuAdapter = new MenuListAdapter(
MainActivity.this, titles, subtitles, icons);
drawerList.setAdapter(menuAdapter);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
// Enable the action bar to have up navigation
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Allow the the action bar to toggle the drawer
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
}
public void onDrawerOpened(View view){
getSupportActionBar().setTitle(drawerTitle);
super.onDrawerOpened(view);
}
};
drawerLayout.setDrawerListener(drawerToggle);
// If this is the first time opening this activity,
// start with loading fragment #1
if (savedInstanceState == null){
selectItem(0);
}
}
// Methods ----------------------------------------------------------------
@Override
public boolean onOptionsItemSelected(MenuItem item){
// If the user has pressed the action bar icon
if (item.getItemId() == android.R.id.home){
// If the drawer is open, close it; vice versa
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
} else {
drawerLayout.openDrawer(drawerList);
}
}
// Finish by letting the super class do the rest
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
// Call the super implementation and synchronize the drawer
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
// Call the super implemenation on this activity
// and the drawer toggle object
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
private void selectItem(int position){
// Create a new fragment transaction and start it
FragmentTransaction fragTran = getSupportFragmentManager()
.beginTransaction();
// Locate the position selected replace the content view
// with the fragment of the number selected
switch (position){
case 0:{
fragTran.replace(R.id.content_frame, fragment1);
break;
}
case 1:{
fragTran.replace(R.id.content_frame, fragment2);
break;
}
case 2:{
fragTran.replace(R.id.content_frame, fragment3);
break;
}
}
// Commit the transaction and close the drawer
fragTran.commit();
drawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerList);
}
public void setTitle(CharSequence title){
// Save the passed in title and set the action bar title
this.title = title;
getSupportActionBar().setTitle(title);
}
// Classes ----------------------------------------------------------------
private class DrawerItemClickListener
implements ListView.OnItemClickListener{
@Override
public void onItemClick(
AdapterView<?> parent,
View view,
int position,
long id) {
// When clicked, select open the appropriate fragment
selectItem(position);
}
}
}
And a log of the crash:
09-26 16:45:43.081: D/AndroidRuntime(1121): Shutting down VM
09-26 16:45:43.081: W/dalvikvm(1121): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
09-26 16:45:45.561: E/AndroidRuntime(1121): FATAL EXCEPTION: main
09-26 16:45:45.561: E/AndroidRuntime(1121): java.lang.NullPointerException
09-26 16:45:45.561: E/AndroidRuntime(1121): at com.example.sidemenututorial.MenuListAdapter.getView(MenuListAdapter.java:73)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.ListView.makeAndAddView(ListView.java:1772)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.ListView.fillDown(ListView.java:672)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.ListView.fillFromTop(ListView.java:732)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.ListView.layoutChildren(ListView.java:1625)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.View.layout(View.java:11180)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewGroup.layout(ViewGroup.java:4203)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.View.layout(View.java:11180)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewGroup.layout(ViewGroup.java:4203)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.View.layout(View.java:11180)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewGroup.layout(ViewGroup.java:4203)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.View.layout(View.java:11180)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewGroup.layout(ViewGroup.java:4203)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.View.layout(View.java:11180)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewGroup.layout(ViewGroup.java:4203)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.os.Looper.loop(Looper.java:137)
09-26 16:45:45.561: E/AndroidRuntime(1121): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 16:45:45.561: E/AndroidRuntime(1121): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 16:45:45.561: E/AndroidRuntime(1121): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 16:45:45.561: E/AndroidRuntime(1121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 16:45:45.561: E/AndroidRuntime(1121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 16:45:45.561: E/AndroidRuntime(1121): at dalvik.system.NativeStart.main(Native Method)
09-26 16:45:45.593: D/dalvikvm(1121): GC_CONCURRENT freed 191K, 3% free 14296K/14599K, paused 12ms+8ms
解决方案
Use a ViewHolder
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Change your getView to
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder vh;
if(convertView==null)
{
vh = new ViewHolder();
convertView = inflater.inflate(
R.layout.drawer_list_item, parent, false);
vh.txtTitle = (TextView) convertView.findViewById(R.id.title);
vh.txtSubtitle = (TextView) convertView.findViewById(R.id.subtitle);
vh.imgIcon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.txtTitle.setText(titles[position]);
vh.txtSubtitle.setText(subtitles[position]);
vh.imgIcon.setImageResource(icons[position]);
return convertView;
}
static class ViewHolder
{
TextView txtTitle,txtSubtitle;
ImageView imgIcon;
}
Also move the below to constructor. Declare this as a class member LayoutInflater inflater
In your constructor
inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
Edit:
In drawer_list_item.xml
Change
android:background="?android:color/darker_gray"
to
android:background="@android:color/darker_gray"
相关推荐
最新文章