ViewPager在GridView中实现。给出ClickListener错误并崩溃

时间:2014-07-22 06:42:48

标签: android

我正在开发一个必须在包含图片的主要活动中有GridView的应用程序。

单击图像时,必须以更大的尺寸打开另一个Activity。在此Activity中,必须实施ViewPager,以便我可以左右滑动以在图像之间切换。

我已经完成了很大一部分工作,我不知道问题出在哪里但是当我点击图片时,应用程序崩溃了。有人可以帮帮我吗?

这是我的主要活动代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivityPager extends ActionBarActivity
{
    static int[] FLAG;
    ViewPager viewPager;
    PagerAdapter adapter;
    GridView gridview;
    int count;

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

         FLAG = new int[] { R.drawable.flag_of_argentina, R.drawable.flag_of_brazil,
                    R.drawable.flag_of_england, R.drawable.flag_of_france,
                    R.drawable.flag_of_germany, R.drawable.flag_of_italy, R.drawable.flag_of_spain,
                    R.drawable.flag_of_uruguay };

         AdapterGrid adapter = new AdapterGrid(MainActivityPager.this, FLAG);
         gridview = (GridView) findViewById(R.id.gridView1);
         gridview.setAdapter(adapter);

         gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id)
            {
                Intent intent = new Intent(view.getContext(), ActivityImage.class);
                intent.putExtra("position", position);
                count = gridview.getCount();
                intent.putExtra("count", count);
                startActivity(intent);
            }
        });
    }

    @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);
    }
}

这是我的AdapterGrid类代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class AdapterGrid extends BaseAdapter
{
    private int[] mFlag;
    private Context mContext;

    public AdapterGrid(Context context, int[] flag) {
        //super(context, flag);
        mContext = context;
        mFlag = flag;
    }

    @Override
    public int getCount() {
        return mFlag.length;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
        {
            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            imageView.setImageResource(mFlag[position]);
        } else {
            grid = (View) convertView;
        }
        return grid;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

这是我的ActivityImage代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class ActivityImage extends FragmentActivity
{
    private static int COUNT;
    MyAdapter mAdapter;
    ViewPager mPager;

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

        Intent intent = getIntent();
        COUNT = intent.getExtras().getInt("count");
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return COUNT;
        }

        @Override
        public Fragment getItem(int position) {
           return FragmentImage.init(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_image, menu);
        return true;
    }
}

这是我的FragmentImage类代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FragmentImage extends Fragment {
    int fragVal;

    static FragmentImage init(int pos) {
        FragmentImage frag = new FragmentImage();
        Bundle args = new Bundle();
        args.putInt("val", pos);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_image, container,
                false);

        ImageView imageView = (ImageView) layoutView.findViewById(R.id.imageView1);
        imageView.setImageResource(MainActivityPager.FLAG[fragVal]);

        return layoutView;
    }
}

这些是我的logcat错误:

07-22 09:40:29.915: E/MoreInfoHPW_ViewGroup(25934): Parent view is not a TextView
07-22 09:40:29.920: E/AndroidRuntime(25934): FATAL EXCEPTION: main
07-22 09:40:29.920: E/AndroidRuntime(25934): Process: com.example.gridviewpager, PID: 25934
07-22 09:40:29.920: E/AndroidRuntime(25934): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gridviewpager/com.example.gridviewpager.ActivityImage}: java.lang.NullPointerException
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.ActivityThread.access$800(ActivityThread.java:157)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.os.Looper.loop(Looper.java:157)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.ActivityThread.main(ActivityThread.java:5293)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at java.lang.reflect.Method.invokeNative(Native Method)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at java.lang.reflect.Method.invoke(Method.java:515)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at dalvik.system.NativeStart.main(Native Method)
07-22 09:40:29.920: E/AndroidRuntime(25934): Caused by: java.lang.NullPointerException
07-22 09:40:29.920: E/AndroidRuntime(25934):    at com.example.gridviewpager.ActivityImage.onCreate(ActivityImage.java:27)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.Activity.performCreate(Activity.java:5389)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-22 09:40:29.920: E/AndroidRuntime(25934):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
07-22 09:40:29.920: E/AndroidRuntime(25934):    ... 11 more

2 个答案:

答案 0 :(得分:0)

     Intent intent = new Intent(getApplicationContext, ActivityImage.class);
                    intent.putExtra("position", position);
                    count = gridview.getCount();
                    intent.putExtra("count", count);
                    startActivity(intent);


public Object getItem(int position) {
        return FLAG[posotion];
    }

    @Override
    public long getItemId(int position) {
        return (long)position;
    }

答案 1 :(得分:0)

似乎mPagernull。检查pager布局中是否确实包含ID activity_activity_image.xml