主要活动:
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
private UiLifecycleHelper uiHelper;
LocationClient mClient;
LocationRequest mRequest;
private String user;
private String userId;
private ImageButton addPlaceBtn;
private SQLiteDataSource datasource;
private TextView notifTextView;
private boolean appIsOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main);
datasource = new SQLiteDataSource(this);
datasource.open();
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
if (isFacebookLoggedIn()) {
System.out.println("We are logged in!");
} else {
showLogin();
}
if (datasource.getAllImageItems().isEmpty()) {
Intent splash = new Intent(this, SplashActivity.class);
startActivity(splash);
}
addPlaceBtn = (ImageButton) findViewById(R.id.addPlace1);
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(),
android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("PREPARATE", null),
OffersFragmentActivity.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("CATEGORII", null),
CategoriesActivity.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setIndicator("CAIETE", null), BooksFragmentActivity.class,
null);
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_text_selector);
mTabHost.getTabWidget().setDividerDrawable(R.drawable.divider);
final TextView tv = (TextView) mTabHost.getTabWidget()
.getChildAt(i).findViewById(android.R.id.title);
if (tv == null)
continue;
else
tv.setTextColor(0xFFFFFFFF);
}
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
switch (mTabHost.getCurrentTab()) {
case 0:
addPlaceBtn.setVisibility(View.VISIBLE);
break;
case 1:
addPlaceBtn.setVisibility(View.GONE);
break;
case 2:
addPlaceBtn.setVisibility(View.GONE);
break;
default:
break;
}
}
});
addPlaceBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (user != null && isOnline()) {
Intent i = new Intent(getApplication(),
CameraActivity.class);
startActivity(i);
} else if (user == null) {
// If user is not logged in, start login activity
Intent i = new Intent(getApplication(), LoginActivity.class);
startActivity(i);
} else if (!isOnline()) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setTitle("Internetul este oprit");
alertDialogBuilder
.setMessage("Pentru actualizarea continutului aveti nevoie de internet. Porniti acum?");
alertDialogBuilder.setPositiveButton("Da",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// go to a new activity of the app
Intent settingsIntent = new Intent(
Settings.ACTION_DATA_ROAMING_SETTINGS);
settingsIntent
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(settingsIntent);
finish();
}
});
// set negative button: No message
alertDialogBuilder.setNegativeButton("Nu",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// cancel the alert box and put a Toast to
// the user
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.action_notifications);
View actionView = item.getActionView();
notifTextView = (TextView) actionView.findViewById(R.id.notifTextView);
String unreadNotifs = datasource.countUnreadNotif();
if (!unreadNotifs.equals("0")) {
notifTextView.setText(unreadNotifs);
} else {
notifTextView.setVisibility(View.GONE);
}
item.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m.performIdentifierAction(item.getItemId(), 0);
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_user) {
Intent i = new Intent(this, UserProfile.class);
i.putExtra("user", user);
i.putExtra("userId", userId);
startActivity(i);
return true;
}
if (id == R.id.action_notifications) {
Intent i = new Intent(this, Notifications.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean isFacebookLoggedIn() {
Session session = Session.getActiveSession();
if (session != null) {
// Session can be open, check for valid token
if (!session.isClosed()) {
if (!session.getAccessToken().equalsIgnoreCase("")) {
return true;
}
}
}
return false;
}
// FB Login Session and user info
public void showLogin() {
Intent i = new Intent(this, LoginActivity.class);
startActivity(i);
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
appIsOn = true;
invalidateOptionsMenu();
if (!isOnline()) {
showGpsButton();
}
SharedPreferences preferences = getSharedPreferences("USERINFO",
Context.MODE_PRIVATE);
user = preferences.getString("name", null);
userId = preferences.getString("userId", null);
userId = preferences.getString("userId", null);
GCMActivity gcm = new GCMActivity(this);
gcm.registerIfNeeded();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
appIsOn = false;
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
datasource.deleteAllRows();
}
public void showGpsButton() {
LinearLayout buttonLayout = (LinearLayout) findViewById(R.id.gpsWarning);
buttonLayout.setVisibility(View.VISIBLE);
buttonLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent settingsIntent = new Intent(
Settings.ACTION_DATA_ROAMING_SETTINGS);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(settingsIntent);
finish();
}
});
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
}
我在KitKat上也遇到了类似的问题。但在Lollipop上似乎很有效。不管怎样,我们必须支持许多OS版本,对吗?:)
主要原因似乎是新活动的开始迫使MainActivity
在完全完成创建之前暂停。当它暂停时,它立即保存实例状态。但是由于fragment
实例是在一个奇怪的主线程结束循环中提交的,所以启动-暂停-保存循环变得混乱,导致了这次崩溃。在某些情况下,这可能是一种竞争条件,并偶尔发生。
下面是一些经过反复测试后似乎可以修复它的代码:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Handler hanlder = new Handler();
hanlder.post(new Runnable() {
@Override
public void run() {
if (isFacebookLoggedIn()) {
System.out.println("We are logged in!");
} else {
showLogin();
}
if (datasource.getAllImageItems().isEmpty()) {
Intent splash = new Intent(MainActivity.this, SplashActivity.class);
startActivity(splash);
}
}
});
}
null java.lang.IllegalStateException:在onSaveInstanceState e/androidRuntime(9008):at android.support.v4.app.fragmentManagerImpl.checkStateLoss(fragmentManager.java:1354)e/androidRuntime(9008):at androi
问题内容: 我从市场上的应用程序中获取用户报告,但出现以下异常: 显然,它与FragmentManager有关,我不使用。stacktrace不显示我自己的任何类,因此我不知道发生此异常的位置以及如何防止它。 作为记录:我有一个tabhost,在每个选项卡中都有一个ActivityGroup在Activity之间切换。 问题答案: 基本上我只需要: 不要在方法上进行调用。这太糟了… 这是支持包中的
问题内容: 我是Android开发的新手。我有一个问题。我尝试了最后几个小时,但我无法弄清楚。如果是这样,我有一个很普遍的问题。IllegalStateException:使用ViewPager在onSaveInstanceState之后无法执行此操作,但由于缺少Android开发经验而失败。 这是代码: 这是错误: 位于android.os.Handler.dispatchMessage(Han
问题内容: Android中的异步回调 试图在Android上以可靠的方式执行异步操作是不必要的麻烦,即AsyncTask确实在概念上存在缺陷吗?还是我只是缺少一些东西? 现在,一切都在引入片段之前。随着Fragments的引入,onRetainNonConfigurationInstance()已被弃用。因此,最新的Google宽容黑客手段是使用永久性的非UI片段,该片段会在配置发生变化(例如,
我是Android开发的新手。我有个问题。我试了几个小时,但我想不出来。如果是这样的话,我有一个很受欢迎的问题。IllegalStateException:在使用ViewPager onSaveInstanceState之后无法执行此操作,但由于缺乏Android开发经验而失败。 下面是代码:
我在下面的manager.popbackbackstack行中遇到此错误。有办法解决这个问题吗?事情发生得很顺利。 致命异常:java.lang.IllegalStateException:在android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2044)在android.support.v4.