Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用从Gson获取的数据在textView中设置文本_Android_Android Intent_Textview_Gson - Fatal编程技术网

Android 如何使用从Gson获取的数据在textView中设置文本

Android 如何使用从Gson获取的数据在textView中设置文本,android,android-intent,textview,gson,Android,Android Intent,Textview,Gson,我尝试用普通的setText方法设置它,但是值为null,textView设置为空,上面没有单词,我从API获取了json,下面是我的代码- public class Child extends AppCompatActivity { TextView mTitle; TextView mDescription; TextView mReleased; ImageView mCover; TextView mRating; @Override protected void onCreate(B

我尝试用普通的setText方法设置它,但是值为null,textView设置为空,上面没有单词,我从API获取了json,下面是我的代码-

public class Child extends AppCompatActivity {

TextView mTitle;
TextView mDescription;
TextView mReleased;
ImageView mCover;
TextView mRating;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child);

    GsonBuilder gsonBuilder = new GsonBuilder();
    String Obj = getIntent().getStringExtra("movie");
    Movie data = gsonBuilder.create().fromJson(Obj, Movie.class);

    mTitle = (TextView) findViewById(R.id.title);
    mTitle.setText(data.getTitle());
    mDescription = (TextView) findViewById(R.id.description);
    mDescription.setText(data.getDescription());
    mReleased = (TextView) findViewById(R.id.released);
    mReleased.setText(data.getReleased());
    mCover = (ImageView) findViewById(R.id.cover);
    mRating = (TextView) findViewById(R.id.rating);
    mRating.setText(data.getRating());
}}
这是电影课-

public class Movie {
public String TMDB_IMAGE_PATH = "http://image.tmdb.org/t/p/w500";

private String title;

@SerializedName("poster_path")
private String poster;

@SerializedName("overview")
private String description;

@SerializedName("backdrop_path")
private String released;

private String rating;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getPoster() {
    return TMDB_IMAGE_PATH + poster;
}

public void setPoster(String poster) {
    this.poster = poster;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getReleased() {
    return released;
}

public void setReleased(String released) {
    this.released = released;
}

public String getRating() {
    return rating;
}

public void setRating(String rating) {
    this.rating = rating;
}

public static class result {
    private List<Movie> results;

    public List<Movie> getResults() {
        return results;
    }
}}
你能试试这些吗

Cast to Movie Movie data=Movie gsonBuilder.create.fromJsonObj,Movie.class; 确保收到的数据符合预期->打印字符串Obj。Logcat.dMOVIE应用程序,Obj;&给我看看结果
忘记将对象转换为JSON并返回。相反,通过Movie.class实现Parcelable接口,并将其作为活动之间的对象发送,例如。这是更快、更干净的,而且应该这样做


您可以找到用于Parcelable的AndroidStudio插件。按照教程进行操作。

调试您的代码,并确保您没有从数据提供程序获取null。并检查是否配置了错误的数据字段名称。一些注释不能回答您的问题,但可以改进代码:1您可以使用savedInstanceState而不是getIntent。2您可以使用它来代替创建上下文变量。至于您的问题,您需要学习如何使用Android Studio调试器。在MainActivity的代码中设置断点,以检查电影数据是否符合预期。然后在Child中设置断点以检查数据是否正确接收。但是如果我更改为savedInstanceState,我将无法使用GetStringExtra我已尝试调试,位置或数据已正确接收在我运行之前,android studio无法解析我的getIntent方法。请向我展示您的导入,并确保上面的代码与您的代码相同。您使用的是Android Studio还是Eclipse?
 Movie data = movies.get(position);
                    final Context context = view.getContext();
                    Intent intent = new Intent(context, Child.class);
                    GsonBuilder gsonBuilder = new GsonBuilder();
                    intent.putExtra("movie", gsonBuilder.create().toJson(data, Movie.class));

                    context.startActivity(intent);