这是Carte.java(菜单界面对应的MainActivity)里的代码:
```
package com.example.schoolrestaurant;import java.util.ArrayList;
import java.util.List;import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;class StuAdapter extends ArrayAdapter<Student>
{
private int resource;
public StuAdapter(Context context,int resource,List<Student> objects)
{
super(context,resource,objects);
this.resource=resource;
}
public View getView(int position,View convertView,ViewGroup parent)
{
Student stu=getItem(position);
RelativeLayout imageListView;
if(convertView==null)
{
imageListView=new RelativeLayout(getContext());
LayoutInflater inflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(resource, imageListView,true);
}
else
{
imageListView=(RelativeLayout)convertView;
}
ImageView iv1=(ImageView)imageListView.findViewById(R.id.stu_image);
iv1.setImageResource(stu.getDrawableId());
TextView tvName=(TextView)imageListView.findViewById(R.id.tv_name);
tvName.setText(stu.getStuName());
TextView tvAge=(TextView)imageListView.findViewById(R.id.tv_age);
tvAge.setText("价格:"+stu.getAge());
int sexID=stu.getSex()==Student.FEMALE?R.drawable.eat:R.drawable.drink;
ImageView iv2=(ImageView)imageListView.findViewById(R.id.image_sex);
iv2.setImageResource(sexID);
return imageListView;
}
}public class Carte extends Activity {
ArrayList<Student> stus=new ArrayList<Student>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carte);
//int drawableId,String stuName,int age,int sex
stus.add(new Student(R.drawable.m01,"番茄牛腩饭",16,Student.FEMALE));
stus.add(new Student(R.drawable.m02,"香菇鸡肉饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m03,"红烧排骨饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m04,"咖喱鸡肉饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m05,"台湾卤肉饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m06,"海带鲜虾饭",16,Student.FEMALE));
stus.add(new Student(R.drawable.m07,"水煮牛肉饭",16,Student.FEMALE));
stus.add(new Student(R.drawable.m08,"可乐",4,Student.MALE));
stus.add(new Student(R.drawable.m09,"雪碧",4,Student.MALE));
stus.add(new Student(R.drawable.m10,"美年达",4,Student.MALE));
StuAdapter adapter=new StuAdapter(this,R.layout.list_item,stus);
ListView lv=(ListView)findViewById(R.id.listview);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.carte, menu);
return true;
}
}
```
这是activity_carte.xml(菜单界面设计)里的代码:```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Carte" > <ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView></LinearLayout>
```
这是list_item.xml(上面菜单界面引用的表单格式)里的代码:```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Carte" > <ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView></LinearLayout>
```
这是MainActivity.java(里面设置了几个导航标签)里的代码:```
package com.example.schoolrestaurant;import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost=getTabHost();
TabSpec tabSpec=tabHost.newTabSpec("tag1");
tabSpec.setIndicator("0");
tabSpec.setContent(new Intent(this,Carte.class));
tabHost.addTab(tabSpec);
TabSpec tabSpec1=tabHost.newTabSpec("tag2");
tabSpec1.setIndicator("1");
tabSpec1.setContent(new Intent(this,Order.class));
tabHost.addTab(tabSpec1);
tabHost.addTab(tabHost.newTabSpec("tag3").
setIndicator("2").
setContent(new Intent(this,My.class)));
RadioGroup radioGroup=(RadioGroup)findViewById(R.id.main_radiogroup);
radioGroup.setOnCheckedChangeListener(new TabCheckedChange());
}
class TabCheckedChange implements OnCheckedChangeListener{ @Override
public void onCheckedChanged(RadioGroup arg0, int checkedRadioButtonID) {
// TODO Auto-generated method stub
switch(checkedRadioButtonID){
case R.id.tab_icon_weixin:
tabHost.setCurrentTabByTag("tag1");
break;
case R.id.tab_icon_address:
tabHost.setCurrentTabByTag("tag2");
break;
case R.id.tab_icon_friend:
tabHost.setCurrentTabByTag("tag3");
break;
default:
break;
}
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
```
这是调试结果:
然后,鼠标一滑动首页表单(菜单)就出现这样的结果:
这是报错信息:
```
package com.example.schoolrestaurant;import java.util.ArrayList;
import java.util.List;import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;class StuAdapter extends ArrayAdapter<Student>
{
private int resource;
public StuAdapter(Context context,int resource,List<Student> objects)
{
super(context,resource,objects);
this.resource=resource;
}
public View getView(int position,View convertView,ViewGroup parent)
{
Student stu=getItem(position);
RelativeLayout imageListView;
if(convertView==null)
{
imageListView=new RelativeLayout(getContext());
LayoutInflater inflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(resource, imageListView,true);
}
else
{
imageListView=(RelativeLayout)convertView;
}
ImageView iv1=(ImageView)imageListView.findViewById(R.id.stu_image);
iv1.setImageResource(stu.getDrawableId());
TextView tvName=(TextView)imageListView.findViewById(R.id.tv_name);
tvName.setText(stu.getStuName());
TextView tvAge=(TextView)imageListView.findViewById(R.id.tv_age);
tvAge.setText("价格:"+stu.getAge());
int sexID=stu.getSex()==Student.FEMALE?R.drawable.eat:R.drawable.drink;
ImageView iv2=(ImageView)imageListView.findViewById(R.id.image_sex);
iv2.setImageResource(sexID);
return imageListView;
}
}public class Carte extends Activity {
ArrayList<Student> stus=new ArrayList<Student>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carte);
//int drawableId,String stuName,int age,int sex
stus.add(new Student(R.drawable.m01,"番茄牛腩饭",16,Student.FEMALE));
stus.add(new Student(R.drawable.m02,"香菇鸡肉饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m03,"红烧排骨饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m04,"咖喱鸡肉饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m05,"台湾卤肉饭",15,Student.FEMALE));
stus.add(new Student(R.drawable.m06,"海带鲜虾饭",16,Student.FEMALE));
stus.add(new Student(R.drawable.m07,"水煮牛肉饭",16,Student.FEMALE));
stus.add(new Student(R.drawable.m08,"可乐",4,Student.MALE));
stus.add(new Student(R.drawable.m09,"雪碧",4,Student.MALE));
stus.add(new Student(R.drawable.m10,"美年达",4,Student.MALE));
StuAdapter adapter=new StuAdapter(this,R.layout.list_item,stus);
ListView lv=(ListView)findViewById(R.id.listview);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.carte, menu);
return true;
}
}
```
这是activity_carte.xml(菜单界面设计)里的代码:```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Carte" > <ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView></LinearLayout>
```
这是list_item.xml(上面菜单界面引用的表单格式)里的代码:```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Carte" > <ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView></LinearLayout>
```
这是MainActivity.java(里面设置了几个导航标签)里的代码:```
package com.example.schoolrestaurant;import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost=getTabHost();
TabSpec tabSpec=tabHost.newTabSpec("tag1");
tabSpec.setIndicator("0");
tabSpec.setContent(new Intent(this,Carte.class));
tabHost.addTab(tabSpec);
TabSpec tabSpec1=tabHost.newTabSpec("tag2");
tabSpec1.setIndicator("1");
tabSpec1.setContent(new Intent(this,Order.class));
tabHost.addTab(tabSpec1);
tabHost.addTab(tabHost.newTabSpec("tag3").
setIndicator("2").
setContent(new Intent(this,My.class)));
RadioGroup radioGroup=(RadioGroup)findViewById(R.id.main_radiogroup);
radioGroup.setOnCheckedChangeListener(new TabCheckedChange());
}
class TabCheckedChange implements OnCheckedChangeListener{ @Override
public void onCheckedChanged(RadioGroup arg0, int checkedRadioButtonID) {
// TODO Auto-generated method stub
switch(checkedRadioButtonID){
case R.id.tab_icon_weixin:
tabHost.setCurrentTabByTag("tag1");
break;
case R.id.tab_icon_address:
tabHost.setCurrentTabByTag("tag2");
break;
case R.id.tab_icon_friend:
tabHost.setCurrentTabByTag("tag3");
break;
default:
break;
}
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
```
这是调试结果:
然后,鼠标一滑动首页表单(菜单)就出现这样的结果:
这是报错信息:
解决方案 »
- Android新手遇到一个小问题、望大神指教、
- 有android开发google map的兄弟吗?道路偏移怎么解决啊?
- 电容屏触摸不是很准 问题
- 我没有安装Eclipse,只是安装了SDK。做AP。
- 如何在系统的设置里放上自己的设置
- Android游戏设计,关于屏幕刷新的问题!
- cocos2d-x 2.2.3项目接入移动mm的SDK问题
- 问下在职场的android开发者是如何关闭程序的
- android 非root情况下,是否有办法实现app关机和重启shutdown,reboot
- getContext()出错
- android studio 编译 出现For input string: "<!"错误,看了2天,不知道是怎么回事,请大神帮忙
- android时间轴悬停
1.你这是虚拟机,可运行内存本身可能就很小。因此你使用真机可能就不会这么容易引发这一错误
2.你没有优化处理Bitmap加载图片的对象,这里你可以参考我的一篇博文https://blog.csdn.net/xj396282771/article/details/42146921
2.优化下布局。listview 宽高全部设为match_parent。
listAdapter 使用viewholder进行布局的加载(viewholder 的使用具体的百度下);
3.按照图片大小,将图片放到适合的drawable下比如说drawable-xhdpi
然后,你可以看看放进去的图片大小R.drawable.m01等;应该是你下载的吧,图片大小可能没注意…setImageResource非常容易OOM:
解决方法:
1、换图片框架,Glide比较大,对于你的项目,其实picasso就够了;
2、如果你要自己写的话,那么设置bitmap,其中先用options.inJustDecodeBounds=true;//不给图片分配内存,此时返回bitmap=null,但是可以得到图片的height,width和MIME类型。然后根据你的imageView大小再用options.inSampleSize=intSize;调整倍率,最终bitmap原图的1/intSize分辨率加载。第二次加载bitmap不要忘记options.inJustDecodeBounds=false;最后,就是list_item.xml贴错了……