app android“应用已意外停止。请再试一次”

时间:2011-02-22 07:36:22

标签: android

我在模拟器中运行应用程序后,我的活动意外停止。我究竟做错了什么?下面是我的主要活动代码和我的android清单:

package android.Database;


import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class projectsDatabase extends Activity {
    /** Called when the activity is first created. */

    SQLiteDatabase myDB = null;
    final static String MY_DB_NAME ="projectsDatabase";
    final static String MY_DB_TABLE = "projects";

    static final int MENU_PROJECTS = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreateDBAndDBTabled();
        setContentView(R.layout.main);
    }

    private void onCreateDBAndDBTabled(){

        myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
        myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE
                + " ( _id integer primary key_autoincrement,"+ 
                "Name varchar(100),"+ 
                "Comment varchar(128),"+ 
                "BookingDetails varchar(255),"+ 
                "Project-Kind integer(3))"
                +"");
         }

    public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);

            menu.add(0, MENU_PROJECTS, 0, R.string.menuProjects)
            .setShortcut('1', 'f')
            .setIcon(R.drawable.icon);
             return true;

          }

    public boolean onOptionsItemSelected(MenuItem item){
          switch (item.getItemId()) {
          case MENU_PROJECTS:
            Intent iProjects= new Intent(this, projects.class);
            startActivity(iProjects);
            return true;
          }
          return false;
        }



}

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".projectsDatabase"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".projects"></activity>
        <activity android:name=".projects_New"></activity>
    </application>
</manifest>

-------------------------------- EDIT -------------- -----------------------------------------

这是我的LogCat输出:

02-22 08:48:01.999: DEBUG/AndroidRuntime(819): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-22 08:48:02.009: DEBUG/AndroidRuntime(819): CheckJNI is ON
02-22 08:48:02.229: DEBUG/AndroidRuntime(819): --- registering native functions ---
02-22 08:48:02.239: INFO/jdwp(819): received file descriptor 23 from ADB
02-22 08:48:02.969: DEBUG/AndroidRuntime(819): Shutting down VM
02-22 08:48:02.979: DEBUG/dalvikvm(819): DestroyJavaVM waiting for non-daemon threads to exit
02-22 08:48:02.979: DEBUG/dalvikvm(819): DestroyJavaVM shutting VM down
02-22 08:48:02.989: DEBUG/dalvikvm(819): HeapWorker thread shutting down
02-22 08:48:02.989: DEBUG/dalvikvm(819): HeapWorker thread has shut down
02-22 08:48:02.999: DEBUG/jdwp(819): JDWP shutting down net...
02-22 08:48:02.999: DEBUG/jdwp(819): +++ peer disconnected
02-22 08:48:02.999: INFO/dalvikvm(819): Debugger has detached; object registry had 1 entries
02-22 08:48:03.009: DEBUG/dalvikvm(819): VM cleaning up
02-22 08:48:03.019: DEBUG/dalvikvm(819): LinearAlloc 0x0 used 629804 of 4194304 (15%)
02-22 08:48:03.479: DEBUG/AndroidRuntime(829): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-22 08:48:03.479: DEBUG/AndroidRuntime(829): CheckJNI is ON
02-22 08:48:03.689: DEBUG/AndroidRuntime(829): --- registering native functions ---
02-22 08:48:03.699: INFO/jdwp(829): received file descriptor 23 from ADB
02-22 08:48:04.439: INFO/ActivityManager(559): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10000000 comp={android.Database/android.Database.projectsDatabase} }
02-22 08:48:04.449: DEBUG/AndroidRuntime(829): Shutting down VM
02-22 08:48:04.459: DEBUG/dalvikvm(829): DestroyJavaVM waiting for non-daemon threads to exit
02-22 08:48:04.469: DEBUG/dalvikvm(829): DestroyJavaVM shutting VM down
02-22 08:48:04.469: DEBUG/dalvikvm(829): HeapWorker thread shutting down
02-22 08:48:04.469: DEBUG/dalvikvm(829): HeapWorker thread has shut down
02-22 08:48:04.479: DEBUG/jdwp(829): JDWP shutting down net...
02-22 08:48:04.479: DEBUG/jdwp(829): +++ peer disconnected
02-22 08:48:04.479: INFO/dalvikvm(829): Debugger has detached; object registry had 1 entries
02-22 08:48:04.489: DEBUG/dalvikvm(829): VM cleaning up
02-22 08:48:04.509: DEBUG/dalvikvm(829): LinearAlloc 0x0 used 639228 of 4194304 (15%)

--------------------------------------编辑2 ------- ---------------------------------------- 这是我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

我运行了你的代码,第一个LogCat异常提供了一个很好的线索:

02-22 09:08:57.414: ERROR/Database(277): Failure 1 (near "key_autoincrement": syntax error) on 0x121a40 when preparing 'CREATE TABLE IF NOT EXISTS projects ( _id integer primary key_autoincrement,Name varchar(100),Comment varchar(128),BookingDetails varchar(255),Project-Kind integer(3))'.

我的数据库帮助器设置如下:

    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + IP_DB
            + " TEXT, " + TIME + " TEXT);");

我认为你的错误在于“key_autoincrement”(正如错误信息所暗示的那样),尝试用2个单词写它而不是用下划线分隔它?

答案 1 :(得分:0)

你的main.xml文件在哪里?您是否创建了一个而未在此处上传或未创建它?如果是这样,那可能是你的问题。

此外,您应该打开logcat并在此处发布您的错误消息。

答案 2 :(得分:0)

一般提示:逐行注释消除您的代码。

从logcat中查看堆栈跟踪对我们也很有帮助。从命令行启动ddms以及log log报告的内容。