在弹出窗口中显示图像

时间:2014-05-22 00:35:43

标签: android android-layout

我试图在弹出窗口中显示图像,但是我遇到了问题而无法理解为什么会这样。我已经确定了使我的应用程序崩溃的代码行,我也一直在尝试关注另一个显示如何创建弹出窗口的帖子here但是我的情况只是略有不同,因为我试图获取我的图像从文件路径。如果不清楚我很抱歉,因为我不太擅长解释

  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();

}

这是我的custom_fullimage_dialog xml文件

<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>

这是我的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" />

这是我的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>

我认为它崩溃的行是在loadPhoto方法中,就像在log.d()中显示的1 2和3一样。

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

here is my log

2 个答案:

答案 0 :(得分:2)

为什么不使用Lazyloading显示图像,而且你根据你的代码使用Alert对话框而不是Popupwindow。通过你的代码,我认为这是一个参考问题。

在您的代码中

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

应该替换为

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

图片视图未正确引用代码上的相应视图,这可能会导致Null点异常。

//////////////////////编辑后/////////////////////// //////////

点击

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

loadPhoto功能

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();
}

下载Lazyloading Lib from Here只需导入库并添加上面的代码, 不要忘记添加外部存储的权限

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

使用Lazyload就是以高效的方式获取Image。

希望这会对你有所帮助

答案 1 :(得分:1)

您需要使用膨胀的视图检索ImageView ID。实际上,ImageView没有附加到正确的布局,并试图在(父)Activity的布局中找到它的ID。这是Null Pointer Exception的原因 正如您在java.lang.NullPointerException行下方的Logcat中所看到的,它说:

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

然后,在第134行,这是image var,即null。要检索它,您需要将其附加到loadPhoto()中的膨胀(/ )视图,如下所示:

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);
    ...
}