我在尝试填充listView时遇到了一些问题。列表视图的条目有一个特殊的设计原因是youtube的视频。它们由两个字符串(标题和描述)和图像组成。我得到一个nullPointerException,但我还没有找到原因。
listVideos正确填充,使用与youtube连接的线程并使用JSON获取播放列表中的每个视频。 videosListView是一个普通的listView,R.layout.entry是一个包含图像的linearLayout和另一个linearLayout(视频标题和描述)。
YoutubeActivity类:
public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
YouTubePlayerView youTubeView;
String URL_VIDEO = "CaA-k1l0xa4";
String KEY_DEVELOPER = MYKEY;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos_layout);
YouTubePlayerView youtubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
Youtube youtube = new Youtube(getApplicationContext(), youtubeView, KEY_DEVELOPER);
//youtube.reproduce(URL_VIDEO);
ListView videosListView = (ListView) findViewById(R.id.listListView);
youtube.loadListView(videosListView, "PLOhl4anP1Mp1vJKmmqGAcu10h5OSbx4zf");
}
@Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
// TODO Auto-generated method stub
}
}
Youtube类(FillListView INSIDE)
public class Youtube{
YouTubePlayerView youtubeView;
String keyDeveloper;
ListView videosListView;
LinearLayout videoEntryView;
Context context;
String urlVideo;
public Youtube(Context context, YouTubePlayerView youtubeView, String keyDeveloper){
this.context = context;
this.youtubeView = youtubeView;
this.keyDeveloper = keyDeveloper;
}
public void loadListView(ListView videosListView, String channel){
this.videosListView = videosListView;
new Thread(new GetYouTubeUserVideosTask(myHandler, channel)).start();
}
Handler myHandler = new Handler(){
public void handleMessage(Message msg){
fillListView(msg);
}
};
private void fillListView(Message msg){
ArrayList<Video> listVideos = (ArrayList) msg.getData().get("VideosList");
videosListView.setAdapter(new AdapterList(context, R.layout.entry, listVideos){
@Override
public void onEntry(Object entry, View view) {
if (entry != null) {
TextView superiorText = (TextView) view.findViewById(R.id.textView_superior);
if (superiorText != null)
superiorText.setText(((Video) entry).getTitle());
TextView inferiorText = (TextView) view.findViewById(R.id.textView_inferior);
if (inferiorText != null)
inferiorText.setText(((Video) entry).getUrl());
ImageView thumb = (ImageView) view.findViewById(R.id.imageView_imagen);
String aux = ((Video)entry).getThumbUrl();
ImageDownloader downloader = new ImageDownloader();
downloader.download(aux, thumb);
}
}
});
private void fillListView(Message msg){
ArrayList<Video> listVideos = (ArrayList) msg.getData().get("VideosList");
videosListView.setAdapter(new AdapterList(context, R.layout.entry, listVideos){
@Override
public void onEntry(Object entry, View view) {
if (entry != null) {
TextView superiorText = (TextView) view.findViewById(R.id.textView_superior);
if (superiorText != null)
superiorText.setText(((Video) entry).getTitle());
TextView inferiorText = (TextView) view.findViewById(R.id.textView_inferior);
if (inferiorText != null)
inferiorText.setText(((Video) entry).getUrl());
ImageView thumb = (ImageView) view.findViewById(R.id.imageView_imagen);
String aux = ((Video)entry).getThumbUrl();
ImageDownloader downloader = new ImageDownloader();
downloader.download(aux, thumb);
}
}
});`
以防万一,adapterList类看起来像这样:
public abstract class AdapterList extends BaseAdapter{
private ArrayList<?> entries;
private int R_layout_IdView;
private Context context;
public AdapterList(Context context, int R_layout_IdView, ArrayList<?> entry){
super();
this.entries = entry;
this.R_layout_IdView = R_layout_IdView;
this.context = context;
}
@Override
public View getView(int posicion, View view, ViewGroup pariente) {
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R_layout_IdView, null);
}
onEntry (entries.get(posicion), view);
return view;
}
@Override
public int getCount() {
return entries.size();
}
@Override
public Object getItem(int position) {
return entries.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public abstract void onEntry(Object object, View view);
}
videos_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Blanco"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
/>
<TextView
android:id="@+id/tituloTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="6dp"
android:text="@string/title"
android:textColor="@color/Negro"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/listaListView"
android:layout_width="match_parent"
android:layout_height="136dp"
android:background="@color/Blanco"
android:paddingBottom="15dp" >
</ListView>
</LinearLayout>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.83"
/>
entry.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/entryListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView_imagen"
android:layout_width="50dp"
android:layout_height="50dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="@string/DescriptionImage"
android:src="@android:drawable/ic_menu_gallery" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_superior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/largeText"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp" />
<TextView
android:id="@+id/textView_inferior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/smallText"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
错误日志:
08-24 13:34:51.828: E/AndroidRuntime(7580): FATAL EXCEPTION: main
08-24 13:34:51.828: E/AndroidRuntime(7580): java.lang.NullPointerException
08-24 13:34:51.828: E/AndroidRuntime(7580): at guidelines.youtube.Youtube.fillListView(Youtube.java:67)
08-24 13:34:51.828: E/AndroidRuntime(7580): at guidelines.youtube.Youtube.access$0(Youtube.java:63)
08-24 13:34:51.828: E/AndroidRuntime(7580): at guidelines.youtube.Youtube$1.handleMessage(Youtube.java:59)
08-24 13:34:51.828: E/AndroidRuntime(7580): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 13:34:51.828: E/AndroidRuntime(7580): at android.os.Looper.loop(Looper.java:137)
08-24 13:34:51.828: E/AndroidRuntime(7580): at android.app.ActivityThread.main(ActivityThread.java:4645)
08-24 13:34:51.828: E/AndroidRuntime(7580): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 13:34:51.828: E/AndroidRuntime(7580): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 13:34:51.828: E/AndroidRuntime(7580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-24 13:34:51.828: E/AndroidRuntime(7580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-24 13:34:51.828: E/AndroidRuntime(7580): at dalvik.system.NativeStart.main(Native Method)
知道错误来自哪里?谢谢!
答案 0 :(得分:1)
解决了!经典nullpointer愚蠢的错误......我正在做ListView list = ListView videosListView =(ListView)findViewById(R.id.listListView);但是videos_layout.xml中的listView有ID listaListView ...该死的,我讨厌这些错误!浪费了太多时间。