无法使用sql异常启动活动ComponentInfo

时间:2017-01-29 08:50:56

标签: java android android-studio

我正在开发一个应用,我只为> LOLLIPOP版本收到此错误。

如何解决此错误?

Log Cat:

    E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: me.saidur.fmradio, PID: 21695
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{me.saidur.fmradio/me.saidur.fmradio.MainActivity}: android.database.sqlite.SQLiteException: no such table: STATION (code 1): , while compiling: INSERT OR REPLACE  INTO STATION(GENRE2,CST,CTQUERYSTRING,GENRE3,ML,STATIONID,ID,NAME,LC,LOGO,GENRE,BRBITRATE,MT) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:135)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                    Caused by: android.database.sqlite.SQLiteException: no such table: STATION (code 1): , while compiling: INSERT OR REPLACE  INTO STATION(GENRE2,CST,CTQUERYSTRING,GENRE3,ML,STATIONID,ID,NAME,LC,LOGO,GENRE,BRBITRATE,MT) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
                                                       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.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                       at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                       at com.orm.SugarRecord.save(SugarRecord.java:280)
                                                       at com.orm.SugarRecord.save(SugarRecord.java:260)
                                                       at me.saidur.fmradio.MainActivity.prepareDatabase(MainActivity.java:194)
                                                       at me.saidur.fmradio.MainActivity.onCreate(MainActivity.java:99)
                                                       at android.app.Activity.performCreate(Activity.java:5990)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:135) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
01-29 12:26:40.122 468-947/? W/ActivityManager:   Force finishing activity 1 me.saidur.fmradio/.MainActivity

MainActivity.java 这里发生了致命异常java.lang.RuntimeException错误:无法启动活动ComponentInfo ...... android.database.sqlite.SQLiteException:没有这样的表:STATION(代码1):......

    protected void onCreate(Bundle savedInstanceState) {

    .................
    .................
    prepareDatabase();   // (MainActivity.java:98)
    .................
    .................
    }

 private void prepareDatabase() {
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {

            long station_id = Station.save(new Station());//(MainActivity.java:194)
            long current_station = CurrentStation.save(new CurrentStation());
            long search_station = SearchStation.save(new SearchStation());
            long station_added_manually_id = StationAddedManually.save(new StationAddedManually());

            try {
                Station station = Station.findById(Station.class, station_id);
                Station.delete(station);
                CurrentStation currentStation = CurrentStation.findById(CurrentStation.class, current_station);
                CurrentStation.delete(currentStation);
                SearchStation searchStation = SearchStation.findById(SearchStation.class, search_station);
                SearchStation.delete(searchStation);
                StationAddedManually stationAddedManually = StationAddedManually.findById(StationAddedManually.class, station_added_manually_id);
                StationAddedManually.delete(stationAddedManually);

            } catch (Exception ignored) {
                Log.e("Exception Raisd", "EX : " + ignored.getMessage());
            }
        } else {
            Station.findById(Station.class, (long) 1);
            CurrentStation.findById(CurrentStation.class, (long) 1);
            SearchStation.findById(SearchStation.class, (long) 1);
            StationAddedManually.findById(StationAddedManually.class, (long) 1);
        }
    }

项目的Manifest.xml文件

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

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:name="me.saidur.fmradio.MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="me.saidur.fmradio.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <meta-data
            android:name="DATABASE"
            android:value="stations.db" />
        <meta-data
            android:name="VERSION"
            android:value="9" />
        <meta-data
            android:name="QUERY_LOG"
            android:value="true" />
        <meta-data
            android:name="DOMAIN_PACKAGE_NAME"
            android:value="me.saidur.fmradio.model" />

        <service
            android:name="me.saidur.fmradio.MyService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

Station.java 这是可能导致问题的station.java文件。

public class Station extends SugarRecord {
    @Ignore
    public static final int TYPE_NONE = -1;
    @Ignore
    public static final int TYPE_MANUALLY_ADDED = 0;
    @Ignore
    public static final int TYPE_ADDED_AS_FAVOURITE_BY_CLICK = 1;
    private String name;
    private String brbitrate;
    private String ctquerystring;
    private String genre;

    @Unique
    private String stationid;
    private String lc;
    private String mt;
    private String logo;
    private String ml;
    private String genre2;
    private String genre3;
    private String cst;

    @Ignore
    private ArrayList<Uri> uriArrayList;
    @Ignore
    private int type = -1;


    public Station() {

    }
............................
............................
............................

    @Override
    public long save() {
        if (this.getStationId() != null && !this.getStationId().equals("")) {
            return super.save();
        } else
            return -1;

    }
............................
............................
............................ 

}

2 个答案:

答案 0 :(得分:0)

我建议您在清单中使用sdk版本,并检查编译过程中是否有任何错误。

    <uses-sdk android:minSdkVersion="integer"
          android:targetSdkVersion="integer"
          android:maxSdkVersion="integer" />

答案 1 :(得分:0)

最后,我通过在manifest.xml文件中注释以下代码来获得崩溃解决方案。

<meta-data
        android:name="DATABASE"
        android:value="stations.db" />
    <meta-data
        android:name="VERSION"
        android:value="9" />
    <meta-data
        android:name="QUERY_LOG"
        android:value="true" />

我尝试了很多解决方案,但没有任何效果。我从以下链接获得了解决方案:Android: Sugar ORM No Such Table Exception