我的应用一直停止

时间:2018-04-08 15:54:15

标签: android database sqlite emulation

这款应用有什么问题?它一直停止,LogCat似乎引用模块memtrack和数据库请求错误。我也使用正确的模拟器与应用程序请求的API。我是Android编码的新手。

这是我的清单

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


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

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

这是 MainActivity

package com.example.davide.poste;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.ListViewCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;

import com.example.davide.poste.Adapter.SearchAdapter;
import com.example.davide.poste.Database.Database;
import com.mancj.materialsearchbar.MaterialSearchBar;

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



public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;

MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();

Database database;


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

    // Init view
    recyclerView = (RecyclerView) findViewById(R.id.recycler_search);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    materialSearchBar = (MaterialSearchBar) findViewById(R.id.search_bar);

    //Init DB
    database = new Database(this);

    //Setup search bar
    materialSearchBar.setHint("Search");
    materialSearchBar.setCardViewElevation(10);
    loadSuggestList();
    materialSearchBar.addTextChangeListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            List<String> suggest = new ArrayList<>();
            for(String search:suggestList)
            {
                if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
                    suggest.add(search);
            }
            materialSearchBar.setLastSuggestions(suggest);

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
    materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
        @Override
        public void onSearchStateChanged(boolean enabled) {
            if(!enabled)
                recyclerView.setAdapter(adapter);
        }

        @Override
        public void onSearchConfirmed(CharSequence text) {
            startSearch(text.toString());
        }

        @Override
        public void onButtonClicked(int buttonCode) {

        }
    });

    //Init Adapter default set all result
    adapter = new SearchAdapter(this,database.getFriends());
    recyclerView.setAdapter(adapter);
}

private void startSearch(String text) {

    adapter = new SearchAdapter(this,database.getFriendByName(text));
    recyclerView.setAdapter(adapter);
}

private void loadSuggestList() {
    suggestList = database.getNames();
    materialSearchBar.setLastSuggestions(suggestList);
}
}

这是 LogCat

  

04-08 15:43:15.668 1972-2410 / com.android.systemui E / ResourcesManager:无法添加资产路径/data/app/com.example.davide.poste-Md6Miz27wpQdWXVZAgMl8w==/base.apk      04-08 15:43:15.669 1972-2410 / com.android.systemui E / ResourcesManager:无法添加资产路径/data/app/com.example.davide.poste-Md6Miz27wpQdWXVZAgMl8w==/base.apk      04-08 15:43:15.670 1972-2410 / com.android.systemui E / ResourcesManager:无法添加资产路径/data/app/com.example.davide.poste-Md6Miz27wpQdWXVZAgMl8w==/base.apk      04-08 15:43:15.683 1972-2410 / com.android.systemui E / ResourcesManager:无法添加资产路径/data/app/com.example.davide.poste-Md6Miz27wpQdWXVZAgMl8w==/base.apk      04-08 15:43:15.763 1762-3443 / system_process E / memtrack:无法加载memtrack模块      04-08 15:43:15.957 1762-3443 / system_process E / memtrack:无法加载memtrack模块      04-08 15:43:16.241 1762-3429 / system_process E / memtrack:无法加载memtrack模块      04-08 15:43:15.553 8772-8772 / com.example.davide.poste E / AndroidRuntime:FATAL EXCEPTION:main       处理:com.example.davide.poste,PID:8772       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.davide.poste / com.example.davide.poste.MainActivity}:android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)           在android.os.Handler.dispatchMessage(Handler.java:105)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6541)           at java.lang.reflect.Method.invoke(Native Method)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)        引起:android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库           在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)           在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)           在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)           在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)           在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)           在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)           在android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)           在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)           在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)           在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)           at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264)           在com.example.davide.poste.Database.Database.getNames(Database.java:59)           在com.example.davide.poste.MainActivity.loadSuggestList(MainActivity.java:106)           在com.example.davide.poste.MainActivity.onCreate(MainActivity.java:51)           在android.app.Activity.performCreate(Activity.java:6975)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)           在android.os.Handler.dispatchMessage(Handler.java:105)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6541)           at java.lang.reflect.Method.invoke(Native Method)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)&lt;

编辑:这是数据库代码

package com.example.davide.poste.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.example.davide.poste.Model.Friend;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

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

/**
 * Created by Leandro on 06/04/18.
 */

public class Database extends SQLiteAssetHelper{

private static final String DB_NAME="friend.db";
private static final int DB_VER=1;

public Database(Context context) {
    super(context, DB_NAME, null, DB_VER);
}

//Function get all friends
public List<Friend> getFriends()
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect={"Id","Name","Address","Email","Phone"};
    String tableName="Friends";

    qb.setTables(tableName);

    Cursor cursor = qb.query(db,sqlSelect,null,null,null,null,null);
    List<Friend> result = new ArrayList<>();
    if(cursor.moveToFirst())
    {
        do {
            Friend friend = new Friend();
            friend.setId(cursor.getInt(cursor.getColumnIndex("Id")));
            friend.setName(cursor.getString(cursor.getColumnIndex("Name")));
            friend.setAddress(cursor.getString(cursor.getColumnIndex("Address")));
            friend.setEmail(cursor.getString(cursor.getColumnIndex("Email")));
            friend.setPhone(cursor.getString(cursor.getColumnIndex("Phone")));

            result.add(friend);
        }while (cursor.moveToNext());
    }
    return result;
}

// Function get all friend's name
public List<String> getNames()
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect={"Name"};
    String tableName="Friends";

    qb.setTables(tableName);
    Cursor cursor = qb.query(db,sqlSelect,null,null,null,null,null);
    List<String> result = new ArrayList<>();
    if(cursor.moveToFirst())
    {
        do {
            result.add(cursor.getString(cursor.getColumnIndex("Name")));
        }while (cursor.moveToNext());
    }
    return result;
}

//Function get friend by name
public List<Friend> getFriendByName(String name)
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect={"Id","Name","Address","Email","Phone"};
    String tableName="Friends";

    qb.setTables(tableName);
    Cursor cursor = qb.query(db,sqlSelect,"Name LIKE ?",new String[]{"%"+name+"%"},null,null,null);
    List<Friend> result = new ArrayList<>();
    if(cursor.moveToFirst())
    {
        do {
            Friend friend = new Friend();
            friend.setId(cursor.getInt(cursor.getColumnIndex("Id")));
            friend.setName(cursor.getString(cursor.getColumnIndex("Name")));
            friend.setAddress(cursor.getString(cursor.getColumnIndex("Address")));
            friend.setEmail(cursor.getString(cursor.getColumnIndex("Email")));
            friend.setPhone(cursor.getString(cursor.getColumnIndex("Phone")));

            result.add(friend);
        }while (cursor.moveToNext());
    }
    return result;
}
}

提前致谢!

0 个答案:

没有答案