Android 阵列适配器工作不正常

Android 阵列适配器工作不正常,android,arrays,android-arrayadapter,Android,Arrays,Android Arrayadapter,我使用数组适配器从用户输入填充listview。但是,array adpter将无法工作,并导致应用程序崩溃。我不知道为什么会这样,如果有人能解释一下原因,我将不胜感激。下面是我的类和我得到的错误 我的班级 import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import andr

我使用数组适配器从用户输入填充listview。但是,array adpter将无法工作,并导致应用程序崩溃。我不知道为什么会这样,如果有人能解释一下原因,我将不胜感激。下面是我的类和我得到的错误

我的班级

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AddScore extends AppCompatActivity {
    private ListView lv;
    private EditText player;
    private EditText description;
    private EditText winner;
    private EditText game;
    private Button btn3;
    private String TAG = "Hannah";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_score);
        // MySQLiteHelper db = new MySQLiteHelper(this);
        lv = (ListView) findViewById(R.id.lv);
        player = (EditText) findViewById(R.id.player);
        description = (EditText) findViewById(R.id.description);
        winner = (EditText) findViewById(R.id.winner);
        game = (EditText) findViewById(R.id.game);
        btn3 = (Button) findViewById(R.id.button3);


        btn3.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {
                List<String> sLog = new ArrayList<>();
                MySQLiteHelper Helper = new MySQLiteHelper(view.getContext());
                List sLogs = Helper.getAllScores();
                sLog.add(player.getText().toString().trim());
                sLog.add(description.getText().toString().trim());
                sLog.add(winner.getText().toString().trim());
                sLog.add(game.getText().toString().trim());


                for (int i = 0; i < sLog.size(); i++) {
                    Score score = new Score();
                    sLog.add(score.toString());
                }

                //ListView listView = (ListView) findViewById(R.id.lv);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, sLog);
                Log.d(TAG, "**************************");
                lv.setAdapter(arrayAdapter);
                Score score = new Score();
                arrayAdapter.add(String.valueOf(score));

                Toast.makeText(AddScore.this, "Button Clicked", Toast.LENGTH_SHORT).show();

            }
        });


    }
}
导入android.content.Intent;
导入android.os.Bundle;
导入android.app.Activity;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.Toast;
公共类AddScore扩展了AppCompative活动{
私有ListView lv;
私人编辑文本播放器;
私有文本描述;
私人编辑文本赢家;
私人文本游戏;
专用按钮btn3;
私有字符串TAG=“Hannah”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u score);
//MySQLiteHelper db=新的MySQLiteHelper(此);
lv=(ListView)findViewById(R.id.lv);
player=(EditText)findViewById(R.id.player);
description=(EditText)findViewById(R.id.description);
winner=(EditText)findViewById(R.id.winner);
game=(EditText)findViewById(R.id.game);
btn3=(按钮)findViewById(R.id.button3);
btn3.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
List sLog=new ArrayList();
MySQLiteHelper=newmysqlitehelper(view.getContext());
List sLogs=Helper.getAllScores();
添加(player.getText().toString().trim());
sLog.add(description.getText().toString().trim());
sLog.add(winner.getText().toString().trim());
sLog.add(game.getText().toString().trim());
对于(int i=0;i
错误 错误:(62,53)错误:无法推断ArrayAdapter的类型参数

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, sLog);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,sLog);

ArrayAdapter ArrayAdapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1,sLog);

您需要传递上下文。由于这是在闭包中,您需要显式地
getActivity()
,因为
不会为您提供上下文。

因为仅使用
指的是
视图。OnClickListener
类不是
活动
类。而
ArrayAdapter
需要
context
作为第一个参数,可以在这里由
activity
而不是
View.OnClickListener
提供

所以试着这样做:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddScore.this, android.R.layout.simple_list_item_1, sLog);
Log.d(TAG, "**************************");
lv.setAdapter(arrayAdapter);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(AddScore.this,android.R.layout.simple\u list\u item\u 1,sLog);
Log.d(标签为“*************************”);
低压设置适配器(阵列适配器);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddScore.this, android.R.layout.simple_list_item_1, sLog);
Log.d(TAG, "**************************");
lv.setAdapter(arrayAdapter);