开发者

app android "app has stopped unexpectedly. please try again"

my activity stops unexpectedly as soon as i run the app in the emulator. What am i doing wrong? Below is my code from the main activity and my android manifest:

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-------------------------------------------------------

here is my LogCat output:

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 shutti开发者_运维知识库ng 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%)

--------------------------------------Edit 2----------------------------------------------- here is my 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>


I ran your code, the first LogCat exception gives a good clue:

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))'.

My database helper is setup like this instead:

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

I think your error lies with the "key_autoincrement" (as the error message implies), try writing it in 2 words as opposed to separating it with an underscore?


Where's your main.xml file? Have you created one and not uploaded it here or have you not created it? If so, that could be your problem.

Also, you should open logcat and post your error message here.


General Tip: Eliminate your code by comment out line by line.

It would also be helpful for us to see the stacktrace from logcat. Start ddms from the command line and se what logcat is reporting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜