Android Popup Window崩溃了应用程序

时间:2013-02-24 09:10:24

标签: android menu arraylist android-manifest popupwindow

嗨,我是Android的新手,我正在尝试为餐厅制作菜单。我的应用程序将在图像按钮中显示项目列表及其拇指大小的图像。列表中有多个项目。当用户点击图像按钮时,弹出另一个窗口显示图像。根据单击的按钮,图像应弹出。我为此目的使用了switch语句。也就是说,如果按下按钮一,则应将R.drawable.imag01传递给弹出窗口类。

Appetizer.java

package com.example.thumbnailzoom;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Appetizer extends Activity implements View.OnClickListener {
ImageButton ibAp1;
int img;
PopupWin pop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appetizer);
DisplayComponent();

ibAp1.setOnClickListener(this);
}

private void DisplayComponent() {
ibAp1 = (ImageButton) findViewById(R.id.Ap1);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Ap1:
img = R.drawable.appetimg01;
pop.setPopupImage(img);
pop.displayImage();
break;
}
}

}

Appetizer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/appetizerTitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Canape Assortments       "
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/appetizerPrice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="1.800"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/appetizerDes1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="20"
android:text="6 pieces of mini toasted bread topped with smoked salmon prawn and turkey"
android:textSize="15sp"
android:textStyle="italic" />

<ImageButton
android:id="@+id/Ap1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/appetimg01" />
</LinearLayout>

</LinearLayout>

PopupWin.java

package com.example.thumbnailzoom;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;

public class PopupWin extends Activity {
private ImageView img;
private int imgValue;
private Button bDismiss;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void setPopupImage(int img){
imgValue = img;
}

public void displayImage() {

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
img.setImageResource(imgValue);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

Button bDismiss = (Button) popupView.findViewById(R.id.dismiss);
bDismiss.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/dim_back" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dim_back"
android:orientation="vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:orientation="vertical" 
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true" />

<Button
android:id="@+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20sp"
android:text="Go Back"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

Android Manifest

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

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" >
<activity
android:name="com.example.thumbnailzoom.Appetizer"
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="com.example.thumbnailzoom.PopupWin"
android:label="@string/app_name" >
</activity>
</application>

</manifest>

代码在一起编写时有效但我想将图像弹出窗口的代码与项目列表分开。此代码将显示“菜单”列表,但是当我单击缩略图时,应用程序崩溃。我缺少什么请指导。任何形式的帮助都非常感谢。先感谢您。抱歉新手邋code的代码。

3 个答案:

答案 0 :(得分:1)

您必须获得NullPointerException,因为在Apetizer活动中您没有初始化PopupWin pop;并且您正在onclick方法中访问pop对象而不初始化

答案 1 :(得分:0)

您正在将PopupWin创建为单独的活动,但不会将其作为活动启动。您无法创建PopupWin pop;之类的对象,然后像使用其他任何活动一样使用它们。此外,您没有在任何地方初始化pop,因此无论如何都会遇到NullPointerException,无论它是什么类。

现在,假设您初始化它:如果您这样做,那么您的Activity的onCreate()永远不会被调用,并且不会创建其上下文等,因为它不遵循Activity生命周期。因此,每当您尝试使用LayoutInflater之类的内容时,您都会收到异常,因为getBaseContext()将返回null。

所以解决这个问题,使用:

启动PopupWin
case R.id.Ap1:
    Intent intent = new Intent(this, PopupWin.class);
    startActivity(intent);
    break;

您可以传递要用作Intent Extra的图像资源,并在另一端检索

答案 2 :(得分:0)

你可以尝试这个来打开弹出窗口

public void OpenPopWindow(View v)
{
    LayoutInflater layoutInflater  = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
     View popupView = layoutInflater.inflate(R.layout.popupmenu, null);
       PopupWindow popupwindow = new PopupWindow(popupView, LayoutParams.FILL_PARENT,  
               LayoutParams.WRAP_CONTENT, false); 

             popupwindow .setAnimationStyle(R.style.DialogAnimation);  
             popupwindow .setWidth(display.getWidth());  
             popupwindow .showAtLocation(v, Gravity.BOTTOM, 0, 0);  
             popupwindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.black));}