【问题标题】:How can i solve getApplicationContext() java.lang.NullPointerExeception我如何解决 getApplicationContext() java.lang.NullPointerException
【发布时间】:2015-04-21 05:21:27
【问题描述】:

我正在尝试使用 android 连接到 azure 应用程序,但出现此错误。这是我的代码和日志。

我已经检查了所有内容,并且看起来是正确的,但我认为问题在于getApplicationContext()。我无法调试。

有人可以帮帮我吗?

public class login extends Activity {

    UserServicesImpl usv;
    private EditText txtemail;
    private EditText txtpassword;


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

        txtemail = (EditText) findViewById(R.id.txtemail);
        txtpassword = (EditText) findViewById(R.id.txtpassword);
    }

    public void LoginKarrega(View view){

                usv = new UserServicesImpl();

                AlertDialog alertDialog = new AlertDialog.Builder(login.this).create();
                alertDialog.setTitle("Alert");

                if(usv.exist(txtemail.getText().toString(), txtpassword.getText().toString()) == true) {
                    alertDialog.setMessage("Sucessful login");
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                }
                else {
                    alertDialog.setMessage("wrong login");
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                }

                alertDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Logcat:

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
            at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
            at com.microsoft.windowsazure.mobileservices.notifications.MobileServicePush.<init>(MobileServicePush.java:134)
            at com.microsoft.windowsazure.mobileservices.MobileServiceClient.initialize(MobileServiceClient.java:1473)
            at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:182)
            at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:158)
            at ao.co.karrega.services.customers.UserServicesImpl.<init>(UserServicesImpl.java:39)
            at ao.co.karrega.login.LoginKarrega(login.java:32)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

【问题讨论】:

  • 发布您的 LogCat 输出
  • 请发布您的 LogCat 跟踪...
  • 我已经发布了。请帮帮我
  • 为什么要@Pooja?我正在显示活动和日志,您还需要什么?
  • 你试图获取getApplicationContext()的代码在哪里

标签: java android azure-mobile-services android-applicationinfo


【解决方案1】:

使用下面的代码来解决这个错误。目前您正在使用它而不是 getApplicationContext()。

try {
        mClient = new MobileServiceClient(
                "https://newvbdemo.azure-mobile.net/",
                "SUFYiOYEAlbEoKfsDgfWNIywaPSMhC29",
                getApplicationContext()

        );
        mTable = mClient.getTable("Users",Users.class);
    }catch (MalformedURLException e) {
        e.printStackTrace();
    }

【讨论】:

    【解决方案2】:

    您的班级UserServicesImpl extends Activity,但似乎不是Activity。

    特别是,活动通常没有显式构造函数,并且您不能在其onCreate() 生命周期方法之前使用Activity 作为Context。您的堆栈跟踪中的构造阶段&lt;init&gt; 为时过早。

    堆栈跟踪还显示您正在尝试在LoginKarrega 中使用new 实例化一个活动。永远不要用new 自己实例化一个活动。

    看起来您应该删除 extends Activity 并将 Context 作为参数传递。

    【讨论】:

    • 先生。 @laalto 我已经更新了,你能再看看吗?
    猜你喜欢
    • 2015-02-10
    • 2014-07-27
    • 2021-01-09
    • 2021-09-24
    • 2012-05-14
    • 2022-06-13
    • 1970-01-01
    • 2015-11-29
    • 2020-07-26
    相关资源
    最近更新 更多