Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Keyboard 设备和AVD上的不同行为_Keyboard_Nullpointerexception_Avd_Onclicklistener - Fatal编程技术网

Keyboard 设备和AVD上的不同行为

Keyboard 设备和AVD上的不同行为,keyboard,nullpointerexception,avd,onclicklistener,Keyboard,Nullpointerexception,Avd,Onclicklistener,目前我正在开发一个基于自定义键盘的应用程序。基本上,我有一个带有编辑文本的布局和一个带有键盘布局的辅助xml。应用程序应该启动,隐藏默认键盘并显示自定义键盘 几天前,我完成了这个应用程序,当时我正在用AVDs测试它,2.1、2.2和2.3都运行得很好 之后,我决定在实际设备上试用,但自定义键盘不起作用,所以我调试了它。问题发生在为键设置onclick侦听器时,它们都抛出nullPointerException 真正让我困惑的是,它在AVD上工作得非常完美,也为AVD进行了调试,完全没有nullP

目前我正在开发一个基于自定义键盘的应用程序。基本上,我有一个带有编辑文本的布局和一个带有键盘布局的辅助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);


}

}
公共类主扩展活动实现OnTouchListener、OnClickListener、, OnFocusChangeListener{ 私人编辑会议; 专用按钮mBSpace、mBack、mBorrar; 私人关系布局,mKLayout; 私有布尔isEdit=true; 西、中、西私人酒店; 私有字符串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”、“X”、“Y”、“Z”、“X”、“Y”、“Z”!”}; 私有按钮mB[]=新按钮[40]; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 试一试{ setContentView(R.layout.main); setKeys();//从xml和setOnClickListeners获取每个按钮的ID。 mEt=(EditText)findViewById(R.id.xEt); mEt.setOnTouchListener(此); mEt.setOnFocusChangeListener(此); mEt.setOnClickListener(this); mLayout=(相对长度)findViewById(R.id.xK1); mKLayout=(RelativeLayout)findviewbyd(R.id.xKeyBoard); hideDefaultKeyboard();//abrir teclado al-prender应用程序 启用键盘(); changeCapitalLetters(); changeCapitalTags(); }捕获(例外e){ w(getClass().getName(),例如toString()); } } @凌驾 公共布尔onTouch(视图v,运动事件){ 如果(v==mEt){ 隐藏错误键盘(); 启用键盘(); } 返回true; } @凌驾 公共void onClick(视图v){ 如果(v!=mBack&&v!=mBorrar){ 添加文本(v); }否则,如果(v!=mBorrar&&v==mBack){ isBack(v); }否则,如果(v!=mBack&&v==mBorrar){ 伊斯博拉(v); } } @凌驾 public void onFocusChange(视图v,布尔hasFocus){ if(v==mEt&&hasFocus==true){ isEdit=真; } } 私有void addText(视图v){ 如果(isEdit==true){ 字符串b=“”; b=(字符串)v.getTag(); 如果(b!=null){ //在Edittext中添加文本 见附件(b); } } } 私有void返回(视图v){ 如果(isEdit==true){ CharSequence cc=mEt.getText(); 如果(cc!=null&&cc.length()>0){ { mEt.setText(“”); mEt.append(cc.subSequence(0,cc.length()-1)); } } } } 私有void isBorrar(视图五){ 如果(isEdit==true){ CharSequence cc=mEt.getText(); 如果(cc!=null&&cc.length()>0){ { mEt.setText(“”); } } } } 私有void changeCapitalLetters(){ 对于(int i=0;i<?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>