2010-12-15 59 views
-1

我有一个MapView,与itemizedoverlays,酷似在Android开发者指南的例子:http://developer.android.com/resources/tutorials/views/hello-mapview.html可以从ItemizedOverlay类开始一个Intent吗? (MapView类项目)

在itemizedoverlay,我有一个个性化的对话,一个按钮。所有罚款直到这里,但现在我有问题试图添加功能的按钮。我需要按钮开始一个新的活动,但我无法实现....¿为什么?因为在这一行:i = new Intent (NyItemizedOverlay.class, Locate.class);我有当前的Intent类作为第一个参数,目标意图类在第二个参数。

MyItemizedOverlay不是一个Intent类...它是ItemizedOverlay扩展,然后当我尝试启动intent时,它不编译,我试图传递一个普通类作为第一个参数,并且它需要一个intent类。我必须把发射器类,但发射器类不是一个意图:S

如果我试图把第一个参数的另一个意图类,我得到这个错误:No enclosing instance of the type AllActivity is accessible in scope ....(AllActivity是一个公共我的应用程序的活动类)

我该如何解决这个问题?

完整的代码在这里:

public class MyItemizedOverlay extends ItemizedOverlay { 


private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
private ArrayList<String> permissions = new ArrayList<String>(); 
private Context mContext; 

public MyItemizedOverlay(Drawable defaultMarker) { 
    super(boundCenterBottom(defaultMarker)); 
} 

protected OverlayItem createItem(int i) { 
    return mOverlays.get(i); 
} 

public int size() { 
    return mOverlays.size(); 
} 

public void addOverlay(OverlayItem overlay) { 
    mOverlays.add(overlay); 
    populate(); 
} 
public void addOverlay(OverlayItem overlay,String permission) { 
    mOverlays.add(overlay); 
    permissions.add(permission); 
    populate(); 
} 

public MyItemizedOverlay(Drawable defaultMarker, Context context) { 
    //super(defaultMarker); 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
} 
public void clear() 
{ 
    mOverlays.clear(); 
    permissions.clear();//lista de permisos de cada usuario, ya que hay dos campos, el email (snippet) y el permission, una lista. 
} 

protected boolean onTap(int index) { 
    try{ 
    OverlayItem item = mOverlays.get(index); 
    if (permissions.size()==0) 
    {  
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
    dialog.setTitle(item.getTitle()); 
    dialog.setMessage(item.getSnippet()); 
    dialog.show(); 
    } 
    else 
    { 

    //set up dialog 
    Dialog dialog = new Dialog(mContext); 
    dialog.setContentView(R.layout.personal_dialog); 
    dialog.setTitle(item.getTitle()); 
    dialog.setCancelable(true); 
    //there are a lot of settings, for dialog, check them all out! 


    //set up text 
    TextView DialogEmail = (TextView) dialog.findViewById(R.id.DialogEmail); 
    TextView DialogPermission = (TextView) dialog.findViewById(R.id.DialogPermission); 
    DialogEmail.setText(item.getSnippet()); 
    DialogPermission.setText(permissions.get(index)); 

    final String userName=item.getTitle(); 
    final String email=item.getSnippet(); 
    final int cont=index; 
    //set up button 
    Button button = (Button) dialog.findViewById(R.id.DialogButton); 
    button.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //do something 
     Bundle bundle = new Bundle(); //bundle is like the letter 
     bundle.putString ("user",userName); //arg1 is the keyword of the txt, arg2 is the txt 
      bundle.putString ("email", email); 
      bundle.putString ("permission", permissions.get(cont)); 
      Intent i=null; 

      i = new Intent (MyItemizedOverlay.class, Locate.class); 

      i.putExtras(bundle); 
      startActivity(i); 
    } 
    }); 
    //now that the dialog is set up, it's time to show it  
    dialog.show(); 

    } 
    }catch(Exception e){} 
    return true; 
} 


} 

回答

4

Intent javadoc清楚地表明,第一个参数必须是(按活动扩展)上下文,第二个类你试图启动活动。在你的情况,你需要做的:

Intent intent = new Intent(mContext, AllActivity.class); 
mContext.startActivity(intent); 
+0

解决!非常感谢 – NullPointerException 2010-12-15 14:49:05

3

一般,意图获得在其构造Context作为第一个参数之一和Activity类作为第二个。所以,请执行下列操作:

Intent intent = new Intent(mContext, AllActivity.class); 
mContext.startActivity(intent); 
+0

它的工作原理!但现在我有另一个问题:S ....我需要知道变量fullName,电子邮件和权限,启动与该包的活动,但是当我按下按钮时访问这些变量,这些变量不存在......这是合乎逻辑的,这些变量的值是外部的,并且当按钮按下的监听器启动时会丢失......我怎样才能在按钮的监听器上获得这些变量值? – NullPointerException 2010-12-15 14:35:01

+1

ypu可以为您的意图使用putExtra()和getExtras()。但是,最好将答案标记为正确,并提出另一个问题或仅搜索活动之间的数据传递。 – 2010-12-15 14:36:38

+1

它不会丢失。使用'MyItemizedOverlay.this.name'等 – Falmarri 2010-12-15 14:37:17

0

为了意向构造应该通过实施这一类的应用程序包的Context,检查this。这不是意图。
它必须是一些活动,例如MyClass使用您的MyItemizedOverlay类。最好的办法是在Context处声明myContext,并在onCreate中声明其值为myContext = MyClass.this;。然后你可以声明这样的意图

new Intent (MyClass.myContext , Locate.class); 
相关问题