我正在尝试使用HoloEverywhere类AutoCompleteTextView来处理我的应用。主要原因是我希望Holo主题适用于3.0之前的设备(特别是API级别8-10)这是我的主要活动代码:
package com.example.testautocomp;
import org.holoeverywhere.widget.AutoCompleteTextView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
private AutoCompleteTextView m_edit;
private static final String[] items = { "lorem", "ipsum", "dolor", "sit",
"amet", "consectetuer", "adipiscing", "elit", "morbi", "vel",
"augue", "purus", "sitter", "sitting", "sither", "sithar" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_edit = (AutoCompleteTextView)findViewById(R.id.edit2);
m_edit.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, items));
}
}
也是我的activity_main.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="Testing" />
</LinearLayout>
当我运行我的应用程序时,它进入eclipse的调试器,在按下resume(或F8)继续该线程后,我的应用程序崩溃/强制关闭,我的LogCat中出现以下错误:
04-19 17:52:56.342: W/dalvikvm(675): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-19 17:52:56.422: E/AndroidRuntime(675): FATAL EXCEPTION: main
04-19 17:52:56.422: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testautocomp/com.example.testautocomp.MainActivity}: java.lang.ClassCastException: android.widget.AutoCompleteTextView cannot be cast to org.holoeverywhere.widget.AutoCompleteTextView
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-19 17:52:56.422: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 17:52:56.422: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 17:52:56.422: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-19 17:52:56.422: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-19 17:52:56.422: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method)
04-19 17:52:56.422: E/AndroidRuntime(675): Caused by: java.lang.ClassCastException: android.widget.AutoCompleteTextView cannot be cast to org.holoeverywhere.widget.AutoCompleteTextView
04-19 17:52:56.422: E/AndroidRuntime(675): at com.example.testautocomp.MainActivity.onCreate(MainActivity.java:22)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.Activity.performCreate(Activity.java:4465)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-19 17:52:56.422: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-19 17:52:56.422: E/AndroidRuntime(675): ... 11 more
如何修复此CastClassException?
答案 0 :(得分:0)
不,你的行动不正确。您应该在org.holoeverywhere.app.Activity上更改活动基类。
答案 1 :(得分:-1)
非常类似于此question
通过将main_activity.xml更改为:
来解决此问题<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<org.holoeverywhere.widget.AutoCompleteTextView
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="Testing" />
</LinearLayout>
主要部分是 org.holoeverywhere.widget.AutoCompleteTextView