无法在onClick()方法

时间:2016-12-06 16:02:54

标签: android onclick onclicklistener

我正在努力实现从一项活动到另一项活动的可分享过渡 这是我的主要活动代码

 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    if(Build.VERSION.SDK_INT>=21){
        TransitionInflater ti=TransitionInflater.from(this);
        Transition t=ti.inflateTransition(R.transition.transition);
        getWindow().setEnterTransition(t);
        getWindow().setExitTransition(t);
    }
    setContentView(R.layout.activity_main);
    Button b=(Button)  findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.setTransitionName("test");
            ActivityOptionsCompat c=ActivityOptionsCompat.makeSceneTransitionAnimation(this,view.getTransitionName());
        }
    });
}

和第二个活动代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(Build.VERSION.SDK_INT>=21){

        Transition i=TransitionInflater.from(this).inflateTransition(R.transition.transition);
        getWindow().setEnterTransition(i);
        getWindow().setExitTransition(i);
    }
    setContentView(R.layout.b);

}

我收到错误

 error: incompatible types: <anonymous OnClickListener> cannot be converted to Activity
            ActivityOptionsCompat c=ActivityOptionsCompat.makeSceneTransitionAnimation(this,view.getTransitionName());

在style.xml中启用当前窗口转换

 <item name="android:windowContentTransitions">true</item>

并在res文件夹中创建了transation xml文件

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<slide/>
<changeBounds/>
</transitionSet>

在所有教程中都有效,但是当我尝试它不起作用时 为什么会发生这种情况以及它的修复方法

它显示的错误是

Wrong 2nd argument type.ound:'java.lang.String',required:android.support.v4.util.Pair<android.view.View,java.lang.String>makeSceneTransitionAnimation(Activity,android.support.v4.util.Pair<android.view.View,java.lang.String>...)in ActivityOptionsCompat cannot be applied to (MainActivity,java.lang.String)

3 个答案:

答案 0 :(得分:0)

尝试更改

ActivityOptionsCompat c=ActivityOptionsCompat.makeSceneTransitionAnimation(this,view.getTransitionName());

ActivityOptionsCompat c=ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),view.getTransitionName());

答案 1 :(得分:0)

尝试替换

ActivityOptionsCompat c = ActivityOptionsCompat.makeSceneTransitionAnimation(this,view.getTransitionName());

ActivityOptionsCompat c = ActivityOptionsCompat.makeSceneTransitionAnimation(YOUR_ACTIVITY.this,view.getTransitionName());

例如MainActivity.this

UPD

查看documentationmakeSceneTransitionAnimation应该是这样的:

  

makeSceneTransitionAnimation(活动活动,                   查看sharedElement,                   String sharedElementName)

答案 2 :(得分:0)

Vasily Kabunov的答案为我工作。我尝试使用上下文变量,但我没有工作。当我替换df = pd.DataFrame({'a': [1,1,2,2,3,3,4,4], 'b': [1,0,1,0,1,0,1,0], 'c': [1.5,2.5,30,20,100,150,0.7,0.3]}) 时 与Intent intent = new Intent(context, AddNodeActivity.class); 它奏效了。