【问题标题】:The operator != is undefined for the argument type(s) long, null运算符 != 未定义参数类型 long, null
【发布时间】:2014-01-16 03:47:20
【问题描述】:

我是初学者 android 开发者,这是我的第一个项目。我应该将 ViewData.java 上的 ListView 中的选定数据显示到 EntryTO.java 上的 ListView。首先,我收到一个错误 nullPointerException。我尝试检测 if 函数的问题在哪里。并出现该错误。 "操作符 != 未定义参数类型 long, null"

这是我尝试从 listview ViewData.java 获取数据时的代码

ListView lv = (ListView) findViewById(android.R.id.list);
          lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
          @Override
          public void onItemClick(final AdapterView<?> adapter, View v, int pos, final long id) {
          final Dialog dialog = new Dialog(ViewData.this);
          dialog.setContentView(R.layout.dialog_view);
          dialog.setTitle("Masukkan Qty");
          dialog.show();
          final product b = (product) getListAdapter().getItem(pos);
          edtqty = (EditText) dialog.findViewById(R.id.edtqty);
          buttonok = (Button) dialog.findViewById(R.id.buttonok);
          buttonok.setOnClickListener(new OnClickListener(){
              @Override
              public void onClick(View v) {
              switchToEdit(b.getId());
              Toast.makeText(ViewData.this, "Ambil product\n"+ b.getId() +"\n"+ b.getname() +"\n"+ b.getbrand()
              + edtqty.getText().toString(), Toast.LENGTH_LONG).show();
              dialog.dismiss();
                   };
             });
          }
      });
  public void switchToEdit(long id){
      product b = dataSource.getproduct(id);
      Intent i = new Intent(this, EntryTO.class);
      Bundle bun = new Bundle();

      bun.putLong("id", b.getId());
      bun.putString("brand", b.getbrand());
      bun.putString("qty",edtqty.getText().toString());
      i.putExtras(bun);
      finale();
      startActivity(i);
  }

当我尝试将 ListView ViewData.java 中的选定数据显示到 EntryTO.java 上的另一个列表视图时,这是代码

      Bundle bun = null;
       if(bun.getLong("id")!=null && bun.getString("brand")!=null && bun.getString("qty")!=null)
       {   bun = EntryTO.this.getIntent().getExtras();
           id = bun.getLong("id");
           brand = bun.getString("brand");
           qty = bun.getString("qty");

           String[]array ={brand,qty};

           adapter = new ArrayAdapter<String>(EntryTO.this, 
                  R.layout.item_list,R.id.edtnama, array);
           setListAdapter(adapter);
       }

请帮帮我。

【问题讨论】:

  • 您无法将原始longnull 进行比较。

标签: java android listview


【解决方案1】:

您可以使用 Long 参数。像这样:

if( bun.getLong("id") != 0L)

【讨论】:

    【解决方案2】:

    bun.getLong("id")!=null 毫无意义。你不能有一个“空长”。

    bun.getLong("id")!=0L 之类的东西可能更有意义(假设 0 是“默认”值...)您还重复了所有 getLong()getString() 调用。调用一次并存储返回值以供以后使用会更有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2018-06-26
      • 2018-06-30
      • 2018-04-10
      • 2021-07-03
      • 2017-06-04
      相关资源
      最近更新 更多