我使用onSaveInstanceState()
保存ListView信息,onRestoreInstanceState()
包含用户更改方向。
但是,当用户移动到其他活动时,我希望删除ListView信息,因此我使用clear();
方法,但出于某种原因,我使用clear();
方法收到错误。
LogCat:
07-25 10:00:59.326: E/AndroidRuntime(940): FATAL EXCEPTION: main
07-25 10:00:59.326: E/AndroidRuntime(940): java.lang.RuntimeException: Unable to pause activity {com.example.project/com.example.project.InternetActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread.access$2500(ActivityThread.java:125)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.os.Looper.loop(Looper.java:123)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-25 10:00:59.326: E/AndroidRuntime(940): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 10:00:59.326: E/AndroidRuntime(940): at java.lang.reflect.Method.invoke(Method.java:521)
07-25 10:00:59.326: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-25 10:00:59.326: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-25 10:00:59.326: E/AndroidRuntime(940): at dalvik.system.NativeStart.main(Native Method)
07-25 10:00:59.326: E/AndroidRuntime(940): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-25 10:00:59.326: E/AndroidRuntime(940): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-25 10:00:59.326: E/AndroidRuntime(940): at java.util.ArrayList.get(ArrayList.java:311)
07-25 10:00:59.326: E/AndroidRuntime(940): at com.example.project.InternetActivity.onSaveInstanceState(InternetActivity.java:68)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.Activity.performSaveInstanceState(Activity.java:1036)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
07-25 10:00:59.326: E/AndroidRuntime(940): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3330)
07-25 10:00:59.326: E/AndroidRuntime(940): ... 12 more
InternetActivity.java - 我没有添加所有的.java文件,因为它很长。 Instand I刚刚添加了问题。
package com.example.project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class InternetActivity extends ListActivity implements OnClickListener
{
final static String TAG = "InternetActivity";
static List<Movie> moviesList = new ArrayList<Movie>();
ArrayAdapter<Movie> adapter;
static public Movie[] moviesArray;
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection);
ListView list = getListView();
Button cancelButton = (Button) findViewById(R.id.BtnCancel);
cancelButton.setOnClickListener(this);
Button goButton = (Button) findViewById(R.id.BtnGo);
goButton.setOnClickListener(this);
adapter = new ArrayAdapter<Movie>(this, R.layout.itemlist_movie_internet, R.id.textTitle, moviesList);
list.setAdapter(adapter);
registerForContextMenu(list);
list.setOnItemClickListener(this);
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
for (int i = 0; i < moviesArray.length; i++)
{
Movie m = moviesList.get(i);
outState.putString("title"+""+i,m.getSubject());
outState.putString("body"+""+i,m.getBody());
outState.putString("url"+""+i,m.getUrl());
}
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
for (int i = 0; i < moviesArray.length; i++)
{
state.getString("title"+""+i);
state.getString("body"+""+i);
state.getString("url"+""+i);
}
}
@Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.BtnCancel: //Move back to main activity
moviesList.clear(); //PROBLEM
Intent intent = new Intent(InternetActivity.this, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
感谢。
答案 0 :(得分:0)
您正在查看moviesArray的长度,但尝试从moviesList获取元素。也许您想使用moviesList.length?
以下是有问题的代码:
for (int i = 0; i < moviesArray.length; i++)
{
Movie m = moviesList.get(i);
答案 1 :(得分:0)
您没有初始化moviesArray
。因此,要么在使用它之前初始化它,要么在代码更改中从moviesList
获取元素
moviesArray.length
到moviesList.size()
,即
更改
for (int i = 0; i < moviesArray.length; i++)
到
for (int i = 0; i < moviesList.size(); i++)
在将其清除为
之前检查moviesList
的大小
if(moviesList != null && moviesList.size() > 0)
moviesList.clear();
答案 2 :(得分:0)
更改此行
您的代码中的“ moviesList.size()”而不是电影Array_length
for (int i = 0; i < moviesList.size(); i++)
{
Movie m = moviesList.get(i);
outState.putString("title"+""+i,m.getSubject());
outState.putString("body"+""+i,m.getBody());
outState.putString("url"+""+i,m.getUrl());
}