2013-02-24 30 views
1

嗨,我是新来的机器人,我想为餐厅做一个菜单。我的应用程序将在图像按钮中显示项目列表及其缩略图。列表中有多个项目。当用户点击图像按钮时,会弹出另一个窗口显示图像。根据点击哪个按钮,图像应该弹出。我用switch语句来达到目的。也就是说,如果按下按钮1,则应将R.drawable.imag01传递给弹出窗口类。Android弹出窗口崩溃应用程序

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

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

时写在一起,但我想图像弹出式的代码从项目列表分开代码工作。此代码将显示菜单列表,但当我点击缩略图时,应用程序崩溃。我错过了什么,请指导。任何形式的帮助都非常感谢。先谢谢你。对于新手潦草的代码感到抱歉。

+0

总是从后一个logcat中完整的堆栈跟踪与你在这里发表任何崩溃的bug。 – 2013-02-24 09:12:03

+0

它给了我这个错误说明\t资源\t路径\t位置\t类型 此LinearLayout布局或其LinearLayout父项是无用的;将背景属性转移到其他视图\t弹出。xml \t/ThumbnailZoom/res/layout \t line 14 \t Android Lint问题 – user2037113 2013-02-24 10:24:04

+0

这不是崩溃日志,它不是错误。这是一个警告。你的布局有些效率不高,但这不是问题。 – 2013-02-24 15:12:57

回答

1

你必须得到NullPointerException,因为在Apetizer活动你没有intilizing PopupWin pop;并且您正在访问onclick方法中的弹出式对象,而无需初始化

0

您正在创建PopupWin作为单独的活动,但未将其作为活动启动。您不能像PopupWin pop;一样创建对象,然后像使用其他任何活动一样使用它们。另外,你并没有在任何地方初始化pop,所以无论它是什么类型,反正你都会遇到NullPointerException。如果你这样做,那么你的Activity的onCreate()永远不会被调用,并且它的上下文等不会被创建,因为它不会遵循Activity的生命周期。因此,只要您尝试使用类似LayoutInflater的东西,就会得到异常,因为getBaseContext()将返回空值。

所以解决这个问题,使用启动PopupWin

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

你可以通过你想作为一个Intent额外的使用和检索它的另一端

+0

我写了PopupWin的onCreate方法的意图,并将其描述为清单中的单独活动。我还在Appetizer的displaycomponent()方法下初始化了开胃菜中的pop。但现在应用程序崩溃。我无法坚持adnroid的概念。它与C++和java非常不同 – user2037113 2013-02-24 10:18:31

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