Android KeyboardView 抛出空指针

发布于 2024-12-06 02:31:21 字数 1030 浏览 0 评论 0原文

我想制作自己的键盘。

我有以下代码

当我运行代码时,它抛出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-12-13 02:31:21

将这一行

 setContentView(R.layout.main);

移至紧邻的位置

super.onCreate(savedInstanceState);

findViewById() 方法在活动中的视图树(视图层次结构)中搜索具有指定 id 的视图,并返回该 View 对象。如果在调用 findViewById() 之前没有调用 setContentView,则特定的 View 对象既没有被创建也没有添加到 View 层次结构中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文