【问题标题】:Why is the context null?为什么上下文为空?
【发布时间】:2019-03-31 07:04:10
【问题描述】:

我正在尝试使用默认 SMS 应用程序来实现发送 SMS 的代码,但我得到的是空上下文,我不明白为什么。

我已经尝试过 getBaseContext()、getContext()、getApplicationContext,但没有成功。

我做错了什么?

我正在 Moto E5 Android 8.1 Go 上测试此代码。

代码在 MainActivity.java 中

public void sendSMS(String msg)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
    {
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); //<---Line 107 --- Need to change the build to API 19

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/plain");
        sendIntent.putExtra(Intent.EXTRA_TEXT, msg);

        if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
        // any app that support this intent.
        {
            sendIntent.setPackage(defaultSmsPackageName);
        }
        startActivity(sendIntent);
    }
    else // For early versions, do what worked for you before.
    {
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        smsIntent.setType("vnd.android-dir/mms-sms");
        smsIntent.putExtra("address", SMSParameters.getTelefono(this));
        smsIntent.putExtra("sms_body",msg);
        startActivity(smsIntent);

    }
}

HomeFragment 类的一部分

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    final Button buttonAusente = view.findViewById(R.id.button_ausente);
    buttonAusente.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View view)
        {
            String message = null;

                //enviarComando AUSENTE

                //Line 70 below
            message = MessageGenerator.getMessageAusente(_password, _id, SMSParameters.getSecuencia(getContext())); //<-- Line 70
            new MainActivity().sendSMS(message);

        }
    });

    final Button buttonDesactivar = view.findViewById(R.id.button_desactivar);
    buttonDesactivar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
            String message = null;
           //enviarComando DESACTIVAR
            message = MessageGenerator.getMessageDesactivar(_password, _id, SMSParameters.getSecuencia(getContext()));
            new MainActivity().sendSMS(message);

        }
    });

    final Button buttonPresente = view.findViewById(R.id.button_presente);
    buttonPresente.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            String message = null;

               // enviarComando PRESENTE
            message = MessageGenerator.getMessagePresente(_password, _id, SMSParameters.getSecuencia(getContext()));
            new MainActivity().sendSMS(message);
        }
    });

    final Button buttonPanico = view.findViewById(R.id.button_panico);
    buttonPanico.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            String message = null;

            //  enviarComando(Comandos.PANICO);
            message = MessageGenerator.getMessagePanico(_password, _id, SMSParameters.getSecuencia(getContext()));
            new MainActivity().sendSMS(message);

        }
    });

    setStatusIcon(getContext(), Comandos.AUSENTE, view);
    setStatusIcon(getContext(), Comandos.DESACTIVAR, view);
    setStatusIcon(getContext(), Comandos.PRESENTE, view);
    setStatusIcon(getContext(), Comandos.PANICO, view);

    saveViewStatus(view);

    return view;
}

【问题讨论】:

  • 请将堆栈跟踪添加到您的问题中 - 另外,这段代码在哪里被调用?我们是在一个 Activity,一个 Service 中吗?
  • 也许我理解错了错误说“尝试在空对象引用上调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()'”
  • 我没有看到您在您提供的 sn-p 中调用 getApplicationContext()。您能否更新您的问题以提供我要求的信息?
  • 我用“this”替换,结果相同。请给我一分钟,我正在更新。
  • 这段代码在哪里调用?

标签: android android-context


【解决方案1】:

new MainActivity().sendSMS(message); - 这行不通。首先,您不通过其构造函数实例化活动,而是使用Intent,其次,因为您不想创建新活动,而是访问片段当前附加到的活动。

为了从您的 Fragment 与宿主 Activity 进行通信,您可以查看我的回答 here (see Methods 2 and 3)this sample projectAndroid Documentation 中也概述了这些技术。

【讨论】:

  • 我这样做是因为当我在 HomeFragment 类中创建方法时,我收到一个关于从非静态方法调用静态方法的错误,还有红色的 getContext() 和更多错误。现在我将阅读您的链接。
猜你喜欢
  • 2023-02-26
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2016-03-21
  • 1970-01-01
相关资源
最近更新 更多