Splash Screen Causes MenuItems not to appear

2019-03-04 03:24发布

问题:

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);
   }
};    

}

回答1:

Create a new activity which extends the Android Activity class, and place your menu handling in there. Then, extend the new activity in your other activities - thus ensuring that the menu handling is consistent. For lists, you could create a second new activity which extends ListActivity, or grab the ListActivity code and just make that extend your previous activity with the menus.



回答2:

In Splash 2 put

SetContentView(R.layout.country_list);

just below super.onCreate(savedInstanceState);