1

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

开胃菜.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;
}
}

}

开胃菜.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();
}
});
}
}

弹出.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>

安卓清单

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

代码一起编写时有效,但我想将图像弹出的代码与项目列表分开。此代码将显示菜单列表,但当我单击缩略图时,应用程序崩溃。我错过了什么请指导。任何形式的帮助都将受到高度赞赏。先感谢您。并对新手草率的代码感到抱歉。

4

3 回答 3

1

你一定会得到,NullPointerException因为在Apetizer活动中你没有被灌输PopupWin pop;并且您正在onclick方法中访问 pop 对象而无需初始化

于 2013-02-24T09:20:34.783 回答
0

您正在将其创建PopupWin为单独的 Activity,但并未将其作为 Activity 启动。您不能像创建对象一样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 的图像资源传递并在另一端检索它

于 2013-02-24T09:25:55.520 回答
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));}
于 2013-03-26T18:22:38.677 回答