質問

Anyone have any idea why this don't works ? the startActivity(i);

Not Working

public class UiHelper {

 /**
 * About Dialog
 */
public static void showAboutDialog(Activity activity) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(R.string.about_title);

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(activity);
    final View dialogView = factory.inflate(R.layout.about_dialog, null);

    TextView versionText = (TextView) dialogView.findViewById(R.id.about_version);
    versionText.setText(activity.getString(R.string.about_version) + " " + getVersion(activity));

    builder.setView(dialogView);

    builder.setIcon(android.R.drawable.ic_dialog_info);
   /** builder.setNeutralButton(activity.getString(R.string.button_close),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });**/

    builder.setPositiveButton("Facebook",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
              String url = "http://www.facebook.com/page/";
              final Intent i = new Intent(Intent.ACTION_VIEW);
              i.setData(Uri.parse(url));
              startActivity(i); /** <<-- Error <<--**/

        }


    });


    builder.setNegativeButton("Website",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    String url = "http://www.website.com/";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }

            });
    AlertDialog question = builder.create();
    question.show();
}

but i try this will works

Working

AlertDialog.Builder alert = new AlertDialog.Builder(PTRmainActivity.this);
        alert.setTitle("About");
        alert.setMessage("Version 1.0.0");
        alert.setIcon(R.drawable.icon);
        alert.setPositiveButton("Facebook",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                String url = "http://www.facebook.com/page/";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);

            }
        });

        alert.setNegativeButton("Website",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                String url = "http://www.website.com/";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });

        alert.show();
役に立ちましたか?

解決

Make the function take in a final argument:

public static void showAboutDialog(final Activity activity) {

and then use that argument to start the Activity (static functions don't have access to any non-static instance methods:

builder.setPositiveButton("Facebook",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
              String url = "http://www.facebook.com/page/";
              final Intent i = new Intent(Intent.ACTION_VIEW);
              i.setData(Uri.parse(url));
              activity.startActivity(i); /** <<-- Error <<--**/

        }

他のヒント

if you above code is on different class means not in main activity's class then try this one in your not working code..

activity.startActivity(i);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top