gpt4 book ai didi

android - 使用 java.lang.ClassNotFoundException 扩展布局时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:19 31 4
gpt4 key购买 nike

现在我知道这个问题已经在 SO 上被问过好几次了,但没有一个答案对我有用。

我正在尝试为我的项目创建自定义首选项。更具体地说,它是对 HorizontalListView 的偏好。直接附在它下面。我基本上是通过修改 this code 创建的对于 SeekBarPreference(我也在使用并且工作正常)。我的 ListViewPreferenceSeelkBarPreference 位于完全相同的文件夹中(正如我所说的那样没有问题),但我不断收到 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;
}
}
}

对于大多数人来说,这个问题是由于没有带有参数 ContextAttributeSet 的构造函数造成的,但正如您所见,我显然有。

这是错误不断发生的 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com