你好我正在开发我的第一个应用程序,到目前为止我成功从SD卡获取图像并在gridview中显示它们。但是当我点击图片时它没有全屏显示我。 我正在使用片段滑动菜单。每当我试图点击图片时,我的应用程序就会关闭。
blow是我的xml和java文件。
GridViewactivity.java
package com.Adz.mediaplus;
import java.util.ArrayList;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
public class GridViewActivity extends Fragment {
private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_grid_view,
container, false);
// Gridview adapter
adapter = new GridViewImageAdapter(getActivity(), imagePaths,
columnWidth);
// setting grid view adapter
gridView.setAdapter(adapter);
return rootView;
}
@Override
public void onStart() {
super.onStart();
gridView = (GridView) getView().findViewById(R.id.grid_view);
utils = new Utils(getActivity());
// Initilizing Grid View
InitilizeGridLayout();
// loading all image paths from SD card
imagePaths = utils.getFilePaths();
}
private void InitilizeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
}
GridViewAdepter.java
package com.Adz.mediaplus;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GridViewImageAdapter extends BaseAdapter {
private Activity _activity;
private ArrayList<String> _filePaths = new ArrayList<String>();
private int imageWidth;
public GridViewImageAdapter(Activity activity, ArrayList<String> filePaths,
int imageWidth) {
this._activity = activity;
this._filePaths = filePaths;
this.imageWidth = imageWidth;
}
@Override
public int getCount() {
return this._filePaths.size();
}
@Override
public Object getItem(int position) {
return this._filePaths.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(_activity);
} else {
imageView = (ImageView) convertView;
}
// get screen dimensions
Bitmap image = decodeFile(_filePaths.get(position), imageWidth,
imageWidth);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
imageWidth));
imageView.setImageBitmap(image);
// image view click listener
imageView.setOnClickListener(new OnImageClickListener(position));
return imageView;
}
class OnImageClickListener implements OnClickListener {
int _postion;
// constructor
public OnImageClickListener(int position) {
this._postion = position;
}
@Override
public void onClick(View v) {
// on selecting grid view image
// launch full screen activity
Intent i = new Intent(_activity, FullScreenViewActivity.class);
i.putExtra("position", _postion);
_activity.startActivity(i);
}
}
/*
* Resizing image size
*/
public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
try {
File f = new File(filePath);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_WIDTH = WIDTH;
final int REQUIRED_HIGHT = HIGHT;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
&& o.outHeight / scale / 2 >= REQUIRED_HIGHT)
scale *= 2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
FullScreenActivity.java
package com.Adz.mediaplus;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FullScreenViewActivity extends Fragment {
private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_fullscreen_view,
container, false);
return rootView;
}
@Override
public void onStart() {
super.onStart();
viewPager = (ViewPager) getView().findViewById(R.id.pager);
utils = new Utils(getActivity().getApplicationContext());
Intent i = super.getActivity().getIntent();
int position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapter(getActivity(),
utils.getFilePaths());
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
}
FullScreenImageAdepter.java
package com.Adz.mediaplus;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imgDisplay;
Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
imgDisplay.setImageBitmap(bitmap);
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
activity_fullscreen_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
layout_fullscreen_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.Adz.mediaplus.TouchImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/button_background"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Close" />
</RelativeLayout>
logcat的
01-12 22:03:46.443: W/ResourceType(9872): No known package when getting value for resource number 0xffffffff
01-12 22:03:46.443: W/ImageView(9872): Unable to find resource: -1
01-12 22:03:46.443: W/ImageView(9872): android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
01-12 22:03:46.443: W/ImageView(9872): at android.content.res.Resources.getValue(Resources.java:1014)
01-12 22:03:46.443: W/ImageView(9872): at android.content.res.Resources.getDrawable(Resources.java:659)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ImageView.resolveUri(ImageView.java:616)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ImageView.onMeasure(ImageView.java:722)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.measure(View.java:15513)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.measure(View.java:15513)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ListView.setupChild(ListView.java:1909)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ListView.makeAndAddView(ListView.java:1834)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ListView.fillDown(ListView.java:674)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ListView.fillSpecific(ListView.java:1332)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.ListView.layoutChildren(ListView.java:1630)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.layout(View.java:14003)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-12 22:03:46.443: W/ImageView(9872): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.layout(View.java:14003)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.layout(View.java:14003)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.layout(View.java:14003)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-12 22:03:46.443: W/ImageView(9872): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 22:03:46.443: W/ImageView(9872): at android.os.Looper.loop(Looper.java:137)
01-12 22:03:46.443: W/ImageView(9872): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-12 22:03:46.443: W/ImageView(9872): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 22:03:46.443: W/ImageView(9872): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 22:03:46.443: W/ImageView(9872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 22:03:46.443: W/ImageView(9872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 22:03:46.443: W/ImageView(9872): at dalvik.system.NativeStart.main(Native Method)
01-12 22:03:46.443: W/ResourceType(9872): No known package when getting value for resource number 0xffffffff
01-12 22:03:46.443: W/ImageView(9872): Unable to find resource: -1
01-12 22:03:46.443: W/ImageView(9872): android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
01-12 22:03:46.443: W/ImageView(9872): at android.content.res.Resources.getValue(Resources.java:1014)
01-12 22:03:46.443: W/ImageView(9872): at android.content.res.Resources.getDrawable(Resources.java:659)
01-12 22:03:46.443: W/ImageView(9872): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-12 22:03:46.443: W/ImageView(9872): at android.view.View.layout(View.java:14003)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-12 22:03:46.443: W/ImageView(9872): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-12 22:03:46.443: W/ImageView(9872): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-12 22:03:46.443: W/ImageView(9872): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-12 22:03:46.443: W/ImageView(9872): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-12 22:03:46.443: W/ImageView(9872): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-12 22:03:46.443: W/ImageView(9872): at android.os.Handler.handleCallback(Handler.java:725)
01-12 22:03:46.443: W/ImageView(9872): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 22:03:46.443: W/ImageView(9872): at android.os.Looper.loop(Looper.java:137)
01-12 22:03:46.443: W/ImageView(9872): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-12 22:03:46.443: W/ImageView(9872): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 22:03:46.443: W/ImageView(9872): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 22:03:46.443: W/ImageView(9872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 22:03:46.443: W/ImageView(9872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 22:03:46.443: W/ImageView(9872): at dalvik.system.NativeStart.main(Native Method)
01-12 22:03:46.443: W/Trace(9872): Unexpected value from nativeGetEnabledTags: 0
01-12 22:03:47.723: W/Trace(9872): Unexpected value from nativeGetEnabledTags: 0
01-12 22:03:47.723: W/Trace(9872): Unexpected value from nativeGetEnabledTags: 0
01-12 22:03:47.723: D/AndroidRuntime(9872): Shutting down VM
01-12 22:03:47.723: W/dalvikvm(9872): threadid=1: thread exiting with uncaught exception (group=0xb5dd2908)
01-12 22:03:47.723: E/AndroidRuntime(9872): FATAL EXCEPTION: main
01-12 22:03:47.723: E/AndroidRuntime(9872): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.Adz.mediaplus/com.Adz.mediaplus.FullScreenViewActivity}; have you declared this activity in your AndroidManifest.xml?
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.Activity.startActivityForResult(Activity.java:3370)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.Activity.startActivityForResult(Activity.java:3331)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.Activity.startActivity(Activity.java:3566)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.Activity.startActivity(Activity.java:3534)
01-12 22:03:47.723: E/AndroidRuntime(9872): at com.Adz.mediaplus.GridViewImageAdapter$OnImageClickListener.onClick(GridViewImageAdapter.java:87)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.view.View.performClick(View.java:4202)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.view.View$PerformClick.run(View.java:17340)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.os.Handler.handleCallback(Handler.java:725)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.os.Looper.loop(Looper.java:137)
01-12 22:03:47.723: E/AndroidRuntime(9872): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-12 22:03:47.723: E/AndroidRuntime(9872): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 22:03:47.723: E/AndroidRuntime(9872): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 22:03:47.723: E/AndroidRuntime(9872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 22:03:47.723: E/AndroidRuntime(9872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 22:03:47.723: E/AndroidRuntime(9872): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
一些建议:
不要使用Activity调用片段名称结束。用Fragment结束它们。它们不是活动,而是碎片。这只会增加混乱。
您尚未在清单中声明FullScreenViewActivity。
因此发生了崩溃:_activity.startActivity(i);
见#2。
不要对成员变量使用“_”。 Android(虽然不一定更好)惯例是使用“m”。所以mActivity,mPosition等。除非它是一个局部变量,在这种情况下不需要前缀(位置,活动等)。
您正在重新创建适配器onStart()
,这有点效率低下,您可能只想创建一次并为任何其他情况执行notifyDataSetChanged()
。
更新:
我的时间很短因为我必须离开,但是notifyDataSetChanged背后的想法是...
void onResume() {
super.onResume();
if ( mAdapter == null ) {
mAdapter = new YourAdapter(); // blah blah blah
mList.setAdapter(mAdapter);
///etc.
} else {
mAdapter.notifyDataSetChanged();
}
}