gpt4 book ai didi

android - 我已经添加了 FLAG_ACTIVITY_NEW_TASK 标志,但我仍然收到 "outside of an Activity context requires"错误

转载 作者:行者123 更新时间:2023-11-30 02:45:49 26 4
gpt4 key购买 nike

当我调用 PublicParFun.ShareAPP(getApplicationContext()); 时,我仍然收到错误“从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志”我添加了 FLAG_ACTIVITY_NEW_TASK 标志。

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

CallerMin.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();
}
});
}


}

PublicParFun.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();
}
}


}

最佳答案

在 CallerMain Activity 类中

而不是里面的 getApplicationContext()

PublicParFun.ShowMainUI(getApplicationContext());

做 CallerMain.this 为

PublicParFun.ShowMainUI(CallerMain.this);

关于android - 我已经添加了 FLAG_ACTIVITY_NEW_TASK 标志,但我仍然收到 "outside of an Activity context requires"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25072365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com