How to inflate a LinearLayout ( for android keyboard )?
I'm trying to create an android keyboard, right now I want it just to inflate, i.e. show, linearlayout from main.xml
Here's the java file
package keyboard.rob.com;
import ...
public class zyz extends InputMethodService
impleme开发者_Go百科nts KeyboardView.OnKeyboardActionListener {
private KeyboardView mInputView;
@Override public void onCreate() {
super.onCreate();
}
@Override public View onCreateInputView() {
mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.main, null);
return mInputView;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="center" android:id="@+id/LL_whole" android:orientation="vertical" android:clickable="false">
<LinearLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@layout/backrep" android:layout_gravity="center" android:id="@+id/LL_total" android:orientation="vertical" android:layout_height="wrap_content">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:background="@drawable/spell_frame" android:layout_marginBottom="10px" android:layout_marginTop="5px" android:layout_gravity="center" android:id="@+id/LL_spell" android:paddingLeft="10px">
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_width="wrap_content" android:id="@+id/LLrow1">
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/LLrow2"></LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/LLrow3"></LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_width="wrap_content" android:id="@+id/LLrow4">
</LinearLayout>
</LinearLayout>
</LinearLayout>
and the error it crashes with:
08-29 20:21:28.128: ERROR/AndroidRuntime(10227): java.lang.ClassCastException: android.widget.LinearLayout
Any ideas? Thanks!
try below code..
package keyboard.rob.com;
import ...
public class TotalKeyboard extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
private LinearLayout mInputView;
@Override public void onCreate() {
super.onCreate();
}
@Override public View onCreateInputView() {
mInputView = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
return mInputView;
}
}
You're casting your layout, which basically is a LinearLayout
, to a KeyboardView
. This gives you a classcast exception. Since onCreateInputView()
returns a View
, it should suffice to remove the cast. LayoutInflater.inflate()
already returns a View
. No need to cast it to anything. (LinearLayout
extends the View
class, so it's valid in this case)
So try changing
mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.main, null);
to
mInputView = getLayoutInflater().inflate(R.layout.main, null);
Alternatively you can cast it to a LinearLayout
, but thats unneccesary here.
精彩评论