2015-11-01 3 views
1

Я пытаюсь показать ActionBar на одном из моих действий, но я не могу понять, почему он не будет отображаться.
Может кто-нибудь помочь, пожалуйста?Мое приложение не будет показывать ActionBar

активность:

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> 
+0

Вы уверены, что вы не используете тему .NoActionBar? –

+0

Пожалуйста, см. Мое обновление, я включил мою тему –

+0

@FrankN. Stein Это не позволило мне продлить это, поэтому я расширил его до ActionBarActivity, и он работал благодаря –

ответ

1

Вот почему: public class Login extends Activity {
Вы должны простираться от AppCompatActivity.

Not ActionBarActivity, так как это deprecated.

1

просто изменить

public class Login extends Activity { 

    } 
    public class Login extends AppCompatActivity { 
    } 
Смежные вопросы