Android的呼叫意图空指针异常 [英] Android Call Intent Null pointer exception

查看:118
本文介绍了Android的呼叫意图空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林在我的智慧在这里结束。我有它实现了 OnClickListener COUS我需要上的按钮相同的动作accros我的应用程序类。过去,这工作得很好。但自从我从应用程序preferences得到一些所需的数据增加了一些功能。 startActivity 抛出一个空指针异常结果这里是类:

Im at my wits end here. I have a Class which Implements the OnClickListener cous i need the same action on Buttons accros my Application. This used to work just fine. But since I added some functionality by getting some needed data from the app preferences. startActivity throws a null pointer exception.
Here is the class:

//Imports
public class CallClickListener extends Activity implements View.OnClickListener {

    protected AppPreferences appPrefs;
    String contactPersonName;
    String contactPersonTelephone;
    String name;

    public CallClickListener(Context context){
        Log.d("TRACE", "init CallClick");
        appPrefs = new AppPreferences(context);

        try {

            JSONObject object = appPrefs.getConsultantObject();

            contactPersonName = object.getString("contactPersonName");
            contactPersonTelephone = object.getString("contactPersonTelephone");
            name = object.getString("name");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        final View v = view;

        AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
        alert.setTitle("Anrufen");
        alert.setMessage("Kontakt für " + name + ", " + contactPersonName + " anrufen");
        alert.setPositiveButton("Anrufen", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+contactPersonTelephone));
                startActivity(callIntent);// this line throws the exception
            }
        });
        alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
            }
        });
        alert.show();
    }
}

字符串都在那里从应用preFS,我也试图与硬编码PHONENUMBER只是柜面。警报工作正常,但只要我打的积极按钮,应用程序崩溃结果
我想补充监听器是这样的:

The Strings are all there from appPrefs, i also tried with hardcoding a phonenumber just incase. the Alert works fine, but as soon as i hit the positive Button, the app crashes
I add the Listener like this:

bCall.setOnClickListener(new CallClickListener(getApplicationContext()));

我添加了必要的呼叫权限。

I added the necessary Call permissions.

我是相当新的Andr​​oid开发人员,我错过什么?

I'm fairly new to Android dev, what am I missing?

推荐答案

做到这一点....让您在构造函数中传递到一个字段变量上下文对象。并改变startActivity为 context.startActivity 。它将工作呢。

Do this.... make the context object that you passed in the constructor into a field variable. and change startActivity to context.startActivity. It will work then.

编辑:突出了完整的解决方案。

Highlighting the full solution.

bCall.setOnClickListener(新CallClickListener(getApplicationContext()));

应改为 YourActivityClass.this 而不是 getApplicationContext 的。

启动活动在同一个任务不与上下文对象,它是不是一个活动工作。所以,你需要更改的背景下活动或你开始一个新的任务活动。也无需调用startActivity上提供给你的构造函数,你都拿到NPE的背景下,因为你的 CallClickListerner 没有上下文。

Start Activity in the same task does not work with a context object that is not an Activity. So you need to either change the context to Activity or you start the activity in a new task. Also without calling startActivity on the context provided to your constructor you were getting the NPE because your CallClickListerner has no context.

这篇关于Android的呼叫意图空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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