Привет, я новичок в андроиде, и я пытаюсь создать меню для ресторана. Мое приложение отобразит список элементов вместе с их большим размером изображения в кнопке изображения. В списке есть несколько элементов. Когда пользователь нажимает кнопку «Изображение», появляются другие окна, показывающие изображение. В зависимости от того, какая кнопка нажата, изображение должно выскочить. Я использовал инструкцию switch для этой цели. То есть, если нажать кнопку один, 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 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>
Кодекс работает, когда написано вместе, но я хочу, чтобы отделить код изображения всплывающего окна из списка элементов. Этот код отобразит список меню, но когда я нажму на миниатюру, приложение выйдет из строя. Что мне не хватает, пожалуйста, посоветуйте. Любая помощь высоко ценится. Заранее спасибо. И извините за новичком неаккуратный код.
Всегда отправляйте полную трассировку стека из logcat с любой аварийной ошибкой, которую вы публикуете здесь. –
это то, что я предлагаю эту ошибкуОписание \t Ресурс \t Путь \t Местоположение \t Тип Этот макет LinearLayout или его родитель LinearLayout бесполезен; передать фоновый атрибут другому виду \t всплывающее окно.xml \t/ThumbnailZoom/res/layout \t Строка 14 \t Проблема с линзой Android – user2037113
Это не журнал сбоев, это не ошибка. Это предупреждение. В вашем макете есть что-то неэффективное, но здесь проблема не в этом. –