Android应用程序在eclipse中无法在设备上启动

时间:2014-04-11 18:04:39

标签: java android

基本上当我运行应用程序时,它会崩溃,这是我的代码,我不明白我做错了什么。我知道这与"答案"有关。变量

sqlAnswer = (EditText)findViewById(R.id.ans1);

这是我的MainActivity代码:我也拿出了答案,因为它出现了一个错误,我认为这是因为进行计算的按钮没有给出相同的输出,而且#&# 39;基本上是从XML重命名TextView。

public class MainActivity extends Activity implements OnClickListener {

Button sqlUpdate, sqlView;
EditText sqlNumber1, sqlNumber2, sqlAnswer;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
    sqlNumber1 = (EditText)findViewById(R.id.num1input);
    sqlNumber2 = (EditText)findViewById(R.id.num2input);
    sqlAnswer = (EditText)findViewById(R.id.ans1);

    sqlView = (Button) findViewById(R.id.bSQLopenView);
    sqlView.setOnClickListener(this);
    sqlUpdate.setOnClickListener(this);

}

public void onClick(View arg0) {

    switch (arg0.getId()){
    case R.id.bSQLUpdate:
        boolean didItWork = true;
        try{
        String number_one = sqlNumber1.getText().toString();
        String number_two = sqlNumber2.getText().toString();
        String answer = sqlAnswer.getText().toString();

        DBA entry = new DBA(MainActivity.this);

        entry.open();

        entry.createEntry(number_one, number_two);

        entry.close();
        } catch (Exception e) {
            e.printStackTrace();
                            didItWork = false;
        }finally{
            if (didItWork){
                Dialog d = new Dialog(this);
                d.setTitle("It Worked");
                TextView tv = new TextView(this);
                tv.setText("Success!");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    case R.id.bSQLopenView:
        Intent i = new Intent("com.cmpcione.calculator.SQLVIEW");
        startActivity(i);
        break;
    }
}

public void sum(View v) {
    EditText number1input = (EditText)findViewById(R.id.num1input);
    EditText number2input = (EditText)findViewById(R.id.num2input);

    Integer num1=Integer.parseInt(number1input.getText().toString()),num2=Integer.parseInt(number2input.getText().toString());

    Integer ans1=num1+num2;

    TextView answer=(TextView)findViewById(R.id.ans1);

    answer.setText(ans1.toString());

}

DBA类的代码:

public class DBA {

public static final String KEY_ROWID = "_id";
public static final String KEY_NUMBER1 = "num1input";
public static final String KEY_NUMBER2 = "num2input";
public static final String KEY_ANSWER = "ans1";


private static final String DATABASE_NAME = "CalculationsDB";
private static final String DATABASE_TABLE = "Calculation_table";
private static final int DATABASE_VERSION = 2;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        //creates table and rows
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +

                KEY_ROWID + " INTEGER PRIMARY KEY, " + 
                KEY_NUMBER1 + " TEXT NOT NULL, " + 
                KEY_NUMBER2 + " TEXT NOT NULL, " +
                KEY_ANSWER + " TEXT NOT NULL);" 
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public DBA(Context c){
    ourContext = c;
}

public DBA open() {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();

}

public long createEntry(String number_one, String number_two) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NUMBER1, number_one);
    cv.put(KEY_NUMBER2, number_two);
    cv.put(KEY_ANSWER, answer);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NUMBER1, KEY_NUMBER2};
    //using the cursor to read from database
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = " ";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iNumber1 = c.getColumnIndex(KEY_NUMBER1);
    int iNumber2 = c.getColumnIndex(KEY_NUMBER2);
    int iAnswer = c.getColumnIndex(KEY_ANSWER);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        //it gets the string of our index then it gets the first number and then the last
        result = result + c.getString(iRow) + " " + c.getString(iNumber1) + " " + c.getString(iNumber2) + "\n";
    }
    return null;


}

}

activity_main.xml如下,我在这里有几个按钮,你可以看到:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cmpcione.calculator.MainActivity$PlaceholderFragment" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/num1" />

    <EditText
        android:id="@+id/num1input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

    </EditText>

    <TextView
        android:id="@+id/num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/num2" />

    <EditText
        android:id="@+id/num2input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/calc_sum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sum"
        android:text="@string/calc_sum" />

    <Button
        android:id="@+id/calc_sub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="substract"
        android:text="@string/calc_sub" />

    <Button
        android:id="@+id/calc_mul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="multiply"
        android:text="@string/calc_mul" />

    <Button
        android:id="@+id/calc_div"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="divide"
        android:text="@string/calc_div" />

    <TextView
        android:id="@+id/ans2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:text="@string/answer" />

    <Button
        android:id="@+id/bSQLUpdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update Database" />

    <Button
        android:id="@+id/bSQLopenView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="View Database" />

</LinearLayout>

<TextView
    android:id="@+id/ans1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/linearLayout1"
    android:layout_marginBottom="132dp"
    android:layout_marginLeft="139dp"
    android:text="@string/ans" />

 </RelativeLayout>

logcat的完整列表:它从无法将数字插入数据库开始:

04-11 17:57:48.177: E/AndroidRuntime(278): FATAL EXCEPTION: main
04-11 17:57:48.177: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cmpcione.calculator/com.cmpcione.calculator.MainActivity}: java.lang.ClassCastException: android.widget.TextView
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 17:57:48.177: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:57:48.177: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
04-11 17:57:48.177: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 17:57:48.177: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 17:57:48.177: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
04-11 17:57:48.177: E/AndroidRuntime(278): Caused by: java.lang.ClassCastException: android.widget.TextView
04-11 17:57:48.177: E/AndroidRuntime(278):  at com.cmpcione.calculator.MainActivity.onCreate(MainActivity.java:27)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

1 个答案:

答案 0 :(得分:1)

ans1TextView

的ID
<TextView
android:id="@+id/ans1"

所以改变这个

sqlAnswer = (EditText)findViewById(R.id.ans1);

TextView sqlAnswer =(TextView)findViewById(R.id.ans1);