07-30 09:20:22.401: E/AndroidRuntime(1748): FATAL EXCEPTION: main
07-30 09:20:22.401: E/AndroidRuntime(1748): java.lang.NullPointerException
07-30 09:20:22.401: E/AndroidRuntime(1748): at com.example.swiperefreshdemoprac.adapter.DemoAdapter.getView(DemoAdapter.java:78)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.ListView.makeAndAddView(ListView.java:1831)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.ListView.fillSpecific(ListView.java:1320)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.ListView.layoutChildren(ListView.java:1630)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.View.layout(View.java:14008)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:324)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.View.layout(View.java:14008)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.View.layout(View.java:14008)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.View.layout(View.java:14008)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.View.layout(View.java:14008)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewGroup.layout(ViewGroup.java:4373)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.os.Handler.handleCallback(Handler.java:725)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.os.Looper.loop(Looper.java:137)
07-30 09:20:22.401: E/AndroidRuntime(1748): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 09:20:22.401: E/AndroidRuntime(1748): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 09:20:22.401: E/AndroidRuntime(1748): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 09:20:22.401: E/AndroidRuntime(1748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 09:20:22.401: E/AndroidRuntime(1748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 09:20:22.401: E/AndroidRuntime(1748): at dalvik.system.NativeStart.main(Native Method)
07-30 09:22:37.049: E/Trace(1806): error opening trace file: No such file or directory (2)
我正在做什么我在BaseAdapter的帮助下在SwipeRefreshLayout中加载一些数据。完整代码如下。
这是主要的活动类。
MainActivity.java
package com.example.swiperefreshdemoprac.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.example.swiperefreshdemoprac.R;
import com.example.swiperefreshdemoprac.adapter.DemoAdapter;
import com.example.swiperefreshdemoprac.gettersetter.GetterSetter;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class MainActivity extends Activity implements OnItemClickListener,OnRefreshListener{
private SwipeRefreshLayout swipeRefreshLayout;
Bitmap VideoThumbImage = null;
ListView VideolistView;
DemoAdapter adapter;
ArrayList<GetterSetter> newArray = new ArrayList<GetterSetter>();
ArrayList<GetterSetter> newArray2 = new ArrayList<GetterSetter>();
public List<ParseObject> objectTest;
int initialnum = 0;
int finalnum = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
swipeRefreshLayout = new SwipeRefreshLayout(MainActivity.this);
setContentView(swipeRefreshLayout);
VideolistView = new ListView(MainActivity.this);
ParseQuery<ParseObject> newQuery = ParseQuery.getQuery("Videos");
newQuery.whereExists("url");
newQuery.addDescendingOrder("number");
newQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
int dataSize = list.size();
for (int i = initialnum; i < finalnum; i++) {
ParseFile Youtubeimage = (ParseFile) list.get(i).get(
"VideoImage");
byte[] imageconvert;
try {
imageconvert = Youtubeimage.getData();
VideoThumbImage = BitmapFactory.decodeByteArray(
imageconvert, 0, imageconvert.length);
} catch (ParseException ee) {
ee.printStackTrace();
}
String Title = (String) list.get(i).get("Title");
String urlYou = (String) list.get(i).get("url");
GetterSetter setItNow = new GetterSetter();
setItNow.setImage(VideoThumbImage);
setItNow.setTitle(Title);
setItNow.setUrl(urlYou);
newArray.add(setItNow);
}
adapter = new DemoAdapter(getApplicationContext(),
newArray);
VideolistView.setAdapter(adapter);
VideolistView.setOnItemClickListener(MainActivity.this);
objectTest = list;
initialnum = finalnum;
if((list.size()-finalnum)>5){
finalnum = finalnum+5;
} else {
finalnum = list.size() - finalnum;
}
} else {
e.printStackTrace();
}
}
});
Log.d("THis is ", "7");
Log.d("This is Testing", "Testing");
// Log.d("The size is", "This is the size :- "+objectTest.size());
swipeRefreshLayout.addView(VideolistView);
swipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
new Thread(){
public void run() {
SystemClock.sleep(2000);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"This is the object Length :- "+objectTest.size() +", This is the ini :- "+initialnum+", This si the final num" + finalnum, Toast.LENGTH_SHORT).show();
for (int i = initialnum; i < finalnum; i++) {
ParseFile Youtubeimage = (ParseFile) objectTest.get(i).get(
"VideoImage");
byte[] imageconvert;
try {
imageconvert = Youtubeimage.getData();
VideoThumbImage = BitmapFactory.decodeByteArray(
imageconvert, 0, imageconvert.length);
} catch (ParseException ee) {
ee.printStackTrace();
}
String Title = (String) objectTest.get(i).get("Title");
String urlYou = (String) objectTest.get(i).get("url");
GetterSetter setItNow2 = new GetterSetter();
setItNow2.setImage(VideoThumbImage);
setItNow2.setTitle(Title);
setItNow2.setUrl(urlYou);
newArray2.add(setItNow2);
}
Toast.makeText(MainActivity.this, "This is the size of the new Array :- " + newArray.size(), Toast.LENGTH_SHORT).show();
adapter.DemoAdapter2(MainActivity.this, newArray2);
// adapter.notifyDataSetChanged();
// adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
// VideolistView.setAdapter(adapter);
}
});
};
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
这是Adapter类
DemoAdapter.java
package com.example.swiperefreshdemoprac.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Binder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.swiperefreshdemoprac.R;
import com.example.swiperefreshdemoprac.gettersetter.GetterSetter;
import com.parse.RefreshCallback;
public class DemoAdapter extends BaseAdapter{
Context cont;
ArrayList<GetterSetter> refrehsetter;
ArrayList<GetterSetter> refrehsetter2;
LayoutInflater inflate;
public DemoAdapter(Context cont,ArrayList<GetterSetter> refrehsetter){
this.cont = cont;
this.refrehsetter = refrehsetter;
}
public void DemoAdapter2(Context cont,ArrayList<GetterSetter> newrefrehsetter) {
// TODO Auto-generated constructor stub
this.cont = cont;
refrehsetter2 = newrefrehsetter;
this.refrehsetter.addAll(refrehsetter2);
notifyDataSetChanged();
Toast.makeText(cont, "This is the size after call :- "+refrehsetter2.size(), Toast.LENGTH_SHORT).show();
// refrehsetter.addAll(refrehsetter2);
// notifyDataSetChanged();
}
public class Holder{
ImageView image;
TextView url, Title;
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder hold = null;
inflate = (LayoutInflater) cont.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(ConvertView == null){
ConvertView = inflate.inflate(R.layout.layout_trailer_item, null);
hold = new Holder();
hold.image = (ImageView) ConvertView.findViewById(R.id.imageView1);
hold.Title = (TextView) ConvertView.findViewById(R.id.textView1);
hold.url = (TextView) ConvertView.findViewById(R.id.urlID);
Toast.makeText(cont,"This is not the result", Toast.LENGTH_SHORT).show();
} else {
hold = (Holder) ConvertView.getTag();
Toast.makeText(cont,"<<<<<This is the result>>>>>", Toast.LENGTH_SHORT).show();
}
GetterSetter getset = (GetterSetter) getItem(position);
hold.image.setImageBitmap(getset.getImage());
hold.Title.setText(getset.getTitle());
hold.url.setText(getset.getUrl());
return ConvertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return refrehsetter.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return refrehsetter.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return refrehsetter.indexOf(getItem(arg0));
}
}
以下是Getter Setter。
GetterSetter.java
package com.example.swiperefreshdemoprac.gettersetter;
import android.graphics.Bitmap;
public class GetterSetter {
String url;
Bitmap image;
String title;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Here I am trying to load the data on refresh but when I swipe to refresh It is not loading the new data that I have got. Can anyone please tell me where am I going wrong.
答案 0 :(得分:2)
嘿,处理适配器标签时出错了。我没有设置标签,也没有将布局充气机设置在适当的位置。让我发布Adapter类。
package com.example.swiperefreshdemoprac.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.swiperefreshdemoprac.R;
import com.example.swiperefreshdemoprac.gettersetter.GetterSetter;
public class DemoAdapter extends BaseAdapter{
Context cont;
ArrayList<GetterSetter> refrehsetter;
LayoutInflater inflate;
public DemoAdapter(Context cont,ArrayList<GetterSetter> refrehsetter){
this.cont = cont;
this.refrehsetter = refrehsetter;
inflate = (LayoutInflater) cont.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
public void updateNewArray(ArrayList<GetterSetter> setit){
// refrehsetter.clear();
refrehsetter.addAll(setit);
notifyDataSetChanged();
}
public class Holder{
ImageView image;
TextView url, Title;
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder hold;
View view = ConvertView;
if(ConvertView == null){
view = inflate.inflate(R.layout.layout_trailer_item, null);
hold = new Holder();
hold.image = (ImageView) view.findViewById(R.id.imageView1);
hold.Title = (TextView) view.findViewById(R.id.textView1);
hold.url = (TextView) view.findViewById(R.id.urlID);
view.setTag(hold);
Toast.makeText(cont,"This is not the result", Toast.LENGTH_SHORT).show();
} else {
hold = (Holder) ConvertView.getTag();
Toast.makeText(cont,"<<<<<This is the result>>>>>", Toast.LENGTH_SHORT).show();
}
GetterSetter getset = (GetterSetter) getItem(position);
hold.image.setImageBitmap(getset.getImage());
hold.Title.setText(getset.getTitle());
hold.url.setText(getset.getUrl());
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return refrehsetter.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return refrehsetter.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return refrehsetter.indexOf(getItem(arg0));
}
}
答案 1 :(得分:0)
您是否收到更多数据?或者您正在获取更新数据?
如果您“刷新”列表,则不应将新列表添加到旧列表中,只需将适配器中的数据源设置为新刷新列表即可。
如果您获得更多数据,addAll()
方法会导致新数据添加到列表底部。