Java 充气器上的充气异常。充气()方法调用
在我的Java 充气器上的充气异常。充气()方法调用,java,android,android-fragments,layout-inflater,navigation-drawer,Java,Android,Android Fragments,Layout Inflater,Navigation Drawer,在我的MenuAdapter类中尝试膨胀视图时,我不断收到膨胀异常。我在try-catch块中包围了这段麻烦的代码,并得到了错误消息: 链接到整个项目: package com.example.sidemenututorial; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import an
MenuAdapter
类中尝试膨胀视图时,我不断收到膨胀异常。我在try-catch块中包围了这段麻烦的代码,并得到了错误消息:
链接到整个项目:
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>
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);
}
}
}
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
二进制XML文件行#1:膨胀类时出错
代码如下:
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>
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);
}
}
}
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
使用取景器
将getView更改为
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;
}
同时将下面的代码移动到构造函数。将其声明为类成员LayoutInflater充气机
在构造函数中
inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
编辑:
在drawer\u list\u item.xml中
改变
android:background="?android:color/darker_gray"
到
当你的类膨胀出错时,你必须首先在XML中寻找问题所在
在您的情况下,问题在于您的抽屉列表项目布局。
在您的linearLayout根目录中,出现“?”字符的原因是android:background=“?android:color/dark\u gray”行。第73行是什么MenuListAdapter.java
在查看代码之后,我发现问题不在代码中,而是在后台属性的xml In drawer\u list\u项中。你得到了resoruce not found exception,通货膨胀失败了。这就是它应该是什么样的android:background=“@android:color/dark\u gray”
,它对我很有用。这是我的模拟器在尝试后的快照。代码建议绝对有效。纠正建议的错误,它应该有效。如果有帮助,不要忘记接受答案。如果你遇到困难,请告诉我,我的项目或IDE一定有问题。您采取了哪些步骤来设置项目以使其正确运行?我将此代码准确复制到MenuListAdapter类中,它落在这一行:convertView=inflater.inflate(R.layout.drawer\u list\u item,parent,false);顺便说一句,同样的异常也被抛出;看起来这在当时并没有什么不同all@kformeck您是否在构造函数中初始化了充气机。这确实有区别。请查看张贴在帖子中的链接。看来你还没有读过这篇文章full@kformeck顺便说一句,如果你做了建议的编辑,你的代码应该可以正常工作,没有错误。eah,我创建了一个新的实例字段:LayoutInflater inflater,然后调用inflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u inflater\u SERVICE);在设置了上下文、String[]和int[]对象之后,它是构造函数中的最后一行代码。充气机在过度跌落时不会失效,你们是我的蝙蝠侠!你刚刚救了我一天!非常感谢你。