片段类Android上的setSupportActionBar

时间:2016-04-17 18:47:17

标签: android toolbar android-toolbar

我想为我的类添加一个工具栏来处理片段之间的通信。

angular.module('starter', ['ionic'])

.config(function($ionicConfigProvider) {
  $ionicConfigProvider.navBar.alignTitle('center');  //align title in center
})

.controller('ControllerOne',[ '$scope', 'freshlyPressed', Ctrl])

.service('freshlyPressed', ['$http','$log', freshlyPressed]);

function Ctrl($scope, freshlyPressed){

  $scope.refreshClicked = function(){
    freshlyPressed.getBlogs($scope);
  }
};

function freshlyPressed($http,$log){
  this.getBlogs = function($scope){
    $http.jsonp("https://public-api.wordpress.com/rest/v1.1/freshly-pressed?callback=JSON_CALLBACK")
    .success(function(result, posts){
      $scope.posts = result.posts;
    });
  };
};

enter image description here

因为ypu可以看到我在setSupportActionbar上遇到错误,当我将鼠标悬停在它上面时,我得到public class FragmentClass extends Activity implements Communicator{ @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.content2); Toolbar toolbar = (Toolbar) findViewById(R.id.playerToolbar); setSupportActionBar(toolbar); super.onCreate(savedInstanceState); }

2 个答案:

答案 0 :(得分:2)

您的活动应该延长AppCompactActivity

public class FragmentClass extends AppCompactActivity implements Communicator{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.content2);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
    }
}

此外Toolbar也应从import android.support.v7.widget.Toolbar而不是android.widget.Toolbar

导入

另请注意:您的活动名称不应该FragmentClass造成混淆,因为它没有延伸Fragment。它应该类似于MainActivity

答案 1 :(得分:1)

将AppcomactActivity与Appcomacttheme一起使用。

尝试在android studio中使用新的Basic Activity,代码将自动添加,如AppTheme和AppCompatActivity,Toolbar和FloatingActionButton。

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}