startActivityForResult 空指针异常。语境?

发布于 2024-11-10 01:28:58 字数 4612 浏览 1 评论 0原文

冒着赢得 StackOverloads“年度笨蛋奖”的风险,我仍然遇到了这个问题(叹气)。我关于此的其他帖子是:

调用来自子菜单:

public boolean onOptionsItemSelected(MenuItem item){
        pob = new Prefs();// public Prefs pob; in field vars
    switch (item.getItemId()){
//-------------------------------------Options menu----------------------------------------
    case R.id.about:
        //Toast.makeText(this, "About menu", Toast.LENGTH_SHORT).show();
        //showAbout();  
        return true;
    case R.id.locale:
        //Toast.makeText(this, "Locale menu", Toast.LENGTH_SHORT).show();
        return true;

//-----Sub menu----------
    case R.id.uk_item://<-- USING THIS BLOCK FOR CALL
    Toast.makeText(this, "UK selected in UsFM", Toast.LENGTH_SHORT).show();// this is ok
        Log.i(LogTag, "UK selected");
        if(pob.isUk&&item.isChecked()){
            item.setChecked(true);
        }else item.setChecked(false);
        pob.changeLocale(this,"uk");
        //finish();
        return true;
    case R.id.us_item:
        //Toast.makeText(this, "US already selected", Toast.LENGTH_SHORT).show();
        if(pob.isUs&&item.isChecked()){
            item.setChecked(true);
        }
        else item.setChecked(false);
        pob.setRegion("us");
        pob.getRegion(this);
        finish();
        return true;
    case R.id.eu_item:
        Toast.makeText(this, "EU selected", Toast.LENGTH_SHORT).show();
        if(pob.isEu&&item.isChecked()){
            item.setChecked(true);
//          pob.changeLocale("eu");
        }else item.setChecked(false);
        return true;
    default :
        return super.onOptionsItemSelected(item);

        }

它在 Prefs 类中调用此方法:

public void changeLocale(Context cxt,String locale){
    try{
    String l=locale;
     if(l.equals("uk")){
        this.isUk=true;
        Log.i(Log_tag,l);
        //Toast.makeText(this, "UK region selected in Prefs",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(cxt, Uk.class);
        Log.i(Log_tag,"intent run");
        startActivity(intent);// <--- ERROR HERE
    }else if(l.equals("eu")){
        this.isEu=true;
        //Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(cxt, Eu.class);
        startActivity(intent);
    }else if(l.equals("us")){
        this.isUs=true;
        //Toast.makeText(this, "Us region selected", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(cxt, Us.class);
        startActivity(intent);
    }else if (l.equals("")){
        Log.i(Log_tag,"no locale passed in");
        finish();
    }
    }catch (NullPointerException e){
        e.printStackTrace();
        Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
        finish();
    }
}

堆栈跟踪为:

05-26 13:53:05.880: WARN/System.err(478): java.lang.NullPointerException
05-26 13:53:05.890: WARN/System.err(478):     at android.app.Activity.startActivityForResult(Activity.java:2661)
05-26 13:53:05.900: WARN/System.err(478):     at android.app.Activity.startActivity(Activity.java:2705)
05-26 13:53:05.900: WARN/System.err(478):     at com.silifeform.android.Prefs.changeLocale(Prefs.java:70)
05-26 13:53:05.900: WARN/System.err(478):     at       com.silifeform.android.Us.onOptionsItemSelected(UsFuelMoney.java:347)
05-26 13:53:05.910: WARN/System.err(478):     at android.app.Activity.onMenuItemSelected(Activity.java:2096)
05-26 13:53:05.910: WARN/System.err(478):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:825)
05-26 13:53:05.920: WARN/System.err(478):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)

05-26 13:53:05.920: WARN/System.err(478): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813) 05-26 13:53:05.920: WARN/System.err(478): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:120)

Stephan 非常友善地提出了一些关于实例化 prefs 对象的建议在 onOptionsItemSelected() 中,但由于我对 android 和 java 都是新手,所以我除了我已经尝试过的两种方法之外,不知道还有其他方法来实例化对象。

抱歉,如果我一遍又一遍地问同样的问题!

At the risk of winning StackOverloads "Dumbass of the Year Award" I'm still having problems with this (sigh). My other posts on this have been:

The call comes from a submenu:

public boolean onOptionsItemSelected(MenuItem item){
        pob = new Prefs();// public Prefs pob; in field vars
    switch (item.getItemId()){
//-------------------------------------Options menu----------------------------------------
    case R.id.about:
        //Toast.makeText(this, "About menu", Toast.LENGTH_SHORT).show();
        //showAbout();  
        return true;
    case R.id.locale:
        //Toast.makeText(this, "Locale menu", Toast.LENGTH_SHORT).show();
        return true;

//-----Sub menu----------
    case R.id.uk_item://<-- USING THIS BLOCK FOR CALL
    Toast.makeText(this, "UK selected in UsFM", Toast.LENGTH_SHORT).show();// this is ok
        Log.i(LogTag, "UK selected");
        if(pob.isUk&&item.isChecked()){
            item.setChecked(true);
        }else item.setChecked(false);
        pob.changeLocale(this,"uk");
        //finish();
        return true;
    case R.id.us_item:
        //Toast.makeText(this, "US already selected", Toast.LENGTH_SHORT).show();
        if(pob.isUs&&item.isChecked()){
            item.setChecked(true);
        }
        else item.setChecked(false);
        pob.setRegion("us");
        pob.getRegion(this);
        finish();
        return true;
    case R.id.eu_item:
        Toast.makeText(this, "EU selected", Toast.LENGTH_SHORT).show();
        if(pob.isEu&&item.isChecked()){
            item.setChecked(true);
//          pob.changeLocale("eu");
        }else item.setChecked(false);
        return true;
    default :
        return super.onOptionsItemSelected(item);

        }

It calls this method in the Prefs class:

public void changeLocale(Context cxt,String locale){
    try{
    String l=locale;
     if(l.equals("uk")){
        this.isUk=true;
        Log.i(Log_tag,l);
        //Toast.makeText(this, "UK region selected in Prefs",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(cxt, Uk.class);
        Log.i(Log_tag,"intent run");
        startActivity(intent);// <--- ERROR HERE
    }else if(l.equals("eu")){
        this.isEu=true;
        //Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(cxt, Eu.class);
        startActivity(intent);
    }else if(l.equals("us")){
        this.isUs=true;
        //Toast.makeText(this, "Us region selected", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(cxt, Us.class);
        startActivity(intent);
    }else if (l.equals("")){
        Log.i(Log_tag,"no locale passed in");
        finish();
    }
    }catch (NullPointerException e){
        e.printStackTrace();
        Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
        finish();
    }
}

The stack trace is:

05-26 13:53:05.880: WARN/System.err(478): java.lang.NullPointerException
05-26 13:53:05.890: WARN/System.err(478):     at android.app.Activity.startActivityForResult(Activity.java:2661)
05-26 13:53:05.900: WARN/System.err(478):     at android.app.Activity.startActivity(Activity.java:2705)
05-26 13:53:05.900: WARN/System.err(478):     at com.silifeform.android.Prefs.changeLocale(Prefs.java:70)
05-26 13:53:05.900: WARN/System.err(478):     at       com.silifeform.android.Us.onOptionsItemSelected(UsFuelMoney.java:347)
05-26 13:53:05.910: WARN/System.err(478):     at android.app.Activity.onMenuItemSelected(Activity.java:2096)
05-26 13:53:05.910: WARN/System.err(478):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:825)
05-26 13:53:05.920: WARN/System.err(478):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)

05-26 13:53:05.920: WARN/System.err(478): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813)
05-26 13:53:05.920: WARN/System.err(478): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:120)

Stephan very kindly made some suggestions regarding instantiating the prefs object in onOptionsItemSelected(), but as I am new to both android and java I don't know any other ways to instantiate an object other than the two I have already tried.

Sorry if I'm asking the same question over and over again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2024-11-17 01:28:59

我通常这样做:

Intent intent = new Intent(this, Eu.class);
startActivity(intent);

如果该位给出空指针,则意味着 context/this 为空或 Eu.class 为空。尝试使用调试模式遍历它以检查两者中的任何一个是否具有空值。
祝你好运!

I usually do this:

Intent intent = new Intent(this, Eu.class);
startActivity(intent);

If that bit gives a nullpointer, that means either the context/this is empty or the Eu.class is empty. Try to walk trough it with the debug mode to check if either of both has a null value.
Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文