2016-02-05 156 views
0

关闭每次我打开我的应用我的手机上,以检查它,它接近第二它打开,并说: “”应用名称“停止”在设备应用程序上打开

它只是自动关闭,甚至没有去主版面

ps tnx帮助家伙!

调试说:

目标设备:54d1969c安装APK: C:\用户\埃雷尔\ 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); 
     } 
    } 
} 
+0

可能是力量关闭正在发生......尝试共享日志... – W0rmH0le

回答

0

onCreate(Bundle)是你初始化你的活动。
最重要的是,在这里您通常会调用setContentView(int),其中包含一个用于定义UI的布局资源,并使用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); 
0

你的错误是这样的一行:

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

移动它的OnCreate()你的活动

0

你有一些误差修改与你码。

ERROR1:

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

你必须将其移动到的onCreate。您不能在方法外部调用findViewById。

您应将其更改为:

你在一个空对象设置clickListener:

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); 
    } 
} 

()

误差2注意的是findViewById是的setContentView后称为:

btnAdd已创建但未实例化d。你必须找到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

String用于创建数据库是错误的。列名应该在引号内。

改变它的东西,如:

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" 
     + ")"; 
} 
+0

不客气。如果有人正确回答了您的问题,请不要忘记将其作为“接受的答案”进行市场宣传以关闭该主题 – W0rmH0le

0

按照这些stepes解决您的问题,如果你仍然有错误:

  1. 安装USB驱动程序(可以很容易地从供应商的网站下载)
  2. 检查USB调试(移动)和工具 - >启用集成(在Android SDK中)
  3. 连接你的设备,首先取消选中,然后检查USB调试(在手机中)会显示一个对话框以确认授权(适用于我)
相关问题