我正在为一家餐馆制作一个菜单应用程序。 我只想让第一页成为一个类别列表,每个类别都包含一个带有标题,描述和价格的菜单列表。
我已经将每个类别链接到新的活动页面,一切正常,直到我修改页面(刚开始第一个菜单类别),每次单击已修改的第一个类别时它都会崩溃。其余类别仍然有效。
感谢您的帮助!
这是我的MainActivity.java
package com.example.jj.landscapemenu;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView List;
String[] memeTitles;
String[] memeDescriptions;
int[] images={R.drawable.smallplates, R.drawable.lunch, R.drawable.streetfood, R.drawable.alltimefav, R.drawable.vegetarian, R.drawable.sideorder, R.drawable.dessert, R.drawable.drinks, R.drawable.happyhour};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
memeTitles = res.getStringArray(R.array.titles);
memeDescriptions = res.getStringArray(R.array.descriptions);
List= (ListView) findViewById(R.id.categories);
LandscapeMenuAdapter adapter=new LandscapeMenuAdapter(this, memeTitles, images, memeDescriptions);
List.setAdapter(adapter);
List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myintent = new Intent(view.getContext(), Main2Activity.class);
startActivityForResult(myintent, 0);
}
if (position == 1) {
Intent myintent = new Intent(view.getContext(), Main3Activity.class);
startActivityForResult(myintent, 1);
}
if (position == 2) {
Intent myintent = new Intent(view.getContext(), Main4Activity.class);
startActivityForResult(myintent, 2);
}
if (position == 3) {
Intent myintent = new Intent(view.getContext(), Main5Activity.class);
startActivityForResult(myintent, 3);
}
if (position == 4) {
Intent myintent = new Intent(view.getContext(), Main6Activity.class);
startActivityForResult(myintent, 4);
}
if (position == 5) {
Intent myintent = new Intent(view.getContext(), Main7Activity.class);
startActivityForResult(myintent, 5);
}
if (position == 6) {
Intent myintent = new Intent(view.getContext(), Main8Activity.class);
startActivityForResult(myintent, 6);
}
if (position == 7) {
Intent myintent = new Intent(view.getContext(), Main9Activity.class);
startActivityForResult(myintent, 7);
}
if (position == 8) {
Intent myintent = new Intent(view.getContext(), Main10Activity.class);
startActivityForResult(myintent, 8);
}
}
} );
}
static class LandscapeMenuAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
LandscapeMenuAdapter(Context c,String[] titles, int imgs[], String[] desc)
{
super(c,R.layout.single_row,R.id.textView,titles);
this.context=c;
this.images=imgs;
this.titleArray=titles;
this.descriptionArray=desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,parent, false);
ImageView myImage= (ImageView) row.findViewById(R.id.imageView);
TextView myTitle= (TextView) row.findViewById(R.id.textView);
TextView myDescription= (TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}}
这是我的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LandscapeMenu</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="titles">
<item>Small Plates</item>
<item>Lunch</item>
<item>Street Food</item>
<item>Bai Tong All Time Favorites</item>
<item>Vegetarian Menu</item>
<item>Side Orders</item>
<item>Desserts</item>
<item>Drinks</item>
<item>Happy Hour Menu</item>
</string-array>
<string-array name="descriptions">
<item> </item>
<item> </item>
<item> </item>
<item> </item>
<item> </item>
<item> </item>
<item> </item>
<item> </item>
<item> </item>
</string-array>
<string-array name="titles2">
<item>Fresh Rolls</item>
<item>Grilled Pork Platter</item>
<item>Crab Wontons</item>
<item>Fried Tofu</item>
<item>Grilled Beef</item>
<item>Spare Ribs</item>
<item>Sai Gok E San</item>
<item>Moo Dade Deaw</item>
<item>Tod Mun Pla</item>
</string-array>
<string-array name="descriptions2">
<item>Prawns, BBQ pork, cilantro, bean sprout, carrots, lettuce, and rice vermicelli wrapped in soft rice skin. Served with our famous peanut sauce.</item>
<item>Thai grilled marinated pork on a skewer served with sticky rice.</item>
<item>Crispy-fried wontons stuffed with crab meat and cream cheese. Served with plum sauce.</item>
<item>Deep-fried tofu, served with plum sauce sprinkled with ground peanuts.</item>
<item>Tender slices of grilled steak served with our homemade chili dipping sauce.</item>
<item>Deep fry marinated pork spare ribs. Served with a spicy Sriracha chili sauce.</item>
<item> </item>
<item>Pork strips marinated in a sugar and salt brine, then deep frying them to a flavorful golden brown.</item>
<item>Homemade, fried fishcakes, served with a cucumber-plum sauce topped with crushed peanuts.</item>
</string-array>
<string-array name="price2">
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
<item>$00.00</item>
</string-array>
</resources>
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jj.landscapemenu.MainActivity"
android:background="@drawable/woodbackground">
<ListView
android:id="@+id/categories"
android:layout_width="584dp"
android:layout_height="814dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
这是我的single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="@drawable/smallplates" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Plates"
android:textAlignment="textStart"
android:textSize="35sp"
android:layout_marginBottom="28dp"
android:layout_alignBottom="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignBottom="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这是我的Main2Activity.java
package com.example.jj.landscapemenu;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
ListView List;
String[] memeTitles2;
String[] memeDescriptions2;
String[] memePrice2;
int[] images={R.drawable.freshrolls, R.drawable.moopingbig, R.drawable.crabwontons, R.drawable.friedtofu, R.drawable.grilledbeef, R.drawable.spareribs, R.drawable.saigokesan, R.drawable.moodadedeaw, R.drawable.todmunpla};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Resources res = getResources();
memeTitles2 = res.getStringArray(R.array.titles2);
memeDescriptions2 = res.getStringArray(R.array.descriptions2);
List= (ListView) findViewById(R.id.categories);
SmallPlatesAdapter adapter=new SmallPlatesAdapter(this, memeTitles2, images, memeDescriptions2, memePrice2);
List.setAdapter(adapter);
}
}
class SmallPlatesAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
String[] priceArray;
SmallPlatesAdapter(Context c,String[] titles, int imgs[], String[] desc, String[] prc)
{
super(c,R.layout.single_row2,R.id.textView3,titles);
this.context=c;
this.images=imgs;
this.titleArray=titles;
this.descriptionArray=desc;
this.priceArray=prc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row2,parent, false);
ImageView myImage= (ImageView) row.findViewById(R.id.imageView2);
TextView myTitle= (TextView) row.findViewById(R.id.textView3);
TextView myDescription= (TextView) row.findViewById(R.id.textView12);
TextView myPrice= (TextView) row.findViewById(R.id.textView13);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
myPrice.setText(priceArray[position]);
return row;
}
}
这是我的single_row2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="250dp"
android:layout_height="165dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/freshrolls" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView2"
android:layout_marginLeft="21dp"
android:layout_marginStart="21dp"
android:layout_toRightOf="@+id/imageView2"
android:textSize="24sp" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView13"
android:layout_alignLeft="@+id/textView3"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/textView3"
android:layout_below="@+id/textView3"
android:textStyle="italic" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignLeft="@+id/textView12"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/textView12" />
</RelativeLayout>
这是我的activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jj.landscapemenu.Main2Activity"
android:background="@drawable/woodbackground">
<ListView
android:id="@+id/smallplates"
android:layout_width="585dp"
android:layout_height="873dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jj.landscapemenu">
<supports-screens android:compatibleWidthLimitDp="320" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main4Activity" />
<activity android:name=".Main5Activity" />
<activity android:name=".Main6Activity" />
<activity android:name=".Main7Activity" />
<activity android:name=".Main8Activity" />
<activity android:name=".Main9Activity" />
<activity android:name=".Main10Activity"></activity>
</application>
</manifest>
编辑列表列表(创建错误) 和意图(没问题)
感谢您的帮助!
答案 0 :(得分:0)
尝试猜测,因为没有崩溃日志。
您的AndroidManifest.xml中是否声明了Main2Activity? 你需要替换它:
Intent myintent = new Intent(view.getContext(), Main2Activity.class);
用这个:
Intent myintent = new Intent(MainActivity.this, Main2Activity.class);
这也是错误的:
List= (ListView) findViewById(R.id.categories);
SmallPlatesAdapter adapter=new SmallPlatesAdapter(this, memeTitles2, images, memeDescriptions2, memePrice2);
List.setAdapter(adapter);
列表是一个对象,您需要创建它的实例。
List list = ....
答案 1 :(得分:0)
写
ListView list;
而不是
ListView List;
并写下
Intent myintent = new Intent(MainActivity.this, Main2Activity.class);
而不是
Intent myintent = new Intent(view.getContext(), Main2Activity.class);