模拟器崩溃“无效的int”

时间:2016-11-22 05:44:52

标签: java android

我已经陷入了我的代码,我无法进行。模拟器崩溃,堆栈指向第47行。请帮助初学者!

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Button obutton = (Button) findViewById(R.id.obutton);
        obutton.setOnClickListener(this);

        final Button btn = (Button) findViewById(R.id.button);
        final EditText edt = (EditText) findViewById(R.id.editText);
        final ListView listview = (ListView) findViewById(R.id.Lista);
        final ArrayList<String> list = new ArrayList<>();



            final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
            listview.setAdapter(adapter);
            btn.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                if (edt.getText().toString() != null) {
                    adapter.addItem(edt.getText().toString());
                }
                }
            });
    }

    private class StableArrayAdapter extends ArrayAdapter<String> {

        ArrayList<String> data = new ArrayList<>();
        private List mIdMap;


        public StableArrayAdapter(Context context, int textViewResourceId,
                                  List<String> objects) {
            super(context, textViewResourceId, objects);
            this.data = (ArrayList) objects;
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return (long) mIdMap.get(Integer.parseInt(item)) <-- Crashing 
        }

        public void addItem(String item) {
            data.add(item);
            notifyDataSetChanged();
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.obutton:
                Intent obama = new Intent(MainActivity3.this, MainActivity.class);
                startActivity(obama);
            case R.id.button:
                Intent test = new Intent(MainActivity3.this, Main6Activity.class);
                startActivityForResult(test, 1);

        }

    }


}

堆栈错误:

  

致命异常:主java.lang.NumberFormatException:无效的int:   java.lang.Integer.invalidInt(Integer.java:138)中的“t”

2 个答案:

答案 0 :(得分:1)

您正在解析无效的整数。字符串应仅包含数字,以将其解析为int。

目前您正在将“t”解析为整数,因此您会收到错误。项目应该类似于“0”,“1”或任何数字数字字符串。

答案 1 :(得分:0)

更改:

public async Task SetProfilingLevelAsync(IMongoDatabase database, int level)
{
    var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument("profile", level));
    await database.RunCommandAsync(command);
}

于:  @覆盖         public long getItemId(int position){             返回-1l; 让它发挥作用。