首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解析ContextMenu.ContextMenuInfo不能解析为类型?

如何解析ContextMenu.ContextMenuInfo不能解析为类型?
EN

Stack Overflow用户
提问于 2013-07-16 20:53:16
回答 1查看 1.4K关注 0票数 0

我是学习机器人的学生。

我只是在测试一些与我的教科书完全相同的代码。

但是..。发生了许多错误..。

我不知道他们为什么会出现..。

activity_context_menu.xml:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    />
<EditText android:id="@+id/edittext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EditText"
    />
<andexam.ver4_1.c09_munu.MyImage
    android:id="@+id/myimage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    />

我不知道安地塞米松.

我正在打字看我的书。

但我想错误不包括这个部分。

代码语言:javascript
复制
package com.example.contextmenu;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class ContextMenu extends Activity {
    Button mBtn;
    EditText mEdit;
    MyImage mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu);

        /*
        mBtn=(Button)findViewById(R.id.edittext);
        registerForContextMenu(mEdit);
        */

        registerForContextMenu(mBtn=(Button)findViewById(R.id.button));

        registerForContextMenu(mEdit=(EditText)findViewById(R.id.edittext));

        mImage=(MyImage)findViewById(R.id.myimage);
        registerForContextMenu(mImage);
    }

    public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        if(v==mBtn){
            menu.setTitle("Button Menu");
            menu.add(0,1,0,"red");
            menu.add(0,2,0,"green");
            menu.add(0,3,0,"blue");
        }
        if(v==mEdit){
            menu.add(0,4,0,"translation");
            menu.add(0,5,0,"writing");
        }
    }

    public boolean onContextItemSelected(MenuItem item){
        switch(item.getItemId()){
        case 1:
            mBtn.setTextColor(Color.RED);
            return true;
        case 2:
            mBtn.setTextColor(Color.GREEN);
            return true;
        case 3:
            mBtn.setTextColor(Color.BLUE);
            return true;
        case 4:
            Toast.makeText(this,"translated",Toast.LENGTH_SHORT).show();
            return true;
        case 5:
            Toast.makeText(this,"wrote",Toast.LENGTH_SHORT).show();
            return true;
        case 100:
            Toast.makeText(this,"rotated",Toast.LENGTH_SHORT).show();
            return true;
        case 101:
            Toast.makeText(this,"resized",Toast.LENGTH_SHORT).show();
            return true;
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.context_menu, menu);
        return true;
    }

}
class MyImage extends ImageView {
    public MyImage(Context context){
        super(context);
    }
    public MyImage(Context context, AttributeSet attrs){
        super(context, attrs);
    }
    public void onCreateContextMenu(ContextMenu menu){
        super.onCreateContextMenu(menu);

        menu.setTitle("MyImage Menu");
        menu.add(0,100,0,"image rotate");
        menu.add(0,101,0,"change image size");
    }
}

不能将ContextMenu.ContextMenuInfo解析为类型

方法add(int,String)未定义为ContextMenu类型

如何消除错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-17 00:43:52

我认为您在创建一个名为:公共类ContextMenu扩展活动的类时犯了错误

因为您使用的是现有类的相同名称: android.view.ContextMenu

编译器无法区分类和现有类。

可能的解决方案:尝试将类重命名(例如使用重构),使其类似于MyContextMenu。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17686776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档