我有一个名为Movies的课程,其中有几个属性,如评级/标题/日期。我查询api以返回有关这些属性的JSON数据。我的MAIN活动是一个ui,我输入电影,点击搜索,然后在列表视图中打开JSON数据的新意图。我可以获取所有json数据并从日志中读取它们,但我无法从列表视图中查看它们。这是我开始意图的方式..
try {
Intent intent = new Intent(getApplicationContext(),
ViewMovies.class);
intent.putParcelableArrayListExtra("movielist", moviesList);
startActivity(intent);
} catch (Exception e) {
Log.e("intent", "failed");
}
moviesList是一个ArrayList,它肯定记录了数据(我通过将它们打印到控制台来验证)。它可以说超过20件事。我的ViewMovies类正在扩展listview,一旦意图开始就会一直崩溃,我看到一个空指针异常,但不知道在哪里。
package com.example.jdmb;
import java.util.ArrayList;
import android.app.ListActivity;
import android.graphics.Movie;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.ArrayAdapter;
public class ViewMovies extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle a = getIntent().getExtras();
ArrayList<Movies> m = a.getParcelableArrayList("movielist");
setListAdapter(new ArrayAdapter<Movies>(ViewMovies.this, android.R.layout.simple_list_item_1, m));
}
}
我正在阅读和学习很多,但无法克服这个障碍。我不知道我做错了什么,我已经尝试重写ArrayAdapter,但我又不知道我在做什么,我只是复制了其他人的但是在这里它是否有帮助
package com.example.jdmb;
import java.util.ArrayList;
import android.content.ClipData.Item;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyClassAdapter extends ArrayAdapter<Movies> {
private ArrayList<Movies> movies;
public MyClassAdapter(Context context, int textViewResourceId,
ArrayList<Movies> objects) {
super(context, textViewResourceId, objects);
this.movies = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// assign view we are converting to a local var
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
/*
* Recall that the variable position is sent in as an argument to this
* method. The variable simply refers to the position of the current
* object in the list. (The ArrayAdapter iterates through the list we
* sent it)
*
* Therefore, i refers to the current Item object.
*/
Movies i = movies.get(position);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView id = (TextView) v.findViewById(R.id.movieId);
TextView title = (TextView) v.findViewById(R.id.movieTitle);
TextView vote_avg = (TextView) v.findViewById(R.id.movieAvg);
TextView backdrop_path = (TextView) v
.findViewById(R.id.movieBackdrop);
TextView release_date = (TextView) v
.findViewById(R.id.movieRelease);
TextView original_title = (TextView) v
.findViewById(R.id.movieTitle2);
TextView vote_count = (TextView) v.findViewById(R.id.movieCount);
TextView adult = (TextView) v.findViewById(R.id.movieAdult);
TextView poster = (TextView) v.findViewById(R.id.moviePoster);
TextView popularity = (TextView) v
.findViewById(R.id.moviePopularity);
// check to see if each individual textview is null.
// if not, assign some text!
id.setText(i.getId());
title.setText(i.getTitle());
vote_avg.setText(i.getVote_average());
backdrop_path.setText(i.getBackdrop_path());
release_date.setText(i.getRelease_date());
original_title.setText(i.getOriginal_title());
vote_count.setText(i.getVote_count());
adult.setText(i.isAdult());
poster.setText(i.getPoster_path());
popularity.setText(i.getPopularity());
}
// the view must be returned to our activity
return v;
}
}
如果我删除自定义数组适配器而不是使用带有测试字符串的字符串适配器,那么listview可以工作,但关键是我需要在listview中查看电影列表,而不是字符串。我的电影类覆盖了toString(),我认为这是答案,但没有变化。这是我的电影课
package com.example.jdmb;
import android.os.Parcel;
import android.os.Parcelable;
public class Movies implements Parcelable {
private int id;
private String title;
private int vote_average;
private String backdrop_path;
private String release_date;
private String original_title;
private int vote_count;
private String adult;
private String poster_path;
private int popularity;
public Movies(int id, String title, int vote_average, String backdrop_path,
String release_date, String original_title, int vote_count,
String adult, String poster_path, int popularity) {
this.id = id;
this.title = title;
this.vote_average = vote_average;
this.backdrop_path = backdrop_path;
this.release_date = release_date;
this.original_title = original_title;
this.vote_count = vote_count;
this.adult = adult;
this.poster_path = poster_path;
this.popularity = popularity;
}
private Movies(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
this.vote_average = in.readInt();
this.backdrop_path = in.readString();
this.release_date = in.readString();
this.original_title = in.readString();
this.vote_count = in.readInt();
this.adult = in.readString();
this.poster_path = in.readString();
this.popularity = in.readInt();
;
}
@Override
public String toString() {
return "Movies [id=" + id + ", title=" + title + ", vote_average="
+ vote_average + ", backdrop_path=" + backdrop_path
+ ", release_date=" + release_date + ", original_title="
+ original_title + ", vote_count=" + vote_count + ", adult="
+ adult + ", poster_path=" + poster_path + ", popularity="
+ popularity + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getVote_average() {
return vote_average;
}
public void setVote_average(int vote_average) {
this.vote_average = vote_average;
}
public String getBackdrop_path() {
return backdrop_path;
}
public void setBackdrop_path(String backdrop_path) {
this.backdrop_path = backdrop_path;
}
public String getRelease_date() {
return release_date;
}
public void setRelease_date(String release_date) {
this.release_date = release_date;
}
public String getOriginal_title() {
return original_title;
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
}
public int getVote_count() {
return vote_count;
}
public void setVote_count(int vote_count) {
this.vote_count = vote_count;
}
public String isAdult() {
return adult;
}
public void setAdult(String adult) {
this.adult = adult;
}
public String getPoster_path() {
return poster_path;
}
public void setPoster_path(String poster_path) {
this.poster_path = poster_path;
}
public int getPopularity() {
return popularity;
}
public void setPopularity(int popularity) {
this.popularity = popularity;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.title);
dest.writeInt(this.vote_average);
dest.writeString(this.backdrop_path);
dest.writeString(this.release_date);
dest.writeString(this.original_title);
dest.writeInt(this.vote_count);
dest.writeString(this.adult);
dest.writeString(this.poster_path);
dest.writeInt(this.popularity);
}
public static final Parcelable.Creator<Movies> CREATOR = new Parcelable.Creator<Movies>() {
public Movies createFromParcel(Parcel in) {
return new Movies(in);
}
public Movies[] newArray(int size) {
return new Movies[size];
}
};
}
错误:
01-02 14:37:52.283: D/AndroidRuntime(2273): Shutting down VM
01-02 14:37:52.283: W/dalvikvm(2273): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-02 14:37:52.293: E/AndroidRuntime(2273): FATAL EXCEPTION: main
01-02 14:37:52.293: E/AndroidRuntime(2273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jdmb/com.example.jdmb.ViewMovies}: java.lang.NullPointerException
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.os.Looper.loop(Looper.java:123)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-02 14:37:52.293: E/AndroidRuntime(2273): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 14:37:52.293: E/AndroidRuntime(2273): at java.lang.reflect.Method.invoke(Method.java:507)
01-02 14:37:52.293: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-02 14:37:52.293: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-02 14:37:52.293: E/AndroidRuntime(2273): at dalvik.system.NativeStart.main(Native Method)
01-02 14:37:52.293: E/AndroidRuntime(2273): Caused by: java.lang.NullPointerException
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.widget.ListView.setAdapter(ListView.java:454)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ListActivity.setListAdapter(ListActivity.java:265)
01-02 14:37:52.293: E/AndroidRuntime(2273): at com.example.jdmb.ViewMovies.onCreate(ViewMovies.java:20)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-02 14:37:52.293: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-02 14:37:52.293: E/AndroidRuntime(2273): ... 11 more
更新:所以事实证明我是在错误的位置启动意图,一旦我将其移出,意图就开始正确地传递数据。我现在可以看到listview它的格式非常糟糕,会花时间搞清楚如何让它看起来很漂亮。
答案 0 :(得分:1)
ArrayList<Movies> m = a.getParcelableArrayList("movielist");
Log.d("m is null" + (m == null ? "yes" : "no");
我认为您的数组列表未正确检索,因为ArrayAdapter的getCount是导致NULL的语句 - 指针异常。 您的类电影不包含默认(非参数化,平凡)构造函数,这也可能在使用列表时导致问题。 “ViewMovies.this”不正确,你应该使用“this”。