如何从对话框启动活动? [英] How to launch an activity from a dialog?

查看:198
本文介绍了如何从对话框启动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何启动活动并从对话框按钮发送一个值?



这是我现在所拥有的。尝试了一些变体,但按下按钮时应用程序崩溃:

  dialog.setPositiveButton(查看配置文件,新的DialogInterface。 OnClickListener(){
public void onClick(DialogInterface dialog,int which){
Intent intent = new Intent();
intent.setClass(context,Profile.class);
intent.putExtra(profileID,8);
startActivity(intent);
dialog.cancel();
return;
}
});

全班:

  public class PlacesItemizedOverlay extends ItemizedOverlay {
private Context context;
private ArrayList&OverlayItem> items = new ArrayList&OverlayItem>();
私人活动aClass;

public PlacesItemizedOverlay(Context aContext,Drawable marker){
super(boundCenterBottom(marker));
context = aContext;
}

public void addOverlayItem(OverlayItem item){
items.add(item);
populate();
}

@Override
protected OverlayItem createItem(int i){
return(OverlayItem)items.get(i);
}

@Override
public int size(){
return items.size();
}

@Override
protected boolean onTap(int index){
aClass = new Activity();
OverlayItem item =(OverlayItem)items.get(index);
if(item.getTitle()!= null)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setPositiveButton(查看配置文件,
new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog,int which){
Intent intent = new Intent();
intent.setClass(context,Profile.class);
intent.putExtra(profileID,8);
aClass.startActivity(intent);
dialog.cancel();
return;
}
});
dialog.show();
}
返回true;
}
}

LogCat:

  06-24 10:35:31.253:WARN / dalvikvm(30118):threadid = 1:线程退出与未捕获的异常(组= 0x4001d5a0)
06 -24 10:35:31.283:ERROR / AndroidRuntime(30118):FATAL EXCEPTION:main
06-24 10:35:31.283:ERROR / AndroidRuntime(30118):java.lang.NullPointerException
06- 24 10:35:31.283:错误/ AndroidRuntime(30118):android.app.Activity.startActivityForResult(Activity.java:2901)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):at android.app.Activity.startActivity(Activity.java:3007)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):在com.example.android.test.PlacesItemizedOverlay $ 1.onClick(PlacesItemizedOverlay。 java:57)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):在com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:159)
06 -24 10:35:31.283:错误/ AndroidRuntime(30118):在android.os.Handler.dispatchMessag e(Handler.java:99)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):在android.os.Looper.loop(Looper.java:143)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):在android.app.ActivityThread.main(ActivityThread.java:4196)
06-24 10:35:31.283:错误/ AndroidRuntime(30118) .lang.reflect.Method.invokeNative(Native Method)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):java.lang.reflect.Method.invoke(Method.java:507)
06-24 10:35:31.283:错误/ AndroidRuntime(30118):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
06-24 10:35 :31.283:ERROR / AndroidRuntime(30118):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-24 10:35:31.283:ERROR / AndroidRuntime(30118):at dalvik.system.NativeStart.main(Native Method)
06-24 10:35:31.293:WARN / ActivityManager(1344):强制整理活动com.example.android.test / .SearchActivity


你确定你已经在这样的清单文件中添加了Profile.class:

 < activity android:name =。个人资料/> 


Any ideas how to launch an activity and send a value from a dialog button?

Here's what I have at the moment. Tried a number of variations but app crashes when the button is pressed:

dialog.setPositiveButton("View Profile", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent();
        intent.setClass(context, Profile.class);
        intent.putExtra("profileID",  "8");            
        startActivity(intent);
        dialog.cancel();
        return;
    } 
});

Full class:

public class PlacesItemizedOverlay extends ItemizedOverlay {
    private Context context;
    private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
    private Activity aClass;

    public PlacesItemizedOverlay(Context aContext, Drawable marker) {
        super(boundCenterBottom(marker));
        context = aContext;
    }

    public void addOverlayItem(OverlayItem item) {
        items.add(item); 
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (OverlayItem) items.get(i);
    }

    @Override
    public int size() {
        return items.size();
    }

    @Override
    protected boolean onTap(int index) {
        aClass = new Activity();
        OverlayItem item = (OverlayItem) items.get(index);
        if(item.getTitle() != null)
            {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setTitle(item.getTitle());
            dialog.setPositiveButton("View Profile",
                    new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent();
                    intent.setClass(context, Profile.class);
                    intent.putExtra("profileID",  "8");            
                    aClass.startActivity(intent);
                    dialog.cancel();
                    return;
                } 
            });
            dialog.show();
        }
        return true;
    }
}

LogCat:

06-24 10:35:31.253: WARN/dalvikvm(30118): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): FATAL EXCEPTION: main
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): java.lang.NullPointerException
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at android.app.Activity.startActivityForResult(Activity.java:2901)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at android.app.Activity.startActivity(Activity.java:3007)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at com.example.android.test.PlacesItemizedOverlay$1.onClick(PlacesItemizedOverlay.java:57)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at android.os.Looper.loop(Looper.java:143)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at android.app.ActivityThread.main(ActivityThread.java:4196)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at java.lang.reflect.Method.invoke(Method.java:507)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118):     at dalvik.system.NativeStart.main(Native Method)
06-24 10:35:31.293: WARN/ActivityManager(1344):   Force finishing activity com.example.android.test/.SearchActivity

解决方案

Did you check that you have added the Profile.class in the manifest file like such:

 <activity android:name=".Profile" />

这篇关于如何从对话框启动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆