Android应用程序挂起类切换上的所有线程

时间:2016-04-05 18:40:17

标签: java android java-threads

我目前正在创建一个具有基本功能的应用程序,该基本具有SQLite元素,我通过Intent intent = new Intent(MainActivity.this, MemoActivity.class); startActivity(intent);链接到我的其余代码。我的问题是我的应用程序工作正常,但当我尝试导航到我的MemoActivity它挂起,它最初导航页面很好,现在它只是挂起android工作室不断给我

  

W / art:暂停所有线程:16.191ms

然后屏幕变黑,不再响应。什么想法让它挂起

MemoActivity.java

import android.support.v7.app.ActionBarActivity;  
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MemoActivity extends ActionBarActivity {

    EditText LInput;
    TextView LText;
    MyDbHandler dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memo);
        LInput = (EditText) findViewById(R.id.LInput);
        LText = (TextView) findViewById(R.id.LText);
        dbHandler = new MyDbHandler(this, null, null, 1);
        printDatabase();
    }

    public void addButtonClicked(View view){
        Memos lyrics = new Memos(LInput.getText().toString());
        dbHandler.addLyric(lyrics);
        printDatabase();
    }

    public void deleteButtonClicked(View view){
        String inputText = LInput.getText().toString();
        dbHandler.deleteLyrics(inputText);
        printDatabase();
    }

    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        LText.setText(dbString);
        LInput.setText("");
    }


}

activity_memo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LInput"
        android:layout_marginTop="71dp"
        android:width="300dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/addButton"
        android:layout_below="@+id/LInput"
        android:layout_alignLeft="@+id/LInput"
        android:layout_alignStart="@+id/LInput"
        android:layout_marginTop="44dp"
        android:onClick="addButtonClicked" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:id="@+id/deleteButton"
        android:layout_alignBottom="@+id/addButton"
        android:layout_alignRight="@+id/LInput"
        android:layout_alignEnd="@+id/LInput"
        android:longClickable="false"
        android:onClick="deleteButtonClicked" />

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/LText"
        android:layout_below="@+id/deleteButton"
        android:layout_toRightOf="@+id/addButton"
        android:layout_toEndOf="@+id/addButton"
        android:layout_marginTop="102dp" /> 

</RelativeLayout>

MyDbHandler.java

    import android.database.sqlite.SQLiteDatabase;  
    import android.database.sqlite.SQLiteOpenHelper;
    import android.database.Cursor;
    import android.content.Context;
    import android.content.ContentValues;
    public class MyDbHandler  extends SQLiteOpenHelper{


        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "Memos.db";
        public static final String TABLE_MEMOS = "Memos";
        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_LYRICS = "lyrics";

    public MyDbHandler(Context context, String name,     SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_MEMOS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_LYRICS + " TEXT" +
            ");";
    db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)     {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_MEMOS);
    onCreate(db);
}

    //add a new row to the database

    public void addLyric(Memos lyrics) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_LYRICS, lyrics.get_lyrics());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_MEMOS, null, values);
        db.close();
    }

    //delete product from database

    public void deleteLyrics (String lyrics){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_MEMOS + " WHERE " + COLUMN_LYRICS     + "=\"" + lyrics + "\";");
    }


    //print out database as string

    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_MEMOS + " WHERE 1";

        //cursor point to result
        Cursor c = db.rawQuery(query, null);
        //first row
        c.moveToFirst();

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("lyrics"))!= null){
                dbString += c.getString(c.getColumnIndex("lyrics"));
                dbString += "\n";
                dbString += "\n";
            }
        }
        db.close();
        return dbString;
    }

}

0 个答案:

没有答案