android 按钮失灵 no keyboard for id 0 5C

图片说明
如图,添加按钮点击无反应,也没错误提示,就是在两个edittext输入时logcat有这样的警告
图片说明
代码如下:

 import java.sql.Date;
import java.text.SimpleDateFormat;

import android.R.integer;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddFoodActivity1 extends Activity implements OnClickListener{
    private MySQLiteOpenHelper dbHelper;
    private EditText sFoodName1,iEXP1;
    String foodsName1,foodsEXP1;
    int nameFlag,expFlag;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.add_food_activity1);


        sFoodName1 = (EditText) findViewById(R.id.et_foodName1);
        iEXP1 = (EditText) findViewById(R.id.et_EXP1);



    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
//              db.beginTransaction();//开启事务

        if (v.getId() == R.id.bt_add1) {
            foodsName1 = sFoodName1.getText().toString();
            foodsEXP1 = iEXP1.getText().toString();

            dbHelper = MySQLiteOpenHelper.getInstance(this);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            //查询是否已存在食物名称
            Cursor cursor = db.rawQuery("select foodsName from food1 where foodsName=?", new String[]{foodsName1});
            while (cursor.moveToNext()){
                String name = cursor.getString(1);
                String exp = cursor.getString(2);

                if (foodsName1.isEmpty()){
                    Toast.makeText(AddFoodActivity1.this, "请输入食物名称", Toast.LENGTH_SHORT).show(); 
                    break;

                }else if(foodsEXP1.isEmpty()){
                    Toast.makeText(AddFoodActivity1.this, "请输入保质期", Toast.LENGTH_SHORT).show();
                    break;

                }else if (foodsName1.isEmpty()&&foodsEXP1.isEmpty()) {
                    Toast.makeText(AddFoodActivity1.this, "请输入食物名称与保质期", Toast.LENGTH_SHORT).show();
                    break;

                }

                nameFlag = 1;//数据库没有此名

                if (foodsName1.equals(name)) {
                    Toast.makeText(AddFoodActivity1.this, "添加失败,食品名重复,请重新输入", Toast.LENGTH_SHORT).show();
                    nameFlag = 0;
                    break;
                }

            }

            if (nameFlag == 1) {
                int trans = Integer.parseInt(foodsEXP1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date curDate = new Date(System.currentTimeMillis());
                String str=sdf.format(curDate);
                String exptime = str + trans*24*60*60*1000;

                db.execSQL("insert into food1 values (null,?,?)",new String[]{foodsName1,exptime});
//              db.setTransactionSuccessful();//事务执行成功
//              db.endTransaction();//结束事务
                Toast.makeText(AddFoodActivity1.this, "添加成功", Toast.LENGTH_SHORT).show();

            }
            cursor.close();
            db.close();
        }
    }

}

这是数据库

 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private static MySQLiteOpenHelper instance;
    public static final String CREATE_FOOD1 = "create table if not exists food1(id integer primary key autoincrement,foodsName varchar(20),EXP NUMERIC)";
    public MySQLiteOpenHelper(Context context,String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_FOOD1);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
//       db.execSQL("drop table if exists food");
//       onCreate(db);

    }
    public static MySQLiteOpenHelper getInstance(Context context) {
        if (instance == null) {
            instance = new MySQLiteOpenHelper(context, "Food1.db", null, 2);
        }
        return instance;

    }


}

cmd查看数据库发现表下没有column,感觉是数据库问题,但具体什么地方又不知道

图片说明

debug了一下,发现dbHelper这里可能死循环了,log cat里一直报那两行信息
图片说明

图片说明
图片说明

新手上路,求大神指教

6个回答



    3.2  android:scrollbars

android:layout_height="wrap_content" android:minHeight="50dp"
android:background="@android:drawable/editbox_background"
android:scrollbars="vertical"
android:maxLines="4">

代码看上去没什么问题。你在onClick方法第一行弹个土司,看一下if条件是否返回true,我不知道你的布局文件中有没有定义其他隐藏按钮,如果有看一下R.id.bt-add1按钮是否是界面上展示出来的那个按钮,id对不对,id是否冲突。

public void onClick(View v) {
Toast.makeText(this, "if条件是否成立:"+(v.getId() == R.id.bt_add1), Toast.LENGTH_SHORT).show();
//TODO ....后续代码
}

shengjunlintianxia
shengjunlintianxia 布局文件发到回答里去了
一年多之前 回复
shengjunlintianxia
shengjunlintianxia 返回的是true啊,这是布局文件
一年多之前 回复

一般是id冲突,或者clean下程序

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_marginBottom="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="食物名称:" 
            android:textSize="16dp"/>

        <EditText
            android:id="@+id/et_foodName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_marginBottom="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="保质期/剩余天数" />

        <EditText
            android:id="@+id/et_EXP1"
            android:layout_width="66dp"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="天" />
    </LinearLayout>

    <Button
        android:id="@+id/bt_add1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="#fff"
        android:text="添  加" 
        android:layout_marginBottom="20dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保鲜提示:
        " />

</LinearLayout>

你试试clean一下

shengjunlintianxia
shengjunlintianxia clean了,没有用
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!