我想在两个活动之间切换,并且我放了一个if语句,但在我解析startActivity方法之后,应用程序崩溃了:
activity1.java
public class StartActivity extends AppCompatActivity {
int count =0;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
db=openOrCreateDatabase("StopSmokeDB", Context.MODE_PRIVATE, null);
try {
Cursor mCount = db.rawQuery("select count(*) from UserTable", null);
mCount.moveToFirst();
count= mCount.getInt(0);
} catch (Exception e){
} finally {
if(count > 0){
finish();
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
} else {
finish();
startActivity(new Intent(this,RegActivity.class));
}
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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="tzahianidgar.stopsmoke.RegActivity"></activity>
<activity android:name="tzahianidgar.stopsmoke.StartActivity"></activity>
</application>
这是我的logcat: 04-10 05:57:38.316 3630-3630 / tzahianidgar.stopsmoke我/艺术:不迟到-Xcheck:jni(已经开启) 04-10 05:57:38.602 3630-3630 / tzahianidgar.stopsmoke W / System:ClassLoader引用未知路径:/data/app/tzahianidgar.stopsmoke-1/lib/x86 04-10 05:57:38.733 3630-3630 / tzahianidgar.stopsmoke W / System:ClassLoader引用未知路径:/data/app/tzahianidgar.stopsmoke-1/lib/x86
答案 0 :(得分:0)
首先,你应该在使用之后关闭光标:
if(mCount != null)
mCount.close();
其次,您在开始新活动之前关闭了当前活动。用以下代码替换finally块代码:
finally {
if(mCount != null)
mCount.close();
if(count > 0){
startActivity(new Intent(StartActivity.this,MainActivity.class));
finish();
} else {
startActivity(new Intent(StartActivity.this,RegActivity.class));
finish();
}
}