【问题标题】:Splash Screen Causes MenuItems not to appear启动画面导致 MenuItems 不出现
【发布时间】:2013-03-19 00:21:12
【问题描述】:

我已经构建了一个启动屏幕,它加载(启动)活动,然后启动另一个活动,它工作正常。 (我已将其附在下面 - 它称为 SPLASH 1)

我创建了另一个启动屏幕来替换这个应该只运行一次的启动屏幕 - 然后在创建 SharedPreferences 布尔值之后它应该加载另一个活动。一切似乎都很好,但现在当它加载新活动时,没有任何菜单项出现。我不知道 SPLASH 2 中发生了什么变化 - 但其中的某些内容导致 MenuItems 在加载与 SPLASH 1 完全相同的活动 (NEWCORE.JAVA) 后不出现 (NEWCORE.JAVA)

我真的不确定这里发生了什么 - 非常感谢任何帮助!

(如果需要任何其他信息,请告诉我)

SPLASH 1.(工作)

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(不工作 - 导致菜单项不会出现在它加载的活动中)

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(由两个启动画面连接 - 使用 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);
   }
};    

}

【问题讨论】:

    标签: java android sharedpreferences menuitem splash-screen


    【解决方案1】:

    创建一个扩展 Android Activity 类的新 Activity,并将您的菜单处理放在其中。然后,在您的其他活动中扩展新活动 - 从而确保菜单处理是一致的。对于列表,您可以创建第二个扩展 ListActivity 的新活动,或者获取 ListActivity 代码并使用菜单扩展您之前的活动。

    【讨论】:

    • 好的 - 它看起来像这样吗? docs.google.com/document/d/…如果这是正确的 - 你能告诉我如何在我的其他活动中扩展它吗? (我对 android 开发还很陌生——我很抱歉)
    • 为什么第二个闪屏会导致它们不可见? (我需要在每个活动上使用不同的 MenuItems - 所以我不确定是否将它们全部存储在一个地方会起作用。我想在每个活动中定义它们。是否可以编辑 SPLASH SCREEN 2 来调用定义的 MenuItems在 NEWCORE.JAVA 中?)
    • 直接通过第一个初始屏幕加载 NEWCORE.JAVA 允许出现菜单项,这并不奇怪 - 但是在使用第二个初始屏幕加载 NEWCORE.JAVA 时(它只是选择 NEWCORE.JAVA如果 SharedPreference 为真)导致它们在调用完全相同的活动时不可见(NEWCORE.JAVA)
    • 谁能帮我弄清楚为什么菜单项将不再出现? (我知道已经提供了一种解决方法 - 但我宁愿在每个活动中定义菜单项 [就像我现在所拥有的那样],而不是在一个单独的活动中控制其他活动)
    【解决方案2】:

    在 Splash 2 中放置

    SetContentView(R.layout.country_list);
    

    就在下面 super.onCreate(savedInstanceState);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 2022-01-14
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多