overridePendingTransition在适配器(android)中不起作用

时间:2015-06-19 06:58:09

标签: java android eclipse

我想在Adapter使用动画中运行新的Activity,但我有一些错误

06-19 09:54:29.253: E/AndroidRuntime(11464): FATAL EXCEPTION: main
06-19 09:54:29.253: E/AndroidRuntime(11464):
java.lang.ClassCastException: android.app.Application cannot be cast
 to android.app.Activity 06-19 09:54:29.253: E/AndroidRuntime(11464):
    at apdapters.NewsAdapter$3.onClick(NewsAdapter.java:134) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.view.View.performClick(View.java:3517) 06-19 09:54:29.253:
 E/AndroidRuntime(11464):   at
 android.view.View$PerformClick.run(View.java:14155) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.os.Handler.handleCallback(Handler.java:605) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.os.Handler.dispatchMessage(Handler.java:92) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.os.Looper.loop(Looper.java:154) 06-19 09:54:29.253:
 E/AndroidRuntime(11464):   at
 android.app.ActivityThread.main(ActivityThread.java:4624) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 java.lang.reflect.Method.invokeNative(Native Method) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 java.lang.reflect.Method.invoke(Method.java:511) 06-19 09:54:29.253:
 E/AndroidRuntime(11464):   at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
 06-19 09:54:29.253: E/AndroidRuntime(11464):   at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 dalvik.system.NativeStart.main(Native Method)

我的代码,我不知道它不起作用

holder.newsItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isOnline()) {
                    Intent i = new Intent();
                    i.setClass(getContext(), NewsDetali.class);
                    i.putExtra("id", ArrayListNews.get(position).getId());
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);

//error this line
                    ((Activity) context).overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);

                } else {
                    Toast.makeText(getContext(), "Отсутствует подключение к интернету", Toast.LENGTH_LONG).show();
                }
            }
        });

UPD:

所有代码

public class NewsAdapter extends ArrayAdapter<NewsObject>{
    ArrayList<NewsObject> ArrayListNews;
    int Resourse;
    Context context;
    LayoutInflater vi;

    private ImageLoader imageLoader;
    final View imageLayout;
    //final ProgressBar  spinner;

    public NewsAdapter(Context context, int resource, ArrayList<NewsObject> objects, ImageLoader imageLoader) {
        super(context, resource, objects);

        ArrayListNews = objects;
        Resourse = resource;
        this.context = context;

        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.imageLoader = imageLoader;

        imageLayout = vi.inflate(R.layout.item_pager_image, null);
        //spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if(convertView == null) {
            convertView = vi.inflate(Resourse, null);
            holder = new ViewHolder();

            Typeface face=Typeface.createFromAsset(context.getAssets(), "font/AvenirNext-Regular.ttf");

            holder.imageNews = (ImageView) convertView.findViewById(R.id.imageNews);
            holder.nameNews = (TextView) convertView.findViewById(R.id.nameNews);
            holder.dateNews = (TextView) convertView.findViewById(R.id.dateNews);
            holder.newsItem = (RelativeLayout) convertView.findViewById(R.id.newsItemAdapter);
            holder.loadImg = (PlayGifView) convertView.findViewById(R.id.loading_bg);

            holder.nameNews.setTypeface(face);
            holder.dateNews.setTypeface(face);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        imageLoader.displayImage(ArrayListNews.get(position).getImageLink(), holder.imageNews, null, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String arg0, View arg1) {

            }

            @Override
            public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {

            }

            @Override
            public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {

                int color = Color.parseColor("#4D000000"); //The color u want             
                holder.imageNews.setColorFilter(color);
                holder.nameNews.setText(ArrayListNews.get(position).getName());
                holder.dateNews.setText(ArrayListNews.get(position).getDate());
            }

            @Override
            public void onLoadingCancelled(String arg0, View arg1) {
                Log.e("Script", "OnLoadeingStarted");
            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String uri, View view, int current, int total) {
                Log.e("Script", "OnProgressUpdate" + uri + " " + total);
            }
        });




        holder.newsItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                    Intent i = new Intent(getContext(), NewsDetali.class);

                    i.putExtra("id", ArrayListNews.get(position).getId());
                    i.putExtra("title", ArrayListNews.get(position).getName());
                    i.putExtra("image", ArrayListNews.get(position).getImageLink());
                    i.putExtra("description", ArrayListNews.get(position).getDescription());
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(i);


                    //danger line!!! Error this
                    overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);

            }
        });

        return convertView;
    }


    static class ViewHolder {
        public ImageView imageNews;
        public TextView nameNews;
        public TextView dateNews;
        public RelativeLayout newsItem;
        public PlayGifView loadImg;
    }



}

2 个答案:

答案 0 :(得分:17)

这可能会有所帮助

Activity activity = (Activity) context;
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.act_start_in_from_right, R.anim.act_start_out_to_left);

答案 1 :(得分:2)

像这段代码一样改变

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
getActivity().overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);

然后像在newActivity中那样收到

Bundle b = getIntent().getExtras();
int value = b.getInt("key");