Android KeyboardView 抛出空指针
我想制作自己的键盘。
我有以下代码
当我运行代码时,它抛出 NullPointerException。
我的 RKeyboardView
扩展了 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
}
}
我的主要活动
MobileActivity.java
public class MobileActivity extends ListActivity {
private RKeyboardView r;
/** Called when the activity is first created. */
@Override
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);
}
}
我应该做什么?请帮我。
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. */
@Override
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将这一行
移至紧邻的位置
findViewById() 方法在活动中的视图树(视图层次结构)中搜索具有指定 id 的视图,并返回该 View 对象。如果在调用 findViewById() 之前没有调用 setContentView,则特定的 View 对象既没有被创建也没有添加到 View 层次结构中。
Move the line
to directly after
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.