当前位置: 首页 > 知识库问答 >
问题:

我已经添加FLAG_ACTIVITY_NEW_TASK标志,但我仍然得到“活动上下文之外需要”错误

柳鸿博
2023-03-14

当我调用PublicParFun时,仍然会出现错误“从活动上下文外部调用startActivity()需要标志_Activity_NEW_TASK标志”。ShareAPP(getApplicationContext()) 我添加了FLAG_ACTIVITY_NEW_TASK FLAG。

顺便说一句,PublicParFun。ShowMainUI(getApplicationContext())可以很好地工作,为什么?

卡勒明。JAVA

package ui;

import info.dodata.callernametalker.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import bll.PublicParFun;

public class CallerMain extends Activity {


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

    private void SetButtons() {
        findViewById(R.id.btnSettings).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PublicParFun.ShowMainUI(getApplicationContext());
            }
        });

        findViewById(R.id.btnShare).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PublicParFun.ShareAPP(getApplicationContext());
            }
        });     

        findViewById(R.id.btnClose).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                finish();
            }
        });     
    }


}

公共公园。JAVA

package bll;

public class PublicParFun {

    public static void ShowMainUI(Context myContext){
        Intent intent = new Intent().setClass(myContext,ui.CallerMain.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
        myContext.startActivity(intent);
    }


    public static void ShareAPP(Context myContext){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 

        intent.putExtra(Intent.EXTRA_TITLE, myContext.getString(R.string.ShareTitle));
        intent.putExtra(Intent.EXTRA_SUBJECT, myContext.getString(R.string.ShareTitle));
        intent.putExtra(Intent.EXTRA_TEXT, myContext.getString(R.string.ShareContent));

        try {
            myContext.startActivity(Intent.createChooser(intent,
                    myContext.getString(R.string.defautlClient)));

        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(myContext,
                    myContext.getString(R.string.defautlNoClient), Toast.LENGTH_SHORT)
                    .show();
        }   
    }


}

共有2个答案

景俊拔
2023-03-14

尝试在Manisfest上添加标志。

端木宏盛
2023-03-14

在CallerMain活动类中

而不是内部的getApplicationContext()

PublicParFun.ShowMainUI(getApplicationContext());

你ain.this

PublicParFun.ShowMainUI(CallerMain.this);
 类似资料: