0

can any say that how can i view image in another activity when click on the particular image with description

Here is the java file

package course.examples.UI.GridLayout;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class GridLayoutActivity extends Activity {

protected static final String EXTRA_RES_ID = "POS";

private int[]  mThumbIdsFlowers = {R.drawable.image1, R.drawable.image2,
                R.drawable.image3, R.drawable.image4,            

      R.drawable.image5,
                R.drawable.image6, R.drawable.image7,   

      R.drawable.image8,
                R.drawable.image9, R.drawable.image10,   

    R.drawable.image11,
                R.drawable.image12};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     String[] description=getResources().getStringArray(R.array.description);
    GridView gridview=(GridView) findViewById(R.id.gridview);

    gridview.setAdapter(new ImageAdapter(this, mThumbIdsFlowers,description));


}
    }

Custom Adapter

 package course.examples.UI.GridLayout;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.Inflater;

    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class ImageAdapter extends BaseAdapter {
    private static final int PADDING = 8;
    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;
    private Context mContext;
    private int[] mThumbIds;
    private String[] description;
    ArrayList<row_item> arraylist;
    class row_item{
     int img;
     String description;
    row_item(int img,String description){
        this.img=img;
        this.description=description;

    }

     }
    public ImageAdapter(Context c, int[] img,String[] description) {
        mContext = c;
        this.mThumbIds =img;
        this.description=description;
        this.arraylist=new ArrayList<row_item>();
        for(int i=0;i<12;i++)
        {
            arraylist.add(new row_item(img[i], description[i]));
        }
    }

    @Override
    public int getCount() {
        return arraylist.size();
    }

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

    // Will get called to provide the ID that
    // is passed to OnItemClickListener.onItemClick()


    // create a new ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.row_style,null);
        ImageView img=(ImageView) view.findViewById(R.id.img);
        TextView text=(TextView) view.findViewById(R.id.text);
        img.setImageResource(arraylist.get(position).img);
        text.setText(arraylist.get(position).description);
        return view;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
        }

I am trying to set layout using the baseadapter class .I am storing the description and image id in the arraylist then I am setting ImageView and TextView from the ArrayList .It is giving runtime error.

here is logcat msg

06-08 02:22:29.516: E/AndroidRuntime(1322): FATAL EXCEPTION: main
06-08 02:22:29.516: E/AndroidRuntime(1322): Process: course.examples.UI.GridLayout, PID: 1322
06-08 02:22:29.516: E/AndroidRuntime(1322): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{course.examples.UI.GridLayout/course.examples.UI.GridLayout.GridLayoutActivity}: java.lang.NullPointerException
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.os.Looper.loop(Looper.java:137)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.ActivityThread.main(ActivityThread.java:4998)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invoke(Method.java:515)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at dalvik.system.NativeStart.main(Native Method)
06-08 02:22:29.516: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at course.examples.UI.GridLayout.GridLayoutActivity.<init>(GridLayoutActivity.java:24)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at java.lang.Class.newInstanceImpl(Native Method)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at java.lang.Class.newInstance(Class.java:1208)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-08 02:22:29.516: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
06-08 02:22:29.516: E/AndroidRuntime(1322):     ... 11 more
06-08 02:22:34.556: I/Process(1322): Sending signal. PID: 1322 SIG: 9

here is layout xml file row_style

<?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="horizontal" >
<ImageView 
    android:id="@+id/img"
     android:layout_height="wrap_content"
    />
<TextView 
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

here is main.xml in layout

<?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="horizontal" >
<GridView 
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   ></GridView>
 </LinearLayout>

here is mainfest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.examples.UI.GridLayout"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:name=".GridLayoutActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ImageViewActivity" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" >
            </action>

            <data
                android:mimeType="image/*"
                android:scheme="android.resource" >
            </data>

            <category android:name="android.intent.category.DEFAULT" >
            </category>
          </intent-filter>
     </activity>
     </application>
   </manifest>

1 Answers1

0

The stack trace points to this line as the source of the exception:

private String[] description=getResources().getStringArray(R.array.description);

The inherited getResources() method is throwing an exception. That's because you can't call getResources() until later, after the activity is constructed and initialized. Move those member variable initializations into onCreate() and you should be good to go.

Simply Change

private String[] description=getResources().getStringArray(R.array.description);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

to

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
String[] description=getResources().getStringArray(R.array.description);
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74