1
我的應用程序在啓動時顯示啓動畫面。屏幕將顯示3秒鐘。在顯示時,應用程序將從服務器下載json。無論何時3分鐘到達或下載完成(以較長的時間爲準),主屏幕都將被加載。以下代碼顯示了這是如何實現的。Android啓動畫面崩潰
if (isNetworkConnected()) {
networkConnected = true;
} else {
networkConnected = false;
toast(R.string.no_internet_connection);
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
timeIsUp = true;
if (syncDone || !networkConnected)
loadHomeActivity();
}
}, SPLASH_TIME_OUT);
Callback<DenkoModel> callback = new Callback<DenkoModel>() {
@Override
public void success(DenkoModel denkoModel, Response response) {
toast(R.string.server_connection_successful);
syncDone = true;
denkoStationService.updateDatabaseWithDenkoModel(denkoModel);
if (timeIsUp)
loadHomeActivity();
}
@Override
public void failure(RetrofitError error) {
toast(R.string.server_connection_failed);
syncDone = true;
if (timeIsUp)
loadHomeActivity();
}
};
denkoStationService.fetchDenkoModel(denkoStationService.fetchDenkoLastDataState(), callback);
該代碼在模擬器上正常工作,但是當我在真實設備上測試它時,應用程序會崩潰。我的猜測是線程問題導致應用程序崩潰,但所有事件都發生在主線程上。
什麼可能導致應用程序崩潰,我該如何解決這個問題?
編輯:我logcat的輸出
04-18 11:43:06.810 11612-11612/? W/SignalStrength﹕ getLevel=2
04-18 11:43:07.230 429-446/? I/PackageManager﹕ Removing non-system package:org.bitbucket.infovillafoundation.denko
04-18 11:43:07.390 429-472/? E/dalvikvm﹕ GC_EXPLICIT freed 1086K, 42% free 17256K/29447K, paused 4ms+25ms, total 120ms
04-18 11:43:08.350 429-472/? I/PackageManager﹕ Updating external media status from unmounted to unmounted
04-18 11:43:08.370 10350-10364/? I/k9﹕ storage path "/storage/sdcard0" unmounting
04-18 11:43:08.410 429-446/? I/PackageManager﹕ Running dexopt on: org.bitbucket.infovillafoundation.denko
04-18 11:43:08.890 429-503/? E/WifiHW﹕ wifi_send_command : SIGNAL_POLL ; interface index=0;
04-18 11:43:09.690 10350-10364/? I/k9﹕ storage path "/storage/sdcard0" unmounted
04-18 11:43:10.040 429-457/? W/lights﹕ TP Button Light current value is 255
04-18 11:43:23.400 11913-11929/? I/PushLogAC2313﹕ [Thread-6813-6813]the pkgName:org.bitbucket.infovillafoundation.denko, localPkgName:com.huawei.android.pushagent(pushagent/null:-1)
04-18 11:43:24.380 24178-24178/? I/AppAddOrRemoveListener﹕ org.bitbucket.infovillafoundation.denko uninstalled
04-18 11:43:24.460 429-457/? I/ActivityManager﹕ Displayed
04-18 11:43:24.460 429-457/? I/ActivityManager﹕ Displayed org.bitbucket.infovillafoundation.denko/.activity.SplashScreenActivity: +2s119ms
04-18 11:43:24.470 11967-11967/? E/Trace﹕ error opening trace file: No such file or directory (2)
04-18 11:43:24.550 11967-11967/? W/dalvikvm﹕ Refusing to reopen boot DEX '/system/framework/hwframework.jar'
04-18 11:43:29.490 10526-11790/? W/AmazonAppstore.AppManagerAndroidPackageDelegate﹕ Unable to determine asin for package org.bitbucket.infovillafoundation.denko. Skipping metadata update.
[ReceiverDispatcher-6814]responseAddPackage pkgName= org.bitbucket.infovillafoundation.denko(pushagent/null:-1)
04-18 11:43:30.360 11913-11942/? I/PushLogAC2313﹕ [ReceiverDispatcher-6814]PushState get action :android.intent.action.PACKAGE_ADDED(pushagent/null:-1)
04-18 11:43:32.480 24178-24178/? I/AppAddOrRemoveListener﹕ org.bitbucket.infovillafoundation.denko installed
04-18 11:43:32.890 12113-12113/? E/Trace﹕ error opening trace file: No such file or directory (2)
04-18 11:43:34.480 10526-11790/? W/AmazonAppstore.AppManagerAndroidPackageDelegate﹕ Unable to determine asin for package org.bitbucket.infovillafoundation.denko. Skipping metadata update.
04-18 11:43:34.570 12159-12159/? W/System.err﹕ Invalid int: ""
04-18 11:43:34.590 12159-12159/? W/ExpressLog﹕ can't find sdcard for log store.
04-18 11:43:34.640 12159-12159/? I/UpdateManager﹕
04-18 11:43:34.950 24178-24178/? I/AppAddOrRemoveListener﹕ org.bitbucket.infovillafoundation.denko replaced
04-18 11:43:34.990 429-3345/? E/LocSvc_api_rpc_glue﹕ V/proc: 1 prog: 3100008c vers: 50001
編輯:
DenkoApplication.java
public class DenkoApplication extends Application {
private static Context initialContext;
private static SQLiteDatabase database;
@Override
public void onCreate() {
super.onCreate();
initialContext = getApplicationContext();
setupDb();
setupLanguage();
registerLocationListener();
}
private void setupDb() {
DenkoDbHelper helper = new DenkoDbHelper(initialContext, "denko-stations-db", null);
database = helper.getWritableDatabase();
}
public static Context getAppContext() {
return initialContext;
}
public static SQLiteDatabase getDatabase() {
return database;
}
private void setupLanguage() {
setLocale(readLanguage());
}
private void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
private void registerLocationListener() {
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new DenkoLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
DenkoLocationListener.isGpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
private SharedPreferences obtainSharedPreferences() {
return getSharedPreferences("org.bitbucket.infovillafoundation.denko", Context.MODE_PRIVATE);
}
private String readLanguage() {
return obtainSharedPreferences().getString("org.bitbucket.infovillafoundation.denko.language", "en");
}
}
SplashScreenActivity.java
public class SplashScreenActivity extends Activity {
private static int SPLASH_TIME_OUT = 3000;
@InjectView(R.id.imgLogo)
ImageView logoImage;
@InjectView(R.id.welcomeText)
TextView welcomeText;
boolean timeIsUp;
boolean syncDone;
boolean networkConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ButterKnife.inject(this);
DenkoStationComponent component = DaggerDenkoStationComponent.builder().denkoStationModule(new DenkoStationModule()).build();
final DenkoStationService denkoStationService = component.provideDenkoStationService();
if (isNetworkConnected()) {
networkConnected = true;
} else {
networkConnected = false;
toast(R.string.no_internet_connection);
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
timeIsUp = true;
if (syncDone || !networkConnected)
loadHomeActivity();
}
}, SPLASH_TIME_OUT);
Callback<DenkoModel> callback = new Callback<DenkoModel>() {
@Override
public void success(DenkoModel denkoModel, Response response) {
toast(R.string.server_connection_successful);
syncDone = true;
denkoStationService.updateDatabaseWithDenkoModel(denkoModel);
if (timeIsUp)
loadHomeActivity();
}
@Override
public void failure(RetrofitError error) {
toast(R.string.server_connection_failed);
syncDone = true;
if (timeIsUp)
loadHomeActivity();
}
};
denkoStationService.fetchDenkoModel(denkoStationService.fetchDenkoLastDataState(), callback);
}
private void loadHomeActivity() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
private SharedPreferences obtainSharedPreferences() {
return getSharedPreferences("org.bitbucket.infovillafoundation.denko", Context.MODE_PRIVATE);
}
private String readLanguage() {
return obtainSharedPreferences().getString("org.bitbucket.infovillafoundation.denko.language", "en");
}
private void writeLanguage(String language) {
obtainSharedPreferences().edit().putString("org.bitbucket.infovillafoundation.denko.language", language).apply();
}
public void toast(int textId) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.toast_text);
text.setText(getResources().getString(textId));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
MainActivity.java
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items = getResources().getStringArray(R.array.menus);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
DenkoMenuArrayAdapter adapter = new DenkoMenuArrayAdapter(this, items);
mDrawerList.setAdapter(adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/* Called when drawer is closed */
public void onDrawerClosed(View view) {
//Put your code here
}
/* Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
//Put your code here
}
};
/* Setting DrawerToggle on DrawerLayout */
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayView(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
});
updateFragment();
}
public void updateFragment() {
FragmentManager fragmentManager = getFragmentManager();
MainViewFragment rFragment = new MainViewFragment();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, rFragment);
ft.commit();
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MainViewFragment();
break;
case 1:
fragment = new StationViewFragment();
break;
case 2:
fragment = new MapViewFragment();
break;
/*case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;*/
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
}
發佈您的logcat。 – Shvet
顯示Logcat輸出。 –
Logcat已包含在問題中。 –