2013-03-19 291 views
0

我已經構建了一個啓動屏幕來加載(啓動)活動,然後啓動另一個活動,它工作正常。 (我在下面附加 - 它叫做SPLASH 1)初始屏幕導致MenuItems不出現

我創建了另一個啓動畫面來替換這個應該只運行一次的閃屏 - 然後在創建一個SharedPreferences布爾值後,它應該加載另一個活動。一切似乎都很好,但現在當它加載新的活動時,沒有任何菜單項出現。我不知道在SPLASH 2中發生了什麼變化 - 但是有些東西會導致MenuItems在加載完全相同的活動SPLASH 1後無法顯示(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 -

(由兩個閃屏僅連接到使用飛濺2時丟失的MenuItems)
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); 
    } 
};  

} 

回答

0

創建一個擴展Android Activity類的新活動,並將您的菜單處理放在那裏。然後,將其他活動中的新活動擴展 - 從而確保菜單處理一致。對於列表,您可以創建第二個新活動來擴展ListActivity,或者獲取ListActivity代碼,並使用菜單擴展先前的活動。

+0

確定 - 它會是這個樣子? https://docs.google.com/document/d/18dHDs38F-Broy9DmNl80omanC7WZjYmXQoqTj1zTiMU/edit P.S. 如果這是正確的 - 你能告訴我怎樣才能將它擴展到我的其他活動中嗎? (我很新的android開發 - 我很抱歉) – NoobNinja 2013-03-19 00:44:38

+0

爲什麼第二個啓動畫面導致它們不可見? (我需要在每個活動上使用不同的MenuItems - 所以我不確定是否將它們全部存儲在一個地方都可以工作,我希望在每個活動中都定義它們。是否可以編輯SPLASH SCREEN 2來調用MenuItems定義的在NEWCORE.JAVA?) – NoobNinja 2013-03-19 00:53:12

+0

奇怪的是,通過第一個啓動畫面直接加載NEWCORE.JAVA允許菜單項出現 - 但是當使用第二啓動畫面加載NEWCORE.JAVA(它只是選擇NEWCORE.JAVA if SharedPreference是真實的)導致它們在調用完全相同的活動時不可見(NEWCORE.JAVA) – NoobNinja 2013-03-19 01:20:22

0

在飛濺2把

SetContentView(R.layout.country_list); 

略低於 super.onCreate(savedInstanceState);