- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的 MenuAdapter
类中尝试膨胀 View 时,我不断收到 InflateException。我将麻烦的代码包围在 try-catch block 中并收到错误消息:
整个项目的链接:
https://docs.google.com/file/d/0B2Iwl4UysxOMa3E5a1l2SHZwOG8/edit?usp=sharing
Binary XML file line #1: Error inflating class
代码如下:
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
最佳答案
当你得到 Error inflating your class 时,你必须首先在你的 XML 中寻找问题。
在您的情况下,问题出在您的 drawer_list_item 布局中。在您的 linearLayout 根目录中,“?”字符导致 android:background="?android:color/darker_gray" 行。
关于java - inflater.inflate() 方法调用时出现 InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19034074/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!