我是Android新手。
我正在尝试在Android开发站点上提供的示例GridView中构建一个上下文菜单。 Eclipse告诉我,如果有人可以帮助找到我编码的位置,则无法解析引用的项目。错了,那会很棒,因为我现在卡住了。谢谢。
我在下面发布了我的整个代码。我收到了错误消息:if (item.getItemId() == R.id.menu_facebook)
和另一个R.id.的
这是ShareGalleryView.java文件:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class ShareGalleryView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videogrid);
GridView vGrid=(GridView) findViewById(R.id.vgrid);
registerForContextMenu(vGrid);
vGrid.setAdapter(new VideoAdapter(this));
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_gallery_share, menu);
}
public boolean onContextItemSelected (MenuItem item){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if (item.getItemId() == R.id.menu_facebook)
{
//TODO open fb
return true;
}
else if (item.getItemId() == R.id.menu_youtube)
{
//TODO open youtube
return true;
}
else if (item.getItemId() == R.id.menu_email)
{
//TODO open email
return true;
}
else if (item.getItemId() == R.id.menu_bluetooth)
{
// TODO send via bluetooth
return true;
}
}
public class VideoAdapter extends BaseAdapter {
private Context mContext;
public VideoAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_2,
R.drawable.sample_6, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_1
};
}
}
这是menu_gallery_share.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_facebook"
android:title="@string/Facebook" />
<item android:id="@+id/menu_youtube"
android:title="@string/YouTube" />
<item android:id="@+id/menu_email"
android:title="@string/Email" />
<item android:id="@+id/menu_bluetooth"
android:title="@string/Via Bluetooth" />
</menu>
</menu>
答案 0 :(得分:2)
您的菜单xml文件定义不正确。 <menu>
只能有<item>
个和<group>
个。删除额外的<menu
&gt;你已经定义了它应该正常工作。您的项目可能也停止构建,您可以在Eclipse的Android控制台中检查它。 See this documentation for more information about the menu resource.
答案 1 :(得分:0)
Wohooo,它的工作原理:)第一个问题是在xml文件中,这是现在更正的代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_facebook"
android:title="Facebook" />
<item android:id="@+id/menu_youtube"
android:title="/YouTube" />
<item android:id="@+id/menu_email"
android:title="Email" />
<item android:id="@+id/menu_bluetooth"
android:title="Via Bluetooth" />
</menu>
对于我的第二个问题 - 未解析的aapt错误 - 我只需要删除错误并清理项目:)
感谢您的回复:)