【问题标题】:Generating a ListView Using ArrayAdapter [duplicate]使用 ArrayAdapter 生成 ListView [重复]
【发布时间】:2018-01-14 05:27:27
【问题描述】:

我正在尝试使用ArrayAdapter 填充ListView,我正在填充来自EditText 的输入。我的程序似乎编译得很好,但在应用程序启动期间它立即崩溃。我怀疑这与我尝试设置列表视图有关。我不确定我在初始化什么错误,以至于我的应用程序立即崩溃。任何和所有提示将不胜感激。提前致谢!

这是我的ArrayListAdapter 的全局声明。

ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);

ListView listView = (ListView) findViewById(R.id.dispScores);
listView.setAdapter(adapter);

我的适配器类:

 private class ScoreAdapter extends ArrayAdapter<scoreScreen> {
    private ScoreAdapter(Context context, ArrayList<scoreScreen> scores) {
        super(context, 0, scores);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        scoreScreen score1 = getItem(position);
        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_scores, parent, false);
        }
        TextView holeNum = (TextView) convertView.findViewById(R.id.holeNum);
        holeNum.setText(score1.hole);
        return convertView;
    }
}

我的ListView 在我的onCreate 方法中。

 ListView listview = (ListView) findViewById(R.id.listView1);
    listview.setAdapter(adapter);

我假设我的问题不在我的 EditText 输入内,因为它们在 OnClickListener 方法内,但以防万一我已将其附在下面。

public View.OnClickListener onClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        EditText input1 = (EditText) findViewById(R.id.scorePrompt);
        TextView output1 = (TextView) findViewById(R.id.textTotal);
        String blankCheck = input1.getText().toString(); //CHANGE INPUT IN scorePrompt TO STRING
        switch (view.getId()) {
            case R.id.buttTotal:
                    if (blankCheck.equals("")) {
                        Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
                        blankError.show();
                        break;
                     } else {
                        int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
                        int sum = num1 + score2;
                        score2 = sum;
                        output1.setText("Your score is : " + Integer.toString(sum));
                        //savedScores.add(input1.getText().toString());

                        scoreScreen addScore = new scoreScreen("Score is" + num1);
                        adapter.add(addScore);
                        j++;
                        input1.setText(""); //Clear input text box
                        break;
                    }
            case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
                output1.setText("you messed up");
                break;
            case R.id.editScore: //Need to set up Save Array before we can edit
                //CURRENTLY ONLY DISPLAYS THE LAST NUNMBER IN THE TEXTEDIT, NEED TO SET UP LISTVIEW!!!!!!
                for (int i=0; i < j; i++){
                //    output1.setText(savedScores.get(i));
                } break;
        }
    }
};

onCreate 按要求添加方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*Button scoresAct = (Button) findViewById(R.id.allScores);   //THIS IS TO GO TO ALL SCORES ACTIVITY
    scoresAct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent scoreScreen = new Intent(MainActivity.this, AllScoresAct.class);
            startActivity(scoreScreen);
        }
    });*/

    Button sumScores = (Button) findViewById(R.id.buttTotal);
    Button saveScores = (Button) findViewById(R.id.allScores);
    Button changeScores = (Button) findViewById(R.id.editScore);
    sumScores.setOnClickListener(onClickListener);
    saveScores.setOnClickListener(onClickListener);
    changeScores.setOnClickListener(onClickListener);

    ListView listview = (ListView) findViewById(R.id.listView1);
    listview.setAdapter(adapter);

}

将我的适配器和 ArrayList 移动到我的 onCreate 后,我收到一个新错误。我对空指针做了一些研究,但我已经初始化了这两个。下面是我的logcat,有什么想法吗?谢谢

【问题讨论】:

  • 把你的堆栈跟踪和错误放在这里
  • MainActivity.java 第 21 行是什么?您不能在 onCreate 方法之外创建new ScoreAdapter
  • 这是因为我的适配器在我的onCreate 之外声明。我已经移动了它,但现在收到一个空指针错误,但我不确定为什么,因为我相信我已经正确初始化了其他所有内容。

标签: android listview arraylist nullpointerexception android-arrayadapter


【解决方案1】:

确保 super.onCreate(savedInstanceState) 是您在 onCreate() 中调用的第一件事,并且不要尝试在 setContentView() 之前从布局 xml 访问任何视图。

编辑:

onCreate() 方法中初始化适配器,而不是在全局范围内进行。

onCreate() {
   .......
   this.adapter = new ScoreAdapter(this, savedScores);
}

【讨论】:

  • 对不起,我是新手。我已经更新了我的问题以包括我的onCreate。不确定您是否收到此通知,但如果您收到并且打扰您,我很抱歉!
  • 更新了答案。
【解决方案2】:

这是我的 ArrayList 和 Adapter 的全局声明。

ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);

您可以在全局范围内声明这些,但对于适配器,this 参数必须在onCreate 内部/之后使用,如错误所示

系统服务在onCreate()之前对Activity不可用

例如

private ArrayList<ScoreScreen> savedScores = new ArrayList<>();
private ScoreAdapter adapter;

@Override
protected void onCreate(Bundle b) {
    ...

    adapter = new ScoreAdapter(this, savedScores);
}

【讨论】:

  • 我已经将我的ArrayList 和我的adapter 都移到了我的onCreate 中,但应用程序在启动时仍然崩溃。此外,当我尝试在我的OnClickListener 方法期间添加到我的ArrayList 时,我收到一个错误(无论我的ArrayList 是在我的onCreate 内部还是在全局范围内声明都会发生这种情况。任何想法为什么?
  • 好吧,这个特定的代码修复了最初的错误。我无法回答为什么您的应用程序在没有看到您的堆栈跟踪的情况下崩溃
  • 我已将 logcat 添加到我的主要帖子中。我的 ListArray 似乎没有正确初始化,但我不知道原因。
  • 嗯,当您在实际上不包含您要查找的 id 的 XML 布局上使用 findViewById 时,通常会出现该错误。 findViewById 方法返回类型的文档中对此进行了解释
  • 嗨板球!你完全正确!我在 xml 和 java 文件之间来回切换,发现我在错误的 xml 中错误地引用了 ListView id。
猜你喜欢
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多