0
我已經創建了一個應用程序與片段和畫廊片段,每當我打開它被關閉的片段。Android照片庫
以下是代碼:
的.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="junagadh.pkmcollege.com.pkmcollege.bscFragment">
<Gallery
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_marginTop="25dp"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
/>
</LinearLayout>
和編碼:
public class bscFragment extends Fragment {
int img[] = new int[5];
public bscFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
for(int i=0;i<img.length;i++) {
img[i]=getResources().getIdentifier("img"+i,"drawable",getActivity().getPackageName());
}
final ImageView iv=(ImageView)getView().findViewById(R.id.imageView1);
iv.setImageResource(img[0]);
Gallery gal=(Gallery)getView().findViewById(R.id.gallery1);
gal.setAdapter(new ImageAdapter(getActivity()));
gal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
iv.setImageResource(img[pos]);
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bsc, container, false);
}
public class ImageAdapter extends BaseAdapter
{
Context c;
public ImageAdapter(Context c){
this.c=c;
}
@Override
public int getCount() {
return img.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View arg1, ViewGroup arg2) {
ImageView iv=new ImageView(c);
iv.setImageResource(img[pos]);
iv.setLayoutParams(new Gallery.LayoutParams(120,150));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
return iv;
}
}
}
日誌:
I/OpenGLRenderer: Initialized EGL, version 1.4
E/EGL_emulation: tid 3280: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa237f040, error=EGL_BAD_MATCH
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: junagadh.pkmcollege.com.pkmcollege, PID: 3263
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at junagadh.pkmcollege.com.pkmcollege.bscFragment.onCreateView(bscFragment.java:34)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 3263 SIG: 9
Application terminated.
我想創建一個照片庫一個畫廊和Imag EVIEW。在圖庫中,它將顯示名稱爲img0,img1,img2等的可繪製文件夾中的所有圖像...並且所選圖像將顯示在圖像視圖上。
有人幫我解決這個問題。
謝謝:)
它解決了我的問題。非常感謝:) @Bayou –
更不用說了。很高興它的工作。快樂編碼! – Bayou