2016-02-05 153 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調試(在手機中)會顯示一個對話框以確認授權(適用於我)
相關問題