gpt4 book ai didi

android - 出现错误 - java.lang.UnsupportedOperationException

转载 作者:行者123 更新时间:2023-11-29 16:10:59 28 4
gpt4 key购买 nike

我已经编写了一些代码来将网页中的数据设置到listview。我已成功读取网页并将必要的数据存储在 String 数组中。现在我想使用 ArrayAdapter 将其分配给 ListView,但数据未在 listview 中设置。请帮我解决错误:

完整日志 CAT

11-07 23:41:40.150: E/AndroidRuntime(3400): FATAL EXCEPTION: main
11-07 23:41:40.150: E/AndroidRuntime(3400): java.lang.UnsupportedOperationException
11-07 23:41:40.150: E/AndroidRuntime(3400): at java.util.AbstractList.add(AbstractList.java:411)
11-07 23:41:40.150: E/AndroidRuntime(3400): at java.util.AbstractList.add(AbstractList.java:432)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
11-07 23:41:40.150: E/AndroidRuntime(3400): at com.air.test.Smscollection$sendMessageAsync.onPostExecute(Smscollection.java:91)
11-07 23:41:40.150: E/AndroidRuntime(3400): at com.air.test.Smscollection$sendMessageAsync.onPostExecute(Smscollection.java:1)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.os.Looper.loop(Looper.java:130)
11-07 23:41:40.150: E/AndroidRuntime(3400): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-07 23:41:40.150: E/AndroidRuntime(3400): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 23:41:40.150: E/AndroidRuntime(3400): at java.lang.reflect.Method.invoke(Method.java:507)
11-07 23:41:40.150: E/AndroidRuntime(3400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-07 23:41:40.150: E/AndroidRuntime(3400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-07 23:41:40.150: E/AndroidRuntime(3400): at dalvik.system.NativeStart.main(Native Method)

logcat 的输出以验证数据是否存储在 String array

11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfies
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies
11-07 23:03:47.640: I/VALUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfies<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies

代码

public class Smscollection extends Activity {

private Spinner spinner1;
private ProgressDialog pd;
private StringBuilder response;
private ListView listView;
private String[] values = new String[0];
private ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smscollection);
listView = (ListView) findViewById(R.id.mylist);
spinner1 = (Spinner) findViewById(R.id.spinnerCategory);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> av, View view, int id,
long ids) {
new sendMessageAsync().execute();
}

public void onNothingSelected(AdapterView<?> av) {
}
});

adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_smscollection, menu);
return true;
}

private class sendMessageAsync extends AsyncTask<Void, Void, String> {

@Override
protected void onPreExecute() {
pd = ProgressDialog.show(Smscollection.this, null, "Loading...",
true, true);
}

@Override
protected void onCancelled() {
Toast.makeText(getApplicationContext(),
"Message Sending Cancelled", Toast.LENGTH_LONG).show();
}

@Override
protected String doInBackground(Void... arg0) {
try {
return doInBg();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
pd.dismiss();
if (result == null) {
// request failed!
// return;
}
values = String.valueOf(result).split("<br/>");
adapter.clear();
for (String str : values) {
adapter.add(str);
}
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
}

public String doInBg() {
String responseRes = null;
try {
final String msgURL = "http://freesmsit.tk/msg/messages.php?category="
+ String.valueOf(spinner1.getSelectedItem().toString()
.replace(" ", "%20"));
URLConnection connection = new URL(msgURL).openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream responseStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
responseRes = response.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return responseRes;
}
}

最佳答案

如果您使用array 作为ArrayAdapter 的数据,该数组将被转换为一个特殊的ArrayList(不是来自平台),它没有实现 addremove 方法(您只能修改该列表中已经存在的项目)。因此,当您尝试使用 add 方法将项目添加到适配器时,应用程序将失败并出现您看到的异常。

解决方案是将您在 ArrayAdapter 中使用的 values 数组替换为 List,例如 ArrayList(正常的)。

private ArrayList<String> values = new ArrayList<String>();

然后修改其余代码以使用列表而不是数组。

关于android - 出现错误 - java.lang.UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13348019/

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