0

I am attempting to display an image on a popup window, however I have ran into problems and cannot see why this is. I have pinpointed the line of code that makes my app crash, also I have been trying to follow another post which shows how to create a popup window here However my situation is only slightly different because I am trying to get my image from a file path. I am sorry if this is not clear as I am not very good at explaining

  private void listClick(){
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parentView, View childView, 
                int position, long id) {

             String selectedItem = list.getItemAtPosition(position).toString();
             String imgFilePath = dataBank.get(selectedItem);

             File imgFile = new File(imgFilePath);
             if(imgFile.exists()){
                 String absFilePath = imgFile.getAbsolutePath();
                 loadPhoto(absFilePath, 10, 10);

             }
             Toast.makeText(context, selectedItem, duration).show(); 
        }
    });  
}

private void loadPhoto(String filepath, int width, int height){
    Log.d("FAILS", "1");
     AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
     Log.d("FAILS", "2");
     View layout = inflater.inflate(R.layout.custom_fullimage_dialog, 
             (ViewGroup)findViewById(R.id.layout_root));
     ImageView image = (ImageView)findViewById(R.id.fullimage);
     Log.d("FAILS", "3");
     image.setImageBitmap(BitmapFactory.decodeFile(filepath));
     Log.d("FAILS", "4");
     imageDialog.setView(layout);
     imageDialog.setPositiveButton("Return", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

     imageDialog.create();
     imageDialog.show();

}

and here is my custom_fullimage_dialog xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">

  <ImageView
      android:id="@+id/fullimage"
      android:layout_width="fill_parent"
      android:layout_height="match_parent" >

  </ImageView>

  <TextView android:id="@+id/custom_fullimage_placename"
      android:layout_width="wrap_content" android:layout_height="fill_parent"
      android:textColor="#FFF">
  </TextView>
</LinearLayout>

Here is my activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.igauto.MainActivity"
tools:ignore="MergeRootFrame" />

and here is my fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.igauto.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text_user" />

<EditText
    android:id="@+id/user_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/userText"
    android:inputType="text" >

    <requestFocus />
</EditText>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="selectImage"
    android:text="@string/button_image" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="sendMessage"
    android:text="@string/button_send" />

<ListView
    android:id="@+id/myList"
    android:layout_width="wrap_content"
    android:layout_height="120dp" >
</ListView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="tempEdit"
    android:text="@string/button_Edit" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:inputType="textMultiLine" />

</LinearLayout>

I believe the line it crashes on is within the loadPhoto method as in the log it shows 1 2 and 3 from the log.d()'s I have put up.

image.setImageBitmap(BitmapFactory.decodeFile(filepath));

here is my log

Community
  • 1
  • 1
DorkMonstuh
  • 721
  • 1
  • 16
  • 34

2 Answers2

2

Why don't you use Lazyloading to display the images,And Also You are using Alert dialog box not Popupwindow as per your code.Looking through you code i think its a reference issue.

In your code

View layout = inflater.inflate(R.layout.custom_fullimage_dialog, 
         (ViewGroup)findViewById(R.id.layout_root));
 ImageView image = (ImageView)findViewById(R.id.fullimage);

should be replace to

View layout = inflater.inflate(R.layout.custom_fullimage_dialog, 
         (ViewGroup)findViewById(R.id.layout_root));
 ImageView image = (ImageView) layout.findViewById(R.id.fullimage);

The image view is not correctly referred to the corresponding View on your code ,that may cause a Null point Exception.

//////////////////////After Editing/////////////////////////////////

In Your on click

String filepath = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg";
    loadPhoto(filepath);//Its just A example path

loadPhoto Function

private void loadPhoto(String filepath) {
    Log.d("FAILS", "1");
    AlertDialog.Builder imageDialog = new AlertDialog.Builder(
            MainActivity.this);
    ImageLoader imagLoader = new ImageLoader(getApplicationContext());//This is class in the Lazyloading 

    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(LAYOUT_INFLATER_SERVICE);

    Log.d("FAILS", "2");

    View layout = inflater.inflate(R.layout.custom_fullimage_dialog,
            (ViewGroup) findViewById(R.id.layout_root));

    ImageView image = (ImageView) layout.findViewById(R.id.fullimage);

    Log.d("FAILS", "3");

    imagLoader.DisplayImage(filepath, image);//Display images easily using Lazyloading Using Function DisplayImage

    Log.d("FAILS", "4");

    imageDialog.setPositiveButton("Return",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
    imageDialog.setView(layout);
    imageDialog.show();
}

Download Lazyloading Lib from Here just import the library and Add the above code , Dont Forget To Add The Permission For External Storage

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The use Of Lazyload is that it fetch Image In a efficent way .

Hope this will help you

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Ramz
  • 6,858
  • 6
  • 59
  • 84
  • Hi ramz I inflate my custom_fullimage_dialog layout so should I not be attaching the view within this layout to my ImageView which is called fullimage? – DorkMonstuh May 22 '14 at 01:37
  • As u inflated the custom_fullimage_dialog layout you should attach the image view within that inflated layout othervise it will cause a null point exception – Ramz May 22 '14 at 04:44
1

You need to retrieve the ImageView id with the inflated view. Indeed, the ImageView is not attached to the right layout and try to find its id on the (parent) Activity's layout. That's the cause of your Null Pointer Exception.
As you can see in the Logcat below the line java.lang.NullPointerException, it's says:

at com...MainActivity.loadPhoto(MainActivity.java:134) 
// [ error at Package_Name.Activity_Name.method(File.line) ]

Then, at line 134, this is image var which is null. To retrieve it, you need to attach it to the inflated(/parent) view in loadPhoto() as follows:

private void loadPhoto(String filepath, int width, int height) {
    ...
    // create the inflated view
    View layout = inflater.inflate(R.layout.custom_fullimage_dialog, (ViewGroup)findViewById(R.id.layout_root)); 
    // retrieve the imageview by attaching it with above var
    ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
    ...
}  
fllo
  • 11,595
  • 5
  • 39
  • 95
  • @Filo when you say(parent)Activities layout would that be the layout where my listView is located? – DorkMonstuh May 22 '14 at 01:49
  • Yes, @DorkMonstuh. I guess its name is *activity_main.xml* (cause of MainActivity name). Anyway, it refers to the layout used for setContextView method in your activity. – fllo May 22 '14 at 01:52
  • @Filo so would I put something like this ImageView image = (ImageView) layout.findViewById(R.id.container); as the id for my activity_main.xml is container. I also have a fragment_main, I put the above code in but it still crashed. – DorkMonstuh May 22 '14 at 01:58
  • In your xml, the ImageView has 'fullimage' has id, then you need to **attach the parent layout** (where this id is: custom_fullimage_dialog.xml) **and** get *the right id* as follows: **layout**...(R.id. **fullimage**). @DorkMonstuh – fllo May 22 '14 at 02:01
  • I think there might be a misunderstanding on my part I'm sorry. I have 3 xml files namely activity_main.xml, fragment_main.xml and custom_fullimage_dialog.xml. The xml code posted above is within the custom_fullimage_dialog.xml which is where my only ImageView is stored. My listView is within my fragment_main. I think I should post up my other 2 xml's for more clarification. @Filo – DorkMonstuh May 22 '14 at 02:15
  • No need @DorkMonstuh, this is not a layout problem at least you call the listview from onCreatedView method in the fragment, there is no problem. Did you try my example above? If it's not working, post the revelant part of your Activity instead. – fllo May 22 '14 at 02:24
  • @Filo how does one attach the parent layout and get the right id? From what you said it seems that I did the right thing in my original post. – DorkMonstuh May 22 '14 at 02:45
  • See the first part of *Ramz* answer, and what you should replace. Also like my example, you just need to add `layout.findViewById();` in your actual code @DorkMonstuh. As we said, the missing part is the "parent container" of the ImageView which named `layout`. – fllo May 22 '14 at 02:47
  • I think I understand now I should change R.id.fullimage to R.id.layout_root as this and attach it to the imageview. so something like this ImageView image = (ImageView) layout.findViewById(R.id.layout_root); – DorkMonstuh May 22 '14 at 03:05
  • @Filo sorry for my incompetence and for your patience I got it to work now however when i go back and select another picture I get an out of memory error from this line image.setImageBitmap(BitmapFactory.decodeFile(filepath)); – DorkMonstuh May 22 '14 at 03:14
  • 1
    I understand my mistake @DorkMonstuh.. Sorry about that! The code is right but my first comment is an error. That induced you (and me) to a misunderstand. – fllo May 22 '14 at 03:15
  • The right herarchy is: Activity's layout = *grandparent* / Dialog's layout (inflated view) = *parent* / ImageView = *child*. Then in my answer, "*inflated(/parent) view*" refers to the ImageView's layout. That being restore, [this answer](http://stackoverflow.com/questions/21012006/android-outofmemoryerror/21012043#21012043) can be useful to understand the OutOfMemory error @DorkMonstuh. – fllo May 22 '14 at 03:44