设备和AVD的不同行为

时间:2012-05-11 20:21:54

标签: keyboard nullpointerexception avd onclicklistener

目前我正在开发基于自定义键盘的应用。基本上我有一个布局和编辑文本和一个辅助xml与键盘布局。该应用程序应该启动,隐藏默认键盘并显示自定义键盘。

几天前,我完成了应用程序,我正在测试它与AVDs 2.1 2.2和2.3所有工作就像一个魅力!

之后我决定在实际设备上试用它但是自定义键盘没有用,所以我调试了它。在为键设置onclick侦听器时会发生此问题,它们都会抛出nullPointerException。

让我感到困惑的是,在AVD上它完美运行,也为AVD调试它,并且根本没有nullPointerException。

这是正常的吗?

以下代码:

public class Main extends Activity implements OnTouchListener, OnClickListener,
    OnFocusChangeListener {
private EditText mEt;
private Button mBSpace, mBack, mBorrar;
private RelativeLayout mLayout, mKLayout;
private boolean isEdit = true; 
private int w, mWindowWidth;
private String cL[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "Ñ", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
        "X", "Y", "Z", ".", "?",  "!"}; 
private Button mB[] = new Button[40];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.main);
        setKeys();  //get ids from xml and setOnClickListeners for every button.
        mEt = (EditText) findViewById(R.id.xEt);
        mEt.setOnTouchListener(this);
        mEt.setOnFocusChangeListener(this);
        mEt.setOnClickListener(this);
        mLayout = (RelativeLayout) findViewById(R.id.xK1);
        mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);
        hideDefaultKeyboard(); //abrir teclado al prender app
        enableKeyboard();
        changeCapitalLetters();
        changeCapitalTags();

    } catch (Exception e) {
        Log.w(getClass().getName(), e.toString());
    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (v == mEt) {
        hideDefaultKeyboard();
        enableKeyboard();
    }
    return true;
}

@Override
public void onClick(View v) {

    if (v != mBack && v != mBorrar) {

        addText(v);

    } else if (v != mBorrar  && v == mBack) {
        isBack(v);
    } else if (v != mBack && v == mBorrar) {
        isBorrar(v);
    }
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (v == mEt && hasFocus == true) {

        isEdit = true;

    } 

}

private void addText(View v) {
    if (isEdit == true) {
        String b = "";
        b = (String) v.getTag();
        if (b != null) {
            // adding text in Edittext
            mEt.append(b);

        }
    }

}

private void isBack(View v) {
    if (isEdit == true) {
        CharSequence cc = mEt.getText();
        if (cc != null && cc.length() > 0) {
            {
                mEt.setText("");
                mEt.append(cc.subSequence(0, cc.length() - 1));
            }

        }
    }

}
private void isBorrar(View v) {
    if (isEdit == true) {
        CharSequence cc = mEt.getText();
        if (cc != null && cc.length() > 0) {
            {
                mEt.setText("");
            }

        }
    }

}
private void changeCapitalLetters() {
    for (int i = 0; i < cL.length; i++)
        mB[i].setText(cL[i]);
}

private void changeCapitalTags() {
    for (int i = 0; i < cL.length; i++)
        mB[i].setTag(cL[i]);
}

// enabling customized keyboard
private void enableKeyboard() {
    mLayout.setVisibility(RelativeLayout.VISIBLE);
    mKLayout.setVisibility(RelativeLayout.VISIBLE);

}

// Disable customized keyboard
private void disableKeyboard() {
    mLayout.setVisibility(RelativeLayout.INVISIBLE);
    mKLayout.setVisibility(RelativeLayout.INVISIBLE);

}

private void hideDefaultKeyboard() {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mEt.getWindowToken(), 0);
    //imm.hideSoftInputFromWindow(mKLayout.getApplicationWindowToken(), 0);

}

//get ids del xml y setOnClickListeners
private void setKeys() {
    mWindowWidth = getWindowManager().getDefaultDisplay().getWidth();
    mB[0] = (Button) findViewById(R.id.xCero);
    mB[1] = (Button) findViewById(R.id.xUno);
    mB[2] = (Button) findViewById(R.id.xDos);
    mB[3] = (Button) findViewById(R.id.xTres);
    mB[4] = (Button) findViewById(R.id.xCuatro);
    mB[5] = (Button) findViewById(R.id.xCinco);
    mB[6] = (Button) findViewById(R.id.xSeis);
    mB[7] = (Button) findViewById(R.id.xSiete);
    mB[8] = (Button) findViewById(R.id.xOcho);
    mB[9] = (Button) findViewById(R.id.xNueve);
    mB[10] = (Button) findViewById(R.id.xA);
    mB[11] = (Button) findViewById(R.id.xB);
    mB[12] = (Button) findViewById(R.id.xC);
    mB[13] = (Button) findViewById(R.id.xD);
    mB[14] = (Button) findViewById(R.id.xE);
    mB[15] = (Button) findViewById(R.id.xF);
    mB[16] = (Button) findViewById(R.id.xG);
    mB[17] = (Button) findViewById(R.id.xH);
    mB[18] = (Button) findViewById(R.id.xI);
    mB[19] = (Button) findViewById(R.id.xJ);
    mB[20] = (Button) findViewById(R.id.xK);
    mB[21] = (Button) findViewById(R.id.xL);
    mB[22] = (Button) findViewById(R.id.xM);
    mB[23] = (Button) findViewById(R.id.xN);
    mB[24] = (Button) findViewById(R.id.xENIE);
    mB[25] = (Button) findViewById(R.id.xO);
    mB[26] = (Button) findViewById(R.id.xP);
    mB[27] = (Button) findViewById(R.id.xQ);
    mB[28] = (Button) findViewById(R.id.xR);
    mB[29] = (Button) findViewById(R.id.xS);
    mB[30] = (Button) findViewById(R.id.xT);
    mB[31] = (Button) findViewById(R.id.xU);
    mB[32] = (Button) findViewById(R.id.xV);
    mB[33] = (Button) findViewById(R.id.xW);
    mB[34] = (Button) findViewById(R.id.xX);
    mB[35] = (Button) findViewById(R.id.xY);
    mB[36] = (Button) findViewById(R.id.xZ);
    mB[37] = (Button) findViewById(R.id.xPUNTO);
    mB[38] = (Button) findViewById(R.id.xPREGUNTA);
    mB[39] = (Button) findViewById(R.id.xEXCLAMACION);
    mBorrar = (Button) findViewById(R.id.xBorrar);
    mBSpace = (Button) findViewById(R.id.xSpace);
    mBack = (Button) findViewById(R.id.xBack);
    for (int i = 0; i < mB.length; i++)
        mB[i].setOnClickListener(this);
    mBorrar.setOnClickListener(this);
    mBSpace.setOnClickListener(this);
    mBack.setOnClickListener(this);


}

}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/xMLayout"
android:background="#000000" android:layout_height="fill_parent"
android:focusable="true"><!-- android:orientation="vertical"  -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/xsubLayout"
    android:keepScreenOn="true"
    android:layout_height="fill_parent"><!-- android:orientation="vertical"  -->
    <EditText android:id="@+id/xEt" android:layout_width="fill_parent"
        android:focusableInTouchMode="true" android:layout_height="wrap_content" />
    <!--<EditText android:id="@+id/et1" android:layout_width="fill_parent"
        android:layout_below="@+id/xEt" android:layout_height="wrap_content" />-->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/xK1"
    android:layout_height="wrap_content"
    android:visibility="gone"> <!-- android:orientation="vertical"  -->
    <include android:id="@+id/xKeyBoard" layout="@layout/keyboard"></include>
</RelativeLayout>

键盘布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hebrwKeyboardView" android:layout_width="fill_parent"
android:layout_alignParentBottom="true" android:layout_below="@+id/xsubLayout"
android:orientation="vertical" android:background="#252625"
android:visibility="visible" android:layout_height="225sp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="225sp"
    android:orientation="vertical" android:layout_alignParentBottom="true"
    android:clipChildren="true">
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="225sp"
        android:padding="0sp">
        <TableRow android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="0sp">
            <LinearLayout android:baselineAligned="true"
                android:layout_width="fill_parent" android:layout_height="45sp"
                android:fitsSystemWindows="true">
                <Button android:soundEffectsEnabled="true" android:id="@+id/xCero"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="0" android:textColor="#000" android:tag="0"
                    android:padding="0sp" android:textStyle="bold" android:layout_gravity="center"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xUno"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:padding="0sp" android:textColor="#000" android:tag="1"
                    android:text="1" android:textStyle="bold" android:layout_gravity="center" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xDos"
                    android:layout_gravity="center" android:layout_width="32sp"
                    android:padding="0sp" android:layout_height="fill_parent"
                    android:text="2" android:tag="2" android:textStyle="bold"
                    android:textColor="#000" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xTres"
                    android:layout_width="32sp" android:layout_gravity="center"
                    android:layout_height="fill_parent" android:text="3" android:tag="3"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" /><!--  android:ellipsize="marquee" /> -->
                <Button android:soundEffectsEnabled="true" android:id="@+id/xCuatro"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:layout_gravity="center_horizontal" android:text="4"
                    android:tag="4" android:fitsSystemWindows="true"
                    android:textColor="#000" android:textStyle="bold"
                    android:ellipsize="marquee" android:padding="0sp"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xCinco"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:tag="5" android:layout_gravity="center" android:text="5"
                    android:fitsSystemWindows="true" android:textColor="#000" android:padding="0sp"
                    android:textStyle="bold" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xSeis"
                    android:layout_width="32sp" android:layout_gravity="center_horizontal"
                    android:layout_height="fill_parent" android:text="6" android:tag="6"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xSiete"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="7" android:fitsSystemWindows="true" android:tag="7"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xOcho"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="8" android:fitsSystemWindows="true" android:tag="8"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xNueve"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:textColor="#000" android:textStyle="bold" android:text="9"
                    android:fitsSystemWindows="true" android:tag="9" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
            </LinearLayout>
        </TableRow>
        <TableRow android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="0sp">
            <LinearLayout android:baselineAligned="true"
                android:layout_width="fill_parent" android:layout_height="45sp"
                android:fitsSystemWindows="true">
                <Button android:soundEffectsEnabled="true" android:id="@+id/xA"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="A" android:textColor="#000" android:tag="A"
                    android:padding="0sp" android:textStyle="bold" android:layout_gravity="center"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xB"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:padding="0sp" android:textColor="#000" android:tag="B"
                    android:text="B" android:textStyle="bold" android:layout_gravity="center" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xC"
                    android:layout_gravity="center" android:layout_width="32sp"
                    android:padding="0sp" android:layout_height="fill_parent"
                    android:text="C" android:tag="C" android:textStyle="bold"
                    android:textColor="#000" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xD"
                    android:layout_width="32sp" android:layout_gravity="center"
                    android:layout_height="fill_parent" android:text="D" android:tag="D"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" /><!--  android:ellipsize="marquee" /> -->
                <Button android:soundEffectsEnabled="true" android:id="@+id/xE"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:layout_gravity="center_horizontal" android:text="E"
                    android:tag="E" android:fitsSystemWindows="true"
                    android:textColor="#000" android:textStyle="bold"
                    android:ellipsize="marquee" android:padding="0sp"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xF"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:tag="F" android:layout_gravity="center" android:text="F"
                    android:fitsSystemWindows="true" android:textColor="#000" android:padding="0sp"
                    android:textStyle="bold" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xG"
                    android:layout_width="32sp" android:layout_gravity="center_horizontal"
                    android:layout_height="fill_parent" android:text="G" android:tag="G"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xH"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="H" android:fitsSystemWindows="true" android:tag="H"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xI"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="I" android:fitsSystemWindows="true" android:tag="I"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xJ"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:textColor="#000" android:textStyle="bold" android:text="J"
                    android:fitsSystemWindows="true" android:tag="J" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
            </LinearLayout>
        </TableRow>
        ...
...
        <TableRow android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:fitsSystemWindows="true"
            android:orientation="horizontal">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="45sp" android:gravity="bottom"
                android:orientation="horizontal">
                <Button android:soundEffectsEnabled="true" android:id="@+id/xBorrar"
                    android:textColor="#000" android:textStyle="bold"
                    android:layout_width="115sp" android:layout_height="fill_parent"
                    android:tag="borrarTodo" android:text="BORRA_TODO" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xSpace"
                    android:textColor="#000" android:textStyle="bold"
                    android:layout_width="135sp" android:layout_height="fill_parent"
                    android:tag=" " android:text="|___ESPACIO___|" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xBack"
                    android:layout_width="70sp" android:layout_height="fill_parent"
                    android:textColor="#000" android:textStyle="bold" android:tag="back"
                    android:layout_gravity="center_horizontal|center_vertical|center"
                    android:fitsSystemWindows="true" android:text="BORRA" />
            </LinearLayout>
        </TableRow>
    </TableLayout>
</TableLayout>

该应用基于我从以下网站下载的示例: http://tutorials-android.blogspot.com/2011/06/create-your-own-custom-keyboard-for.html

任何帮助都会很棒,我不知道现在还有什么可以尝试......

1 个答案:

答案 0 :(得分:1)

我发现了问题所在。我使用的AVD有一个640x480的屏幕,并使用标准布局xml。我用来测试应用程序的物理设备是一个屏幕为800x600的平板电脑,因此针对布局大的xml而没有没有所有按钮。