尝试构建共享元素棒棒糖转换时,转换名称为null

时间:2015-01-31 15:08:32

标签: android android-5.0-lollipop android-transitions

我正在尝试在我的应用中创建共享元素转换。我在activity1中有一个listview,在我列表的每个元素中都有一个图像视图。当用户单击列表中的项目时,此图像也存在于第二个activity2中。

问题如下:

 java.lang.IllegalArgumentException: Shared element name must not be null
             at android.app.ActivityOptions.makeSceneTransitionAnimation(ActivityOptions.java:470)
             at android.app.ActivityOptions.makeSceneTransitionAnimation(ActivityOptions.java:431)
             at android.support.v4.app.ActivityOptionsCompat21.makeSceneTransitionAnimation(ActivityOptionsCompat21.java:31)
             at android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAnimation(ActivityOptionsCompat.java:134)
             at com.frisbeeeapp.frisbeee.main_fragment.Main_All_Annonces$6$2.onItemClick(Main_All_Annonces.java:417)
             at android.widget.AdapterView.performItemClick(AdapterView.java:300)
             at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
             at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
             at android.widget.AbsListView$3.run(AbsListView.java:3833)
             at android.os.Handler.handleCallback(Handler.java:739)
             at android.os.Handler.dispatchMessage(Handler.java:95)
             at android.os.Looper.loop(Looper.java:135)
             at android.app.ActivityThread.main(ActivityThread.java:5221)
             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:899)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

change_image_transform.xml:

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

在activity1的布局中声明的imageview的摘录:

<ImageView
    android:id="@+id/allannonces_image"
    android:transitionName="transitionImg"
    android:layout_width="fill_parent"
    android:layout_height="270dp"
    android:scaleType="centerCrop"
    android:layout_below="@+id/allannonces_imageuser"
    android:layout_marginTop="18dp"/>

以下是activity1中的代码:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Annonce currentAnnonce = (Annonce) list.getAdapter().getItem(position);
        String titre = currentAnnonce.getTitle();
        String cat = currentAnnonce.getCategorie();
        String desc = currentAnnonce.getDescription();
        String etat = currentAnnonce.getEtat();
        String img = currentAnnonce.getImage();
        String prix = currentAnnonce.getPrix();
        String villeObj = currentAnnonce.getVilleObjet();
        String codePostal = currentAnnonce.getCode_postal();
        String departement = currentAnnonce.getDepartement();
        String avatarUser = currentAnnonce.getImageUser();
        String pseudoUser = currentAnnonce.getPseudoUser();
        String dateDebut = currentAnnonce.getDate_debut();
        String idAnnonce = currentAnnonce.getId();
        String emailUser = currentAnnonce.getEmailUser();
        String partel = currentAnnonce.getPartel();
        String parmail = currentAnnonce.getParmail();

        Intent myIntent = new Intent(Main_All_Annonces.this, Detail_annonce.class);
        myIntent.putExtra("titre", titre);
        myIntent.putExtra("cat", cat);
        myIntent.putExtra("desc", desc);
        myIntent.putExtra("etat", etat);
        myIntent.putExtra("img", img);
        myIntent.putExtra("prix", prix);
        myIntent.putExtra("villeObj", villeObj);
        myIntent.putExtra("codePostal", codePostal);
        myIntent.putExtra("departement", departement);
        myIntent.putExtra("avatarUser", avatarUser);
        myIntent.putExtra("pseudoUser", pseudoUser);
        myIntent.putExtra("dateDebut", dateDebut);
        myIntent.putExtra("idAnnonce", idAnnonce);
        myIntent.putExtra("emailUser", emailUser);
        myIntent.putExtra("partel", partel);
        myIntent.putExtra("parmail", parmail);

        myIntent.putExtra("parentAct", "AllAnnonces");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            String transitionName = (String) view.getTag(R.id.allannonces_image);
            ActivityOptionsCompat options =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(
                            Main_All_Annonces.this,
                            view,           // The view which starts the transition
                            transitionName  // The transitionName of the view we’re transitioning to
                    );

            // put more extras in the intent if you want, like the object clicked
            myIntent.putExtra("EXTRA_IMAGE_TRANSITION_NAME", transitionName);
            ActivityCompat.startActivity(Main_All_Annonces.this, myIntent, options.toBundle());
        }
        else {
            // Code to run on older devices
            startActivity(myIntent);
            overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
        }
    }
});

活动2的onCreate里面的代码:

ViewCompat.setTransitionName(image_txt, getIntent().getStringExtra("EXTRA_IMAGE_TRANSITION_NAME"));
        imageLoader.displayImage(img, image_txt);

在activity2的布局中声明的imageview的摘录:

    <ImageView
         android:id="@+id/detail_annonce_image"
         android:transitionName="transitionImg"
         android:layout_width="fill_parent"
         android:layout_height="280dp"
         android:scaleType="centerCrop"
         android:src="@drawable/cover1" />

1 个答案:

答案 0 :(得分:3)

确保两个xml布局中的两个ImageView都具有相同的android:transitionName值集,否则您确实会收到该错误。