装置上的应用程序关闭打开

时间:2016-02-05 17:47:03

标签: android shutdown auto-close

每次我在手机上打开我的应用程序进行检查时,它会在打开的第二个关闭时说: ""应用程序名称"停止"

它只是自动关闭,甚至没有进入主要布局

p.s tnx for help guys!

调试说:

  

目标设备:54d1969c安装APK:   C:\ Users \用户埃雷尔\ AndroidStudioProjects \ AccountSaver \应用\构建\输出\ APK \ APP-debug.apk   将文件上传到:/data/local/tmp/com.erelbiran.accountsaver   com.android.ddmlib.AdbCommandRejectedException:设备未经授权。   此adb服务器的$ ADB_VENDOR_KEYS未设置尝试使用adb kill-server'如果   这似乎是错的。否则请检查您的确认对话框   设备

MainActivity

package com.erelbiran.accountsaver;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


    DB myDB;
    Button btnAdd;
    EditText User = (EditText)findViewById(R.id.EnterUser), Pass = (EditText)findViewById(R.id.EnterPass), Acc = (EditText)findViewById(R.id.EnterAcc);

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openDB();
        btnAdd.setOnClickListener(
                new View.OnClickListener()
                {
                public void onClick(View view) {
                    myDB.insertRow(User.getText().toString(), Pass.getText().toString(), Acc.getText().toString());
                    Toast.makeText(MainActivity.this, "Account Added!", Toast.LENGTH_SHORT).show();
                }}
                 );




    }
    private void openDB(){
        myDB = new DB(this);
        myDB.open();
    }
    private void closeDB(){
        myDB.close();
    }

}

Menifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.erelbiran.accountsaver">

    <application
        android:debuggable="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

数据库代码:

// ------------------------------------ DBADapter.java ---------------------------------------------

package com.erelbiran.accountsaver;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DB {

    /////////////////////////////////////////////////////////////////////
    //  Constants & Data
    /////////////////////////////////////////////////////////////////////
    // For logging:
    private static final String TAG = "DBAdapter";

    // DB Fields
    public static final String KEY_ROWID = "_id";
    public static final int KEY_ACCID = 0;
    public static final String KEY_USER = "username";
    public static final String KEY_PASS = "password";
    public static final String KEY_ACC = "accounts";

    //
    // Setup fields
    public static final int COL_USER = 1;
    public static final int COL_PASS = 2;
    public static final int COL_ACC = 3;


    public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_USER, KEY_PASS, KEY_ACC};

    // DB info: it's name, and the table we are using (just one).
    public static final String DATABASE_NAME = "MyDb";
    public static final String DATABASE_TABLE = "mainTable";
    public static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE_SQL =
            "create table " + DATABASE_TABLE
                    + " (" + KEY_ACCID + " integer primary key autoincrement, "
                    + KEY_USER + " string not null, "
                    + KEY_PASS + " string not null, "
                    + KEY_ACC + " string not null"

                    // Rest  of creation:
                    + ");";

    // Context of application who uses us.
    private final Context context;

    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;

    /////////////////////////////////////////////////////////////////////
    //  Public methods:
    /////////////////////////////////////////////////////////////////////

    public DB(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }

    // Open the database connection.
    public DB open() {
        db = myDBHelper.getWritableDatabase();
        return this;
    }

    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }

    // Add a new set of values to the database.
    public long insertRow(String username   , String password  , String account) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_USER, username);
        initialValues.put(KEY_PASS, password);
        initialValues.put(KEY_ACC, account);


        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    // Delete a row from the database, by rowId (primary key)
    public boolean deleteAcc(long accId) {
        String where = KEY_ACCID + "=" + accId;
        return db.delete(DATABASE_TABLE, where, null) != 0;
    }

    public void deleteAll() {
        Cursor c = getAllRows();
        long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
        if (c.moveToFirst()) {
            do {
                deleteAcc(c.getLong((int) rowId));
            } while (c.moveToNext());
        }
        c.close();
    }



    // Return all data in the database.
    public Cursor getAllRows() {
        String where = null;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Get a specific row (by rowId)
    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }




    /////////////////////////////////////////////////////////////////////
    //  Private Helper Classes:
    /////////////////////////////////////////////////////////////////////

    /**
     * Private class which handles database creation and upgrading.
     * Used to handle low-level database access.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE_SQL);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data!");

            // Destroy old database:
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

            // Recreate new database:
            onCreate(_db);
        }
    }
}

4 个答案:

答案 0 :(得分:0)

onCreate(Bundle)是您初始化活动的地方。
最重要的是,在这里,您通常会使用定义UI的布局资源调用setContentView(int),并使用findViewById(int)检索该UI中需要以编程方式进行交互的窗口小部件。 尝试在onCreate(Bundle )中获取您的ui 试试吧

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    User = (EditText)findViewById(R.id.EnterUser);
    Pass = (EditText)findViewById(R.id.EnterPass);
    Acc = (EditText)findViewById(R.id.EnterAcc);

答案 1 :(得分:0)

你的错误是这一行:

  EditText User = (EditText)findViewById(R.id.EnterUser), 
Pass = (EditText)findViewById(R.id.EnterPass),
 Acc = (EditText)findViewById(R.id.EnterAcc);

将其移至活动的OnCreate()

答案 2 :(得分:0)

你的代码有一些错误。

<强> ERROR1:

EditText User = (EditText)findViewById(R.id.EnterUser), Pass = (EditText)findViewById(R.id.EnterPass), Acc = (EditText)findViewById(R.id.EnterAcc);

您必须将其移至onCreate。你不能在方法之外调用findViewById。

您应将其更改为:

public class MainActivity extends Activity {

    DB myDB;
    Button btnAdd;
    EditText User,Pass, Acc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);

        User = (EditText)findViewById(R.id.EnterUser);
        Pass = (EditText)findViewById(R.id.EnterPass);
        Acc = (EditText)findViewById(R.id.EnterAcc);
    }
}

请注意,在setContentView()之后调用findViewById

<强>误差2

您正在null对象中设置clickListener:

创建btnAdd但未实例化。你必须找到btnAdd应该指向的视图。在setOnClickListener之前使用findViewByID:

public class MainActivity extends Activity {
    Button btnAdd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        btnAdd = (Button)findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(......
    }
}

<强>误差3

用于创建数据库的字符串是错误的。列名应在引号内。

将其更改为:

发件人

public class DB {
    private static final String DATABASE_CREATE_SQL =
        "create table " + DATABASE_TABLE
        + " (" + KEY_ACCID + " integer primary key autoincrement, "
        + KEY_USER + " string not null, "
        + KEY_PASS + " string not null, "
        + KEY_ACC + " string not null"
        + ");";
}

public class DB {
    private static final String DATABASE_CREATE_SQL =
        "create table " + DATABASE_TABLE
        + " (\"" + KEY_ACCID + "\" integer primary key autoincrement, \""
        + KEY_USER + "\" string not null, \""
        + KEY_PASS + "\" string not null, \""
        + KEY_ACC + "\" string not null"
        + ")";
}

答案 3 :(得分:0)

如果您仍有错误,请按照以下步骤解决问题:

  1. 安装USB驱动程序(可以从供应商网站轻松下载)
  2. 检查USB调试(在移动设备中)和工具 - &gt;启用集成(在Android SDK中)
  3. 连接您的设备,首先取消选中,然后选中检查USB调试(在移动设备中),将显示一个对话框以确认授权(适用于我)