0

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

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

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

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

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

}
4

2 回答 2

0

在 Splash 2 中

SetContentView(R.layout.country_list);

略低于 super.onCreate(savedInstanceState);

于 2013-03-19T07:10:04.100 回答
0

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

于 2013-03-19T00:35:05.247 回答