Android I / O 2014演示应用程序问题

时间:2014-07-06 23:52:11

标签: java android xml android-layout

我正在尝试编写一个关于谷歌L预览给我们的新共享元素动画API的句柄。

我一直在尝试使用代码库作为参考点以及:https://developer.android.com/preview/material/animations.html

对于我的生活,我似乎无法让我的应用程序工作。然而,当我从谷歌运行演示应用程序时,转换工作正常。

MyActivity.java:

    public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final ImageView imageView = (ImageView)this.findViewById(R.id.img_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void showPhoto(View view) {
        Intent intent = new Intent();
        intent.setClass(this, MyActivity2.class);

        ImageView hero = (ImageView) ((View) view.getParent()).findViewById(R.id.img_main);
        ((ViewGroup) hero.getParent()).setTransitionGroup(false);

        ActivityOptions options =
                ActivityOptions.makeSceneTransitionAnimation(this, hero, "robot");
        startActivity(intent, options.toBundle());
    }
}

activity_my.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <ImageView
        android:id="@+id/img_main"
        android:viewName="photo1"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/test"
        android:onClick="showPhoto"/>

</RelativeLayout>

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowExitTransitionOverlap">true</item>
        <item name="android:windowActionBarOverlay">false</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

</resources>

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jms_m_000.transitionshit" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyActivity2"
            android:label="@string/title_activity_my_activity2" >
        </activity>
    </application>

</manifest>

我收到的错误:

07-06 18:54:42.153  24145-24145/com.example.jms_m_000.transitionshit
 E/AndroidRuntime﹕FATAL EXCEPTION: main

    Process: com.example.jms_m_000.transitionshit, PID: 24145
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3970)
            at android.view.View.performClick(View.java:4598)
            at android.view.View$PerformClick.run(View.java:19268)
            at android.os.Handler.handleCallback(Handler.java:738)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at rnal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:3965)
 at android.view.View.performClick(View.java:4598)
 at android.view.View$PerformClick.run(View.java:19268)
 at android.os.Handler.handleCallback(Handler.java:738)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5070)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.app.ActivityOptions.toBundle()' on a null object reference
            at com.example.jms_m_000.transitionshit.MyActivity.showPhoto(MyActivity.java:56)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at android.view.View$1.onClick(View.java:3965)
 at android.view.View.performClick(View.java:4598)
 at android.view.View$PerformClick.run(View.java:19268)
 at android.os.Handler.handleCallback(Handler.java:738)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5070)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)

我不确定为什么但是在调用.makeSceneTransitionAnimation() ...

之后选项为空

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

尝试添加

requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

在您活动的onCreate()方法中。

令我惊讶的是,我将选项设为非空。

答案 1 :(得分:1)

通过在第一个Activity中调用setViewName()到'hero'来定义视图名称,并在第二个Activity中为ImageView定义一个视图名称(与makeSceneTransitionAnimation中定义的相同)。这对我有用