TouchImageView中的setImageResource(int)无法应用于(long)

时间:2015-01-31 18:57:00

标签: android gridview picasso

我正在使用Picasso Library从GridView中的多个网址加载图片。现在我希望能够全屏查看图像,因此我添加了clickListener。问题在于此行中的FullScreen活动:fi.setImageResource(gv.getItemId(position)) - > setImageResource (int) in TouchImageView cannot be applied to (long)

public class FullScreen extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        Intent i = getIntent();

        int position = i.getExtras().getInt("id");
        SampleGridViewAdapter gv = new SampleGridViewAdapter(this);

        final TouchImageView fi = (TouchImageView) findViewById(R.id.full_img);
        fi.setImageResource(gv.getItemId(position));

        Picasso.with(getApplicationContext())
                .load(gv.getItem(position))
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .fit().centerInside()
                .into(fi);
    }
}

另请查看此链接,该家伙有同样的问题,但我找不到解决方法:Android Picasso GridView - Store URL data in Integer array?

编辑: - 这是我在运行应用时获得的。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zenyt/com.zenyt.FullScreen}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.zenyt.FullScreen.onCreate(FullScreen.java:16) // line: int position = i.getExtras().getInt("id");
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)

这是我调用FullScreen活动的片段:

public class Audi2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(com.zenyt.R.layout.audi2, container, false);

        GridView gv = (GridView) rootView.findViewById(R.id.grid_view);
        gv.setAdapter(new SampleGridViewAdapter(getActivity()));
        gv.setOnScrollListener(new SampleScrollListener(getActivity()));

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent mainIntent = new Intent(getActivity(), FullScreen.class);
                startActivity(mainIntent);
            }
        });

       return rootView;
    }

抱歉我的英文。

2 个答案:

答案 0 :(得分:1)

gv.getItemId()将返回网格视图的ID,即long。不是资源ID。考虑以另一种方式获取资源。也许你打算这样做呢?

fi.setImageResource(gv.getItem(position));  //not getItemId

除非,否则您已将gv.getItemId()设置为返回资源ID。如果是这种情况,那么只需转换为int。

 fi.setImageResource((int) gv.getItemId(position));

答案 1 :(得分:0)

看起来您没有将捆绑包传递给您的活动。所以,当你试图获得&#34; id&#34;它不在那里。在onClick中创建并添加捆绑包,如下所示:

@Override
public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
      //create bundle
      Bundle bundle = new Bundle();
      //add data to your bundle
      bundle.putInt("id", position);
      //create intent
      Intent mainIntent = new Intent(getActivity(), FullScreen.class);
      //add bundle to intent
      mainIntent.putExtras(bundle);
      //start activity
      startActivity(mainIntent);
}