- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
现在我知道这个问题已经在 SO 上被问过好几次了,但没有一个答案对我有用。
我正在尝试为我的项目创建自定义首选项。更具体地说,它是对 HorizontalListView
的偏好。直接附在它下面。我基本上是通过修改 this code 创建的对于 SeekBarPreference
(我也在使用并且工作正常)。我的 ListViewPreference
与 SeelkBarPreference
位于完全相同的文件夹中(正如我所说的那样没有问题),但我不断收到 ClassNotFoundException
(请参阅下面的 logcat)。这是我的 ListViewPreference
类:
package com.example.ebookreader;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ListViewPreference extends Preference {
private final String TAG = getClass().getName();
private static final String ROBOBUNNYNS = "http://robobunny.com";
private static final int DEFAULT_VALUE = 50;
private int mCurrentValue;
private String mUnitsLeft = "";
private String mUnitsRight = "";
private HorizontalListView mListView;
private TextView mStatusText;
public ListViewPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initPreference(context, attrs);
}
public ListViewPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPreference(context, attrs);
}
private void initPreference(Context context, AttributeSet attrs) {
setValuesFromXml(attrs);
mListView = new HorizontalListView(context, attrs);
LayoutParams params = mListView.getLayoutParams();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.WRAP_CONTENT;
mListView.setLayoutParams(params);
}
private void setValuesFromXml(AttributeSet attrs) {
mUnitsLeft = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsLeft",
"");
String units = getAttributeStringValue(attrs, ROBOBUNNYNS, "units", "");
mUnitsRight = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsRight",
units);
}
private String getAttributeStringValue(AttributeSet attrs,
String namespace, String name, String defaultValue) {
String value = attrs.getAttributeValue(namespace, name);
if (value == null)
value = defaultValue;
return value;
}
@Override
protected View onCreateView(ViewGroup parent) {
RelativeLayout layout = null;
try {
LayoutInflater mInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (RelativeLayout) mInflater.inflate(
R.layout.horizontal_list_view_preference, parent, false);
} catch (Exception e) {
Log.e(TAG, "Error creating seek bar preference", e);
}
return layout;
}
@Override
public void onBindView(View view) {
super.onBindView(view);
try {
// move our seekbar to the new view we've been given
ViewParent oldContainer = mListView.getParent();
ViewGroup newContainer = (ViewGroup) view
.findViewById(R.id.listViewPrefBarContainer);
if (oldContainer != newContainer) {
// remove the seekbar from the old view
if (oldContainer != null) {
((ViewGroup) oldContainer).removeView(mListView);
}
// remove the existing seekbar (there may not be one) and add
// ours
newContainer.removeAllViews();
newContainer.addView(mListView,
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
} catch (Exception ex) {
Log.e(TAG, "Error binding view: " + ex.toString());
}
updateView(view);
}
/**
* Update a SeekBarPreference view with our current state
*
* @param view
*/
protected void updateView(View view) {
try {
RelativeLayout layout = (RelativeLayout) view;
mStatusText = (TextView) layout
.findViewById(R.id.listViewPrefValue);
mStatusText.setText(String.valueOf(mCurrentValue));
mStatusText.setMinimumWidth(30);
TextView unitsRight = (TextView) layout
.findViewById(R.id.listViewPrefUnitsRight);
unitsRight.setText(mUnitsRight);
TextView unitsLeft = (TextView) layout
.findViewById(R.id.listViewPrefUnitsLeft);
unitsLeft.setText(mUnitsLeft);
} catch (Exception e) {
Log.e(TAG, "Error updating seek bar preference", e);
}
}
@Override
protected Object onGetDefaultValue(TypedArray ta, int index) {
int defaultValue = ta.getInt(index, DEFAULT_VALUE);
return defaultValue;
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if (restoreValue) {
mCurrentValue = getPersistedInt(mCurrentValue);
} else {
int temp = 0;
try {
temp = (Integer) defaultValue;
} catch (Exception ex) {
Log.e(TAG, "Invalid default value: " + defaultValue.toString());
}
persistInt(temp);
mCurrentValue = temp;
}
}
}
对于大多数人来说,这个问题是由于没有带有参数 Context
和 AttributeSet
的构造函数造成的,但正如您所见,我显然有。
这是错误不断发生的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.ebookreader"
android:background="@drawable/wallpaper" >
<android:PreferenceCategory
android:key="device_settings"
android:title="Device Settings" >
<android:CheckBoxPreference
android:key="wifi"
android:summary="Enable or Disable Wi-Fi"
android:title="Wi-Fi" />
<android:CheckBoxPreference
android:key="bluetooth"
android:summary="Enable or Disable Bluetooth"
android:title="Bluetooth" />
<android:CheckBoxPreference
android:key="autosync"
android:summary="Enable or Disable AutoSync"
android:title="AutoSync" />
<custom:SeekBarPreference
android:id="@+id/brightness_adjust"
android:defaultValue="100"
android:key="brightness"
android:max="200"
android:summary="Adjust Brightness Levels"
android:title="Brightness" />
</android:PreferenceCategory>
<android:PreferenceCategory
android:key="account_settings"
android:title="Account Settings" >
<custom:ListViewPreference> <!-- Error happens here -->
android:id="@+id/font_selector"
android:key="font"
android:title="Font"
android:summary="Edit Font"
/>
</custom:ListViewPreference>
</android:PreferenceCategory>
</PreferenceScreen>
下面是我的完整 Logcat:
03-14 17:53:14.290: E/AndroidRuntime(449): FATAL EXCEPTION: main
03-14 17:53:14.290: E/AndroidRuntime(449): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ebookreader/com.example.ebookreader.SettingsActivity}: android.view.InflateException: Binary XML file line #40: Error inflating class ListViewPreference
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:126)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:3997)
03-14 17:53:14.290: E/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 17:53:14.290: E/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:491)
03-14 17:53:14.290: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-14 17:53:14.290: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-14 17:53:14.290: E/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method)
03-14 17:53:14.290: E/AndroidRuntime(449): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class ListViewPreference
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1333)
03-14 17:53:14.290: E/AndroidRuntime(449): at com.example.ebookreader.SettingsActivity.onCreate(SettingsActivity.java:31)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
03-14 17:53:14.290: E/AndroidRuntime(449): ... 11 more
03-14 17:53:14.290: E/AndroidRuntime(449): Caused by: java.lang.ClassNotFoundException: android.preference.ListViewPreference in loader dalvik.system.PathClassLoader[/data/app/com.example.ebookreader-2.apk]
03-14 17:53:14.290: E/AndroidRuntime(449): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
03-14 17:53:14.290: E/AndroidRuntime(449): at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
03-14 17:53:14.290: E/AndroidRuntime(449): at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.createItem(GenericInflater.java:375)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
03-14 17:53:14.290: E/AndroidRuntime(449): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
03-14 17:53:14.290: E/AndroidRuntime(449): ... 20 more
如有任何帮助,我们将不胜感激。
最佳答案
但是你为什么写
<custom:ListViewPreference
应该是
<com.example.ebookreader.ListViewPreference
作为你的包,放置类的地方,叫做
关于android - 使用 java.lang.ClassNotFoundException 扩展布局时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15417588/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!