我的应用程序在api23上运行并在api11上崩溃

时间:2017-06-04 21:40:45

标签: android sqlite class crash

我的应用程序处理api23并在api11或android4上崩溃 我认为G级导致了崩溃。 在G类中,我将assets文件夹上的sqlite数据库复制到外部存储器。 当我清除android:name =“。G”在androidmanifest我的应用程序在api11上工作,但在布局点击按钮读取数据库不读取数据库并崩溃我的应用程序

androidmanifest is=
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="view.viramaham.quiz.viraquiz">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application

    android:name=".G"
    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>

    <activity android:name=".Main_soalat">

    </activity>

    <activity android:name=".pasokhnameh">

    </activity>

  </application>
 </manifest>

和我项目中的G类=

package view.viramaham.quiz.viraquiz;
 import android.app.Application;
 import android.content.Context;
 import android.content.Intent;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;

 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;




 public   class  G extends Application {
public static Context context;
public static String SDK_DIR =      Environment.getExternalStorageDirectory().getAbsolutePath();
public static String APP_DIR = SDK_DIR + "/database/";
public static SQLiteDatabase database;


@Override
public  void onCreate() {

    super.onCreate();


    context = getApplicationContext();
    File file = new File(APP_DIR);

    if (!file.exists()) {
        try {
            file.mkdirs();
            file.createNewFile();
                copyFromAssets(getBaseContext().     getAssets()  .  open     ("viraquiz.sqlite"),
                    new FileOutputStream(APP_DIR + "/viraquiz.sqlite"));

        } catch (IOException e) {

            e.printStackTrace();
        }
    }//payandatabase



}//oncreate

public void copyFromAssets(InputStream inputStream, OutputStream   outputStream) {

    byte[] buffer = new byte[1024];
    int length;
    try {
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        inputStream.close();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


}//payan copyFromAssets



}

1 个答案:

答案 0 :(得分:0)

api 11第一次检查班G.我的班级有一个eror.i更改班级G并在所有api上运行我的程序。 G级=

  package view.viramaham.quiz.viraquiz;
 import android.app.Application;
 import android.content.Context;
 import android.content.Intent;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;

 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;




 public   class  G extends Application {
public static Context context;
public static String SDK_DIR =   Environment.getExternalStorageDirectory().getAbsolutePath();
public static String APP_DIR = SDK_DIR + "/database/";
public static SQLiteDatabase database;


@Override
public  void onCreate() {

    super.onCreate();


    context = getApplicationContext();
    File file = new File(APP_DIR);

    if (!file.exists()) {
        try {
            file.mkdirs();
            file.createNewFile();
            copyFromAssets(getBaseContext().getAssets().open("viraquiz.sqlite"),
                    new FileOutputStream(APP_DIR + "/viraquiz.sqlite"));

        } catch (IOException e) {

            e.printStackTrace();
        }
    }//payandatabase



}//oncreate

public void copyFromAssets(InputStream inputStream, OutputStream  outputStream) {

    byte[] buffer = new byte[1024];
    int length;
    try {
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        inputStream.close();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


}//payan copyFromAssets

}