Java 致命异常:AsyncTask#1进程
我试着做一个像twitter一样的无限滚动,我使用的是Android无限滚动列表视图,但总是出现这个错误Java 致命异常:AsyncTask#1进程,java,android,multithreading,android-asynctask,scrollview,Java,Android,Multithreading,Android Asynctask,Scrollview,我试着做一个像twitter一样的无限滚动,我使用的是Android无限滚动列表视图,但总是出现这个错误 FATAL EXCEPTION: AsyncTask #1 Process: candel.ramon.cloudy, PID: 14907 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:30
FATAL EXCEPTION: AsyncTask #1
Process: candel.ramon.cloudy, PID: 14907
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at candel.ramon.cloudy.TablonCloudy$1$1.doInBackground(TablonCloudy.java:75)
这是我的班级:
public class TablonCloudy extends Activity {
// A setting for how many items should be loaded at once from the server
private static final int SEVER_SIDE_BATCH_SIZE = 10;
private InfiniteScrollListView infiniteScroll;
private TablonAdapter tablonAdapter;
// cambiar nombre y datos de esta clase
private BogusRemoteService bogusRemoteService;
private Handler handler;
private AsyncTask<Void, Void, List<String>> fetchAsyncTask;
private Map<String, Integer> sushiMappings;
// formas de cargar
private LoadingMode loadingMode = LoadingMode.SCROLL_TO_TOP;
private StopPosition stopPosition = StopPosition.START_OF_LIST;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tablon_cloudy);
handler = new Handler();
infiniteScroll = (InfiniteScrollListView) this
.findViewById(R.id.infinite_scrollview);
infiniteScroll.setLoadingMode(loadingMode);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
infiniteScroll.setLoadingView(layoutInflater.inflate(
R.layout.loading_view, null));
tablonAdapter = new TablonAdapter(new NewPageListener() {
@Override
public void onScrollNext() {
fetchAsyncTask = new AsyncTask<Void, Void, List<String>>() {
@Override
protected void onPreExecute() {
// Loading lock to allow only one instance of loading
tablonAdapter.lock();
}
@Override
protected List<String> doInBackground(Void... params) {
List<String> result;
// Mimic loading data from a remote service
if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
result = bogusRemoteService
.getNextMessageBatch(SEVER_SIDE_BATCH_SIZE);
} else {
result = bogusRemoteService
.getNextSushiBatch(SEVER_SIDE_BATCH_SIZE);
}
return result;
}
@Override
protected void onPostExecute(List<String> result) {
if (isCancelled() || result == null || result.isEmpty()) {
tablonAdapter.notifyEndOfList();
} else {
// Add data to the placeholder
if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
tablonAdapter.addEntriesToTop(result);
}
// Add or remove the loading view depend on if there
// might be more to load
if (result.size() < SEVER_SIDE_BATCH_SIZE) {
tablonAdapter.notifyEndOfList();
} else {
tablonAdapter.notifyHasMore();
}
// Get the focus to the specified position when
// loading completes
if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
infiniteScroll
.setSelection(result.size() < SEVER_SIDE_BATCH_SIZE ? 0
: 1);
}
}
};
@Override
protected void onCancelled() {
// Tell the adapter it is end of the list when task is
// cancelled
tablonAdapter.notifyEndOfList();
}
}.execute();
}
@Override
public View getInfiniteScrollListView(int position,
View convertView, ViewGroup parent) {
// Customize the row for list view
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.row_demo,
null);
}
String name = (String) tablonAdapter.getItem(position);
if (name != null) {
TextView rowName = (TextView) convertView
.findViewById(R.id.row_name);
ImageView rowPhoto = (ImageView) convertView
.findViewById(R.id.row_photo);
rowName.setText(name);
if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
rowPhoto.setImageResource(position % 2 == 0 ? R.drawable.conversation_driver
: R.drawable.conversation_officer);
} else {
rowPhoto.setImageResource(sushiMappings.get(name));
}
}
return convertView;
}
});
infiniteScroll.setAdapter(tablonAdapter);
// Display a toast when a list item is clicked
infiniteScroll.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(
TablonCloudy.this,
tablonAdapter.getItem(position) + " "
+ getString(R.string.ordered),
Toast.LENGTH_SHORT).show();
}
});
}
});
}
公共类TablonCloudy扩展活动{
//一次应从服务器加载多少项的设置
专用静态最终内部服务器端批量大小=10;
私有无限滚动列表视图无限滚动;
私人小报适配器小报适配器;
//cambiar nombre y datos de esta clase
专用BogusRemoteService BogusRemoteService;
私人经办人;
私有异步任务获取异步任务;
私有地图映射;
//卡加形式
私有加载模式加载模式=加载模式。滚动到顶部;
私有StopPosition StopPosition=StopPosition.START\u的\u列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u tablon\u cloudy);
handler=新的handler();
InfiniteSroll=(InfiniteSrollListView)此
.findViewById(R.id.infinite\u滚动视图);
无限滚动。设置加载模式(加载模式);
LayoutInflater LayoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
infiniteScroll.setLoadingView(布局、平坦、充气(
R.layout.loading_视图,空);
tablonAdapter=newtablonadapter(newnewpagelistener()){
@凌驾
公共无效onScrollNext(){
fetchAsyncTask=newAsyncTask(){
@凌驾
受保护的void onPreExecute(){
//加载锁只允许一个加载实例
tablonAdapter.lock();
}
@凌驾
受保护列表doInBackground(无效…参数){
列出结果;
//模拟从远程服务加载数据
如果(loadingMode==loadingMode.滚动到顶部){
结果=bogusRemoteService
.getNextMessageBatch(服务器端批量大小);
}否则{
结果=bogusRemoteService
.GetNextushiBatch(服务器端批量大小);
}
返回结果;
}
@凌驾
受保护的void onPostExecute(列表结果){
if(isCancelled()|| result==null | | result.isEmpty()){
tablonAdapter.notifyEndOfList();
}否则{
//将数据添加到占位符
如果(loadingMode==loadingMode.滚动到顶部){
表1.ADENTRIESTOTOP(结果);
}
//添加或删除加载视图取决于是否存在
//可能更容易加载
if(result.size() TablonCloudy.java:75