Android KeyboardView throws Nullpointer
I want to make my own keyboard.
I have the following code
When I ran the code, it throws NullPointerException.
My RKeyboardView
that extends KeyboardView
package com.glenn.droid; // MY package
import android.content.Context;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;
public class RKeyboardView extends KeyboardView {
public RKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
My main activity
MobileActivity.java
public class MobileActivity extends ListActivity {
private RKeyboardView r;
/** Called when the activity is first created. */
@Overrid开发者_开发问答e
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
r = (RKeyboardView) findViewById(R.id.rKeyboardView1);
r.setKeyboard(new Keyboard(this,R.xml.qwerty));
setContentView(R.layout.main);
}
}
What should I do? Please help me.
Move the line
setContentView(R.layout.main);
to directly after
super.onCreate(savedInstanceState);
The findViewById() method searches the tree of Views (the View hierarchy) in your activity for one with the specified id, and returns that View object. If you have not called setContentView before you call findViewById(), then the particular View object has neither been created nor added to the View hierarchy.
精彩评论