问题描述
我已经建立了一个闪屏加载(闪)活动,然后又开始活动,并能正常工作。 (我在下面附了 - 这就是所谓的SPLASH 1)
我创建了另一个闪屏取代它这应该只运行一次 - 然后创建一个共享preferences后
布尔它应该加载另一个活动。一切似乎罚款这一点,但现在,当它加载了新的活动,没有一个菜单项的出现。我不知道在SPLASH 2什么改变 - 但东西在里面是造成的MenuItems
不要出现加载完全相同的活动SPLASH 1做(NEWCORE.JAVA)后
我真的不知道是怎么回事 - 任何帮助是极大的AP preciated
!(请让我知道是否需要任何额外的信息)
SPLASH 1(工作)
进口android.app.Activity;
进口android.os.Bundle;
进口android.os.Handler;
进口android.content.Intent;
进口com.nfc.linkingmanager.R;
公共类闪屏延伸活动{
私人布尔mIsBackButton pressed;
私有静态最终诠释SPLASH_DURATION = 1000;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.splash_screen);
处理程序处理程序=新的处理程序();
// 2秒后运行一个线程来启动主屏幕
handler.postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
//确保我们关闭闪屏,使用户不会回来当presses返回键
完();
如果(!mIsBackButton pressed){
//启动主屏幕,如果后退按钮不是$ P $已经pssed
意向意图=新的意图(SplashScreen.this,NewCore.class);
SplashScreen.this.startActivity(意向);
}
}
},SPLASH_DURATION); //时间在毫秒(1秒= 1000毫秒),直到run()方法将被调用
}
@覆盖
公共无效onBack pressed(){
//设置标志设置为true,以便在未来的活动将无法启动
mIsBackButton pressed = TRUE;
super.onBack pressed();
}
}
SPLASH 2(不工作 - 使菜单项没有出现在它加载的活动)
进口android.app.Activity;
进口android.os.Bundle;
进口android.os.Handler;
进口android.content.Intent;
进口com.nfc.linkingmanager.R;
进口android.content.Shared preferences;
进口的java.lang.Object;
。进口的Android preference preferenceManager。
公共类闪屏扩展活动
{
私人处理程序处理程序=新的处理程序()
{
公共无效的handleMessage(信息MSG)
{
意图I =新的意图(SplashScreen.this,AppActivity.class);
SplashScreen.this.startActivity(ⅰ);
this.finish();
}
};
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
如果(!prefs.getBoolean(FIRST_TIME,FALSE))
{
共享preferences.Editor编辑器= prefs.edit();
editor.putBoolean(FIRST_TIME,真正的);
editor.commit();
意图I =新的意图(SplashScreen.this,NewCore.class);
this.startActivity(ⅰ);
this.finish();
}
其他
{
this.setContentView(R.layout.country_list);
handler.sendEmptyMessageDelayed(0,2000);
}
}
}
NEWCORE.JAVA(连接到由两个闪屏 - 仅使用SPLASH 2时失踪的MenuItems)
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.app.ListActivity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.CursorAdapter;
进口android.widget.ListView;
进口android.widget.SimpleCursorAdapter;
进口android.widget.AdapterView.OnItemClickListener;
公共类NewCore扩展ListActivity {
公共静态最后弦乐ROW_ID =ROW_ID;
私人的ListView conListView;
私人的CursorAdapter conAdapter;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
conListView = getListView();
conListView.setOnItemClickListener(viewConListener);
//每名映射到一个TextView
的String []从=新的String [] {姓名};
INT []到=新INT [] {R.id.countryTextView};
conAdapter =新SimpleCursorAdapter(NewCore.this,R.layout.country_list,空,从,到);
setListAdapter(conAdapter); //设置适配器
}
@覆盖
保护无效onResume()
{
super.onResume();
新GetContacts()执行((对象[])NULL);
}
@覆盖
保护无效的onStop()
{
光标光标= conAdapter.getCursor();
如果(光标!= NULL)
cursor.deactivate();
conAdapter.changeCursor(空);
super.onStop();
}
私有类GetContacts扩展的AsyncTask<对象,对象,光标>
{
DatabaseConnector dbConnector =新DatabaseConnector(NewCore.this);
@覆盖
受保护的光标doInBackground(对象... PARAMS)
{
dbConnector.open();
返回dbConnector.getAllContacts();
}
@覆盖
保护无效onPostExecute(光标结果)
{
conAdapter.changeCursor(结果); //设置适配器的游标
dbConnector.close();
}
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
super.onCreateOptionsMenu(菜单);
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.country_menu,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项)
{
意图的addContact =新的意图(NewCore.this,NewCore.class);
startActivity(的addContact);
返回super.onOptionsItemSelected(项目);
}
OnItemClickListener viewConListener =新OnItemClickListener()
{
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3)
{
意图viewCon =新的意图(NewCore.this,NewCore.class);
viewCon.putExtra(ROW_ID,ARG3);
startActivity(viewCon);
}
};
}
在开机2将
的setContentView(R.layout.country_list);
正下方 super.onCreate(savedInstanceState);
I've built a splash screen that loads a (splash) activity and then starts another activity and it works fine. (I've attached it below - it's called SPLASH 1)
I created another splash screen to replace this one which is supposed to only run once - then after creating a SharedPreferences
boolean it is supposed to load another activity. Everything seems fine with this but now when it loads the new activity, none of the menuitems appear. I have no idea what changed in SPLASH 2 - but something in there is causing the MenuItems
not to appear after it loads the exact same activity SPLASH 1 does (NEWCORE.JAVA)
I'm really not sure what is going on here - any help is greatly appreciated!
(please let me know if any additional info is needed)
SPLASH 1. (WORKING)
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import com.nfc.linkingmanager.R;
public class SplashScreen extends Activity {
private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 1000;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
@Override
public void run() {
// make sure we close the splash screen so the user won't come back when it presses back key
finish();
if (!mIsBackButtonPressed) {
// start the home screen if the back button wasn't pressed already
Intent intent = new Intent(SplashScreen.this, NewCore.class);
SplashScreen.this.startActivity(intent);
}
}
}, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
@Override
public void onBackPressed() {
// set the flag to true so the next activity won't start up
mIsBackButtonPressed = true;
super.onBackPressed();
}
}
SPLASH 2 (NOT WORKING - CAUSES MENUITEMS not to appear on the activity it loads)
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import com.nfc.linkingmanager.R;
import android.content.SharedPreferences;
import java.lang.Object;
import android.preference.PreferenceManager;
public class SplashScreen extends Activity
{
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
Intent i = new Intent(SplashScreen.this, AppActivity.class);
SplashScreen.this.startActivity(i);
this.finish();
}
};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
Intent i = new Intent(SplashScreen.this, NewCore.class);
this.startActivity(i);
this.finish();
}
else
{
this.setContentView(R.layout.country_list);
handler.sendEmptyMessageDelayed(0, 2000);
}
}
}
NEWCORE.JAVA (Connected to by both Splash Screens - Only missing MenuItems when using SPLASH 2)
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class NewCore extends ListActivity {
public static final String ROW_ID = "row_id";
private ListView conListView;
private CursorAdapter conAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
conListView=getListView();
conListView.setOnItemClickListener(viewConListener);
// map each name to a TextView
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.countryTextView };
conAdapter = new SimpleCursorAdapter(NewCore.this, R.layout.country_list, null, from, to);
setListAdapter(conAdapter); // set adapter
}
@Override
protected void onResume()
{
super.onResume();
new GetContacts().execute((Object[]) null);
}
@Override
protected void onStop()
{
Cursor cursor = conAdapter.getCursor();
if (cursor != null)
cursor.deactivate();
conAdapter.changeCursor(null);
super.onStop();
}
private class GetContacts extends AsyncTask<Object, Object, Cursor>
{
DatabaseConnector dbConnector = new DatabaseConnector(NewCore.this);
@Override
protected Cursor doInBackground(Object... params)
{
dbConnector.open();
return dbConnector.getAllContacts();
}
@Override
protected void onPostExecute(Cursor result)
{
conAdapter.changeCursor(result); // set the adapter's Cursor
dbConnector.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.country_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent addContact = new Intent(NewCore.this, NewCore.class);
startActivity(addContact);
return super.onOptionsItemSelected(item);
}
OnItemClickListener viewConListener = new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Intent viewCon = new Intent(NewCore.this, NewCore.class);
viewCon.putExtra(ROW_ID, arg3);
startActivity(viewCon);
}
};
}
In Splash 2 put
SetContentView(R.layout.country_list);
just belowsuper.onCreate(savedInstanceState);
这篇关于闪屏导致的MenuItems不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!