android - 我的应用程序不会显示 ActionBar

标签 android android-actionbar android-theme

我试图在我的一个 Activity 上显示一个 ActionBar,但我似乎无法弄清楚为什么它不显示。
有人可以帮忙吗?

Activity :

public class Login extends Activity {
    public static final String TAG = "MainActivity";    
    public static final String DEFAULT_CHAT_NAME = "";
    private WifiP2pManager mManager;
    private Channel mChannel;
    private WifiDirectBroadcastReceiver mReceiver;
    private IntentFilter mIntentFilter;
    private com.ajapps.app.floatingactionbutton.AddFloatingActionButton goToChat;
    private ImageView goToSettings;
    private TextView goToSettingsText;
    private TextView setChatNameLabel;
    private EditText setChatName;
    private ImageView disconnect;
    public static String chatName;
    public static ServerInit server;

    //Getters and Setters
    public WifiP2pManager getmManager() { return mManager; }
    public Channel getmChannel() { return mChannel; }
    public WifiDirectBroadcastReceiver getmReceiver() { return mReceiver; }
    public IntentFilter getmIntentFilter() { return mIntentFilter; }
    public com.ajapps.app.floatingactionbutton.AddFloatingActionButton getGoToChat(){ return goToChat; }
    public TextView getSetChatNameLabel() { return setChatNameLabel; }
    public ImageView getGoToSettings() { return goToSettings; }
    public EditText getSetChatName() { return setChatName; }
    public TextView getGoToSettingsText() { return goToSettingsText; }
    public ImageView getDisconnect() { return disconnect; }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //Init the Channel, Intent filter and Broadcast receiver
        init();

        //Button Go to Settings
        goToSettings = (ImageView) findViewById(R.id.goToSettings);
        goToSettings();

        //Go to Settings text
        goToSettingsText = (TextView) findViewById(R.id.textGoToSettings);

        //Button Go to Chat
        goToChat = (com.ajapps.app.floatingactionbutton.AddFloatingActionButton) findViewById(R.id.goToChat);
        goToChat();

        //Set the chat name
        setChatName = (EditText) findViewById(R.id.setChatName);
        setChatNameLabel = (TextView) findViewById(R.id.setChatNameLabel);
        setChatName.setText(loadChatName(this));

        //Button Disconnect
        disconnect = (ImageView) findViewById(R.id.disconnect);
        disconnect();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        registerReceiver(mReceiver, mIntentFilter);

        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

            @Override
            public void onSuccess() {
                Log.v(TAG, "Discovery process succeeded");
            }

            @Override
            public void onFailure(int reason) {
                Log.v(TAG, "Discovery process failed");
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        int idItem = item.getItemId();
        return super.onOptionsItemSelected(item);
    }

    public void init(){
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        mReceiver = WifiDirectBroadcastReceiver.createInstance();
        mReceiver.setmManager(mManager);
        mReceiver.setmChannel(mChannel);
        mReceiver.setmActivity(this);

        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    }

    public void goToChat(){
        goToChat.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(!setChatName.getText().toString().equals("")){
                    //Set the chat name
                    saveChatName(Login.this, setChatName.getText().toString());
                    chatName = loadChatName(Login.this);

                    //Start the init process
                    if(mReceiver.isGroupeOwner() ==  WifiDirectBroadcastReceiver.IS_OWNER){
                        Toast.makeText(Login.this, "I'm the group owner  " + mReceiver.getOwnerAddr().getHostAddress(), Toast.LENGTH_SHORT).show();
                        server = new ServerInit();
                        server.start();
                    }
                    else if(mReceiver.isGroupeOwner() ==  WifiDirectBroadcastReceiver.IS_CLIENT){
                        Toast.makeText(Login.this, "I'm the client", Toast.LENGTH_SHORT).show();
                        ClientInit client = new ClientInit(mReceiver.getOwnerAddr());
                        client.start();
                    }

                    //Open the ChatActivity
                    Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(Login.this, "Please enter a chat name", Toast.LENGTH_SHORT).show();
                }                   
            }
        });     
    }

    public void disconnect(){
        disconnect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mManager.removeGroup(mChannel, null);
                finish();
            }
        });     
    }

    public void goToSettings(){     
        goToSettings.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //Open Wifi settings
                startActivityForResult(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS), 0);
            }
        });     
    }

    //Save the chat name to SharedPreferences
    public void saveChatName(Context context, String chatName) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        Editor edit = prefs.edit();
        edit.putString("chatName", chatName);
        edit.commit();
    }

    //Retrieve the chat name from SharedPreferences
    public String loadChatName(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString("chatName", DEFAULT_CHAT_NAME);
    }
}

还有我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ajapps.wifichat.MainActivity">

    <!-- Go to Settings screen -->

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <ImageView 
            android:id="@+id/goToSettings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/go_to_settings_button"
            android:src="@drawable/wifi_icon"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/textGoToSettings"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="@string/go_to_settings_text"
            android:textSize="16sp"
            android:textColor="@color/red"
            android:gravity="center"
            android:layout_gravity="center_vertical"/>

    </LinearLayout>


    <!-- Go to Chat screen -->

    <ImageView 
        android:id="@+id/disconnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/setChatNameLabel"
        android:contentDescription="@string/disconnect"
        android:src="@drawable/disconnect"
        android:layout_marginBottom="15dp"
        android:visibility="gone"
        android:layout_centerHorizontal="true"/>

    <TextView 
        android:id="@id/setChatNameLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/setChatName"
        android:text="@string/set_chat_name"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:textColor="@color/app_text"
        android:visibility="gone" />

    <com.ajapps.app.floatingactionbutton.AddFloatingActionButton
        android:id="@+id/goToChat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fab:fab_icon="@drawable/send"
        android:background="@drawable/button_pressed2"
        fab:fab_plusIconColor="@color/half_black"
        fab:fab_colorNormal="@color/red"
        fab:fab_colorPressed="@color/red_pressed"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:visibility="gone" />

    <EditText
        android:id="@+id/setChatName"
        android:inputType="textCapWords"
        android:lines="1"
        android:maxLength="20"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/edit_text"
        android:layout_marginTop="15dp"
        android:visibility="gone" />

</RelativeLayout>

我查看了我的 style.xml,看不到任何会影响它的东西。

这是我的主题:

<resources>

    <style name="AppTheme"
        parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <item name="android:icon">@android:color/transparent</item>
        <item name="windowActionBar">true</item>
        <item name="android:textColorPrimary">#000000</item>
    </style>

    <style name="Match">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="Wrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="MatchWidth" parent="Match">
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="ScrollView" parent="Match">
        <item name="android:fillViewport">true</item>
    </style>

    <style name="LinearLayout" parent="Match">
        <item name="android:orientation">vertical</item>
    </style>

    <style name="RobotoTextView" parent="Wrap">
        <item name="android:textColor">@color/green_light</item>
        <item name="android:layout_gravity">center</item>
    </style>

    <style name="RobotoTextView.Number">
        <item name="android:textSize">@dimen/text_size_fragment_number</item>
    </style>

    <style name="RobotoTextView.Fragment">
        <item name="android:textSize">@dimen/text_size_xlarge</item>
    </style>

    <style name="menu_labels_style">
        <item name="android:background">@drawable/fab_label_background</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:colorPrimaryDark">@color/orange</item>
    </style>

</resources>

最佳答案

只是改变

   public class Login extends Activity {

   }
   public class Login extends AppCompatActivity {
   }

关于android - 我的应用程序不会显示 ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463676/

相关文章:

android - RecyclerView : Animate item resize

android - Google Play(小米盒子)安卓电视应用兼容

java - Android:在我的应用程序拍摄的照片上打印日期和时间

java - Android 操作栏覆盖并默认隐藏在 xml 中

android - 方法调用 'setTitle' 可能产生 'java.lang.NullpointerException'

android - (AppCompat) 没有 UI 的 Activity

android - 如何将自定义样式应用于 SwitchCompat

安卓媒体播放器 : IllegalStateException when app closes/press back button

android - 当我返回到“Activity ”时,空间为空(强制使用软键盘)

android - 文本外观在我的主题中不起作用