2017-08-07 37 views
0

我將使得Android應用程序,但由於致命的異常:主要應用保持爲停止狀態,同時運行收到一個錯誤,同時運行記事本應用

這裏是我的主要活動

package com.example.notepad.myapplication; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.ListView; 

import java.util.List; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
    Button ADD; 
    ListView li; 
    List<Note> notes; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ADD=(Button)findViewById(R.id.addnote); 
     ADD.setOnClickListener(this); 
     li= (ListView) findViewById(R.id.listview); 
     Databasehandler data =new Databasehandler(this); 
     notes= data.getAllNotes(); 
     adaptor adapt=new adaptor(this,notes); 
     try { 
      li.setAdapter(adapt); 
     }catch (NullPointerException e){ 
      Log.e("a","adaptor not found"); 
     } 
     li.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){ 


       /* Intent intent=new Intent(MainActivity.this,EditNoteActivity.class); 
       Bundle bundle=new Bundle() ; 
       bundle.putString("source","editpress"); 
       bundle.putString("noteTitle",note.getTitle()); 
       bundle.putString("noteDescription",note.getDescription()); 
       bundle.putInt("noteId",note.getId()); 
       intent.putExtras(bundle);*/ 
      } 
      } 
     ); 
} 
    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.addnote: 
      Intent myIntent = new Intent(MainActivity.this, EditNoteActivity.class); 
      Bundle bundle = new Bundle(); 
      bundle.putString("source", "addPress"); 
      myIntent.putExtras(bundle); 
      startActivity(myIntent); 
      break; 
     default: 
      break; 



    } 
} 
} 

這裏是我的數據庫處理程序

package com.example.notepad.myapplication; 

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

import java.util.ArrayList; 
import java.util.List; 


public class Databasehandler { 
    DatabaseHandle data; 


    public Databasehandler(Context context) { 
     data=new DatabaseHandle(context); 
    } 
/*public void DeleteNote(String id){ 


}*/ 

    public void addnote() { 
     Note note = new Note(); 
     SQLiteDatabase db = data.getWritableDatabase(); 
     EditNoteActivity edit = new EditNoteActivity(); 
     ContentValues values = new ContentValues(); 
     values.put("title", note.getTitle()); 
     values.put("description", note.getDescription()); 
     db.insert(DatabaseHandle.TableName, null, values); 
     db.close(); 
    } 

    public List<Note> getAllNotes() { 
     List<Note> notes=new ArrayList<Note>(); 
     SQLiteDatabase db= data.getWritableDatabase(); 
     Cursor c = db.query(DatabaseHandle.TableName, new String[] { DatabaseHandle.TableId, 
       DatabaseHandle.TableTitle, DatabaseHandle.TableDescription}, DatabaseHandle.TableId+ "=?",new String[] { String.valueOf(DatabaseHandle.TableId) } , null, null, null); 
     if(c!=null){ 
     while (c.moveToNext()) { 
      int index0 = c.getColumnIndex(DatabaseHandle.TableId); 
      int index1 = c.getColumnIndex(DatabaseHandle.TableTitle); 
      int index2 = c.getColumnIndex(DatabaseHandle.TableDescription); 

      Note note = new Note(); 
      note.setId(Integer.parseInt(c.getString(index0))); 
      note.setTitle(c.getString(index1)); 
      note.setDescription(c.getString(index2)); 
      notes.add(note); 
     }} 
     return notes; 
    } 


    static class DatabaseHandle extends SQLiteOpenHelper { 
     private static final String DatabaseName = "database"; 
     private static final int DatabaseVersion = 3; 
     private static final String TableName = "notes"; 
     private static final String TableId = "_id"; 
     private static final String TableTitle = "title"; 
     private static final String TableDescription = "description"; 
     private static final String TableCreate = "CREATE TABLE IF NOT EXITS" + TableName + "(" + TableId + "INTEGER PRIMARY KEY AUTOINCREMENT" + 
       TableTitle + "TEXT " + TableDescription + "TEXT"; 
     private static final String TableDrop = "DROP TABLE IF EXITS" + TableName; 

     DatabaseHandle(Context context) { 
      super(context, DatabaseName, null, DatabaseVersion); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      try { 
       db.execSQL(TableCreate); 
      } catch (SQLiteException e) { 
       e.printStackTrace(); 
       Log.e("D", "table not created"); 
      } 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int i, int i1) { 
     try{ db.execSQL(TableDrop); 
      onCreate(db);} 
     catch (SQLiteException e){ 
      Log.e("F","version not created"); 
     } 

     } 

    } 
} 

這裏是我的editnote活動

package com.example.notepad.myapplication; 


import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.EditText; 
import android.widget.Toast; 




    public class EditNoteActivity extends AppCompatActivity { 
    String A,B; 
    int id; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edit_notes); 
     EditText editText = (EditText) findViewById(R.id.editText2); 
     EditText editText1 = (EditText) findViewById(R.id.editText); 
     String A = editText.getText().toString(); 
     String B = editText1.getText().toString(); 
     Note note = new Note(); 
     note.setDescription(A); 
     note.setTitle(B); 
     Bundle bundle=getIntent().getExtras(); 
     if(bundle!=null && bundle.containsKey("source")){ 
      if(bundle.getString("source").equalsIgnoreCase("editpress")){ 
       isvalid(); 
       id=bundle.getInt("noteId"); 
       A=bundle.getString("noteTitle"); 
       B=bundle.getString("noteDescription"); 
      }else if(bundle.getString("source").equalsIgnoreCase("addPress")&& !isvalid()){ 
       Databasehandler data=new Databasehandler(this); 
       data.addnote(); 
      }else{ 
       Toast.makeText(this,"invalid argument",Toast.LENGTH_SHORT).show(); 
       super.onBackPressed(); 
      } 
     } 
    } 
    public boolean isvalid(){ 
     if(A.isEmpty()&&B.isEmpty()){ 
      Toast.makeText(this,"please enter tittle and description",Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
     else if(A.isEmpty()){ 
      Toast.makeText(this,"please enter title",Toast.LENGTH_SHORT).show(); 
      return false; 
     }else if(B.isEmpty()){ 
      Toast.makeText(this,"please enter description",Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
     return true; 
    } 

這裏是錯誤

08-07 15:14:46.966 2143-2143/com.example.notepad.myapplication E/SQLiteLog: (1) no such table: notes 
08-07 15:14:46.967 2143-2143/com.example.notepad.myapplication D/AndroidRuntime: Shutting down VM 
08-07 15:14:46.967 2143-2143/com.example.notepad.myapplication E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.notepad.myapplication, PID: 2143 
                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepad.myapplication/com.example.notepad.myapplication.MainActivity}: android.database.sqlite.SQLiteException: no such table: notes (code 1): , while compiling: SELECT _id, title, description FROM notes WHERE _id=? 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:154) 
                        at android.app.ActivityThread.main(ActivityThread.java:6077) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
                        Caused by: android.database.sqlite.SQLiteException: no such table: notes (code 1): , while compiling: SELECT _id, title, description FROM notes WHERE _id=? 
                        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
                        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
                        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
                        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
                        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
                        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
                        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
                        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318) 
                        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165) 
                        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036) 
                        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204) 
                        at com.example.notepad.myapplication.Databasehandler.getAllNotes(Databasehandler.java:41) 
                        at com.example.notepad.myapplication.MainActivity.onCreate(MainActivity.java:27) 
                        at android.app.Activity.performCreate(Activity.java:6662) 
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
                        at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:154)  
                        at android.app.ActivityThread.main(ActivityThread.java:6077)  
                        at java.lang.reflect.Method.invoke(Native Method)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)  

這裏是getter和setter

package com.example.notepad.myapplication; 


    public class Note { 
    public int id=0; 
    public String title=""; 
    public String description=""; 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title=title; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id=id; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 
} 
+0

您在創建表查詢中錯過了逗號(,) –

回答

1
private static final String TableCreate = "CREATE TABLE IF NOT EXITS " + TableName + "(" + TableId + "INTEGER PRIMARY KEY AUTOINCREMENT," + 
       TableTitle + "TEXT ," + TableDescription + "TEXT)"; 

錯過了逗號(,),並把空間後存在關鍵字"CREATE TABLE IF NOT EXITS "+TableName

檢查,更新

+0

ohk ...同樣的錯誤? –

+0

檢查退出拼寫以及..'EXISTS'' –

+0

是相同的錯誤。改變拼寫 – vikky

0

我已經通過更改DatabaseName =「database.db」來解決它

相關問題