Java 运算符 != 未定义参数类型 long, null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21152976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:57:05  来源:igfitidea点击:

The operator != is undefined for the argument type(s) long, null

javaandroidlistview

提问by WardaLyn

I am beginner android developer, and this is my first project. I should have to display selected data from ListView on ViewData.java to ListView on EntryTO.java. First, I get an error nullPointerException. I try to detect where is the problem with if function. and that error appears. "The operator != is undefined for the argument type(s) long, null"

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

This is the code when I try to get data from listview ViewData.java

这是我尝试从 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);
  }

And this the code when I try to display selected data from ListView ViewData.java to another listview on EntryTO.java

这是我尝试将 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);
       }

Please help me.

请帮我。

采纳答案by niushuai

You can use the Long parameter. Like this:

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

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

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

回答by John3136

bun.getLong("id")!=nullmakes no sense. You can't have a "null long".

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

Something like bun.getLong("id")!=0Lmight make more sense (assuming 0 is the "default" value...) You are also repeating all the getLong()and getString()calls. More efficient to call them once and store the returned values for later use.

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