我正在尝试创建两个场景并在它们之间添加过渡。但是当我通过调用方法Scene.getSceneForLayout()
初始化场景时,会发生运行时错误。 Logcat错误:
(无法找到方法android.transition.Scene.getSceneForLayout,从方法com.example.practice.transition.Transition.onCreate中引用
)
我关注http://developer.android.com/training/transitions/scenes.html。
package com.example.practice.transition;
import android.app.Activity;
import android.os.Bundle;
import android.transition.Fade;
import android.transition.Scene;
import android.transition.TransitionManager;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.practice.R;
public class Transition extends Activity {
Scene a_scene,another_scene,ending_scene;
ViewGroup root_scene;
Fade fade_transition;
Button fade_me;
TransitionManager TM;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.transition);
root_scene = (ViewGroup) findViewById(R.id.scene_root);
a_scene = Scene.getSceneForLayout(root_scene, R.layout.a_scene, this);
another_scene = Scene.getSceneForLayout(root_scene, R.layout.another_scene , this);
ending_scene=another_scene;
fade_transition = new Fade();
}
}
这里是transition.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/master_layout"
>
<TextView
android:id="@+id/title_transition"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
/>
<FrameLayout
android:id="@+id/scene_root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/a_scene" />
</FrameLayout>
<Button
android:id="@+id/fade_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fade Me"
/>
</LinearLayout>
a_scene.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scene_container">
<TextView
android:id="@+id/text_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Line 1" />
<TextView
android:id="@+id/text_view2"
android:layout_below="@id/text_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Line 2" />
</RelativeLayout>
another_scene.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scene_container">
<TextView
android:id="@+id/text_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Line 2"/>
<TextView
android:id="@+id/text_view1"
android:layout_width="match_parent"
android:layout_below="@id/text_view2"
android:layout_height="wrap_content"
android:text="Text Line 1" />
</RelativeLayout>
Logcat错误:
05-01 01:18:15.906: I/dalvikvm(31801): Could not find method
android.transition.Scene.getSceneForLayout, referenced from method com.example.practice.transition.Transition.onCreate
05-01 01:18:15.906: W/dalvikvm(31801): VFY: unable to resolve static method 11314: Landroid/transition/Scene;.getSceneForLayout (Landroid/view/ViewGroup;ILandroid/content/Context;)Landroid/transition/Scene;
05-01 01:18:15.906: D/dalvikvm(31801): VFY: replacing opcode 0x71 at 0x0018
05-01 01:18:15.906: D/dalvikvm(31801): DexOpt: unable to opt direct call 0x2c31 at 0x2f in Lcom/example/practice/transition/Transition;.onCreate
05-01 01:18:15.976: E/MoreInfoHPW_ViewGroup(31801): Parent view is not a TextView
05-01 01:18:16.006: D/AndroidRuntime(31801): Shutting down VM
05-01 01:18:16.006: W/dalvikvm(31801): threadid=1: thread exiting with uncaught exception (group=0x416b3ac8)
05-01 01:18:16.006: E/AndroidRuntime(31801): FATAL EXCEPTION: main
05-01 01:18:16.006: E/AndroidRuntime(31801): java.lang.NoClassDefFoundError: android.transition.Scene
05-01 01:18:16.006: E/AndroidRuntime(31801): at com.example.practice.transition.Transition.onCreate(Transition.java:28)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.Activity.performCreate(Activity.java:5250)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.ActivityThread.access$700(ActivityThread.java:152)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.os.Looper.loop(Looper.java:137)
05-01 01:18:16.006: E/AndroidRuntime(31801): at android.app.ActivityThread.main(ActivityThread.java:5328)
05-01 01:18:16.006: E/AndroidRuntime(31801): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 01:18:16.006: E/AndroidRuntime(31801): at java.lang.reflect.Method.invoke(Method.java:511)
05-01 01:18:16.006: E/AndroidRuntime(31801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-01 01:18:16.006: E/AndroidRuntime(31801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-01 01:18:16.006: E/AndroidRuntime(31801): at dalvik.system.NativeStart.main(Native Method)
05-01 01:23:24.597: I/Process(31801): Sending signal. PID: 31801 SIG: 9
答案 0 :(得分:1)
main.java
public class MainActivity extends Activity {
Scene a_scene,another_scene,ending_scene;
ViewGroup root_scene;
Fade fade_transition;
Button fade_me;
TransitionManager TM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root_scene = (ViewGroup) findViewById(R.id.scene_root);
a_scene = Scene.getSceneForLayout(root_scene, R.layout.a_scene, this);
another_scene = Scene.getSceneForLayout(root_scene, R.layout.another_scene , this);
ending_scene=another_scene;
fade_transition = new Fade();
}
public void doTransition(View v){
Log.d("clicked", "clicked");
TransitionManager.go(ending_scene, fade_transition);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/master_layout"
>
<TextView
android:id="@+id/title_transition"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
/>
<FrameLayout
android:id="@+id/scene_root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/a_scene" />
</FrameLayout>
<Button
android:id="@+id/fade_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fade Me"
android:onClick="doTransition"
/>
</LinearLayout>
a_scene.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scene_container">
<TextView
android:id="@+id/text_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Line 1" />
<TextView
android:id="@+id/text_view2"
android:layout_below="@id/text_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Line 2" />
</RelativeLayout>
enter code here
another_scene.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scene_container">
<TextView
android:id="@+id/text_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Line 2"/>
<TextView
android:id="@+id/text_view1"
android:layout_width="match_parent"
android:layout_below="@id/text_view2"
android:layout_height="wrap_content"
android:text="Text Line 1" />
</RelativeLayout>
enter code here