gpt4 book ai didi

java - arrayAdapter 导致应用程序强制关闭

转载 作者:行者123 更新时间:2023-12-02 00:13:45 25 4
gpt4 key购买 nike

我有一个 Activity 从另一个 Activity 加载sharedPreference,并从当我编译我的应用程序时,使用 arrayAdpter 将 sharePreferenece 放入 listView 中,并强制关闭这是我的代码:

public class DawaaActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button add;
ListView dataList;
private SharedPreferences emportPref;
String[] dawaaList;
String theName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialaiz();
emportPref = getSharedPreferences("dawaaData", MODE_PRIVATE);
theName = emportPref.getString("subject", "no data found");
Toast.makeText(this, theName, Toast.LENGTH_LONG).show();
dawaaList[0] = theName;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,dawaaList);
dataList.setAdapter(adapter);
}

private void initialaiz() {
// TODO Auto-generated method stub
add = (Button) findViewById(R.id.button3);
dataList = (ListView) findViewById(R.id.myListView);
add.setOnClickListener(this);
}

public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent(DawaaActivity.this,SettingActivity.class);
startActivity(data);
}

当我制作这两行时,如果代码提示:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,dawaaList);
dataList.setAdapter(adapter)

应用程序运行良好

但当然不填充listView

请帮帮我

最佳答案

我认为您忘记用适当的大小初始化String[]

而不是

String[] dawaaList;

尝试

String[] dawaaList = new String[1];

抛出异常的行是,

dawaaList[0] = theName;

关于java - arrayAdapter 导致应用程序强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com