【问题标题】:Android getIntent() Nullpointer ExceptionAndroid getIntent() 空指针异常
【发布时间】:2014-05-29 15:25:56
【问题描述】:

在我的 MainActivity 中,我有一个 ListView 和一个 OnClickListenter。通过 Button 可以切换到另一个站点(addRecord.xml),在数据库(和 ListView)中添加一条新记录。

单击(并按住)项目时,您将进入与添加新记录时相同的站点。 (往上看)。 直到这里一切正常。 现在我尝试添加一个新的 Intent,因为 (addRecord.xml) 中的 EditTexts 应该填充被单击的 ListView 的 Item 的数据(longclick)。

Intent i = new Intent(getApplicationContext(),ActivityAddRecord.class);
                    i.putExtra("Name",  arrayList.get(position).getName());
                    i.putExtra("update", true);
                    startActivity(i);

在我添加的 ActivityAddRecord 类中:

  Intent i = getIntent();
       final boolean update = i.getExtras().getBoolean("update");
       if(update==true)
       {
           editText1.setText(getIntent().getExtras().getString("Name"));  
       }

现在,当单击并按住 ListView 项目时,您将进入 addrecord.xml 站点,并且 EditText 填充有名称。它工作正常。

但是当我想添加新记录(通过按钮单击)时,应用程序崩溃了。没有这 6 行代码,它不会崩溃。

在 LogCat 中,它在第 52 行显示 NullPointerException,即

final boolean update = i.getExtras().getBoolean("update");

我已经阅读了很多关于这种错误的主题,但是解决方案对我没有帮助

有什么想法吗?

【问题讨论】:

    标签: java android listview android-intent nullpointerexception


    【解决方案1】:

    替换final boolean update = i.getExtras().getBoolean("update");

    通过

    final boolean update = i.getBooleanExtra("update");

    还有editText1.setText(getIntent().getExtras().getString("Name"));

    通过

    editText1.setText(i.getStringExtra("Name"));

    【讨论】:

      【解决方案2】:

      你应该使用i.getBooleanExtrai.getStringExtra,因为你没有提供一个Bundle(你没有调用i.setExtras(Bundle)

      【讨论】:

        【解决方案3】:
           Intent i = getIntent();
           final boolean update =i.getBooleanExtra("update",false);
           if(update==true)
           {
               editText1.setText(getIntent().getExtras().getString("Name"));  
           }
        

        请根据您的要求使用它。它可以正常工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-31
          相关资源
          最近更新 更多