java - 如何修复我的 android 应用程序中的布局视图?
问题描述
我正在尝试使用我的 api 创建按钮。我使用 调用 api Retrofit
,然后创建按钮。但是有代码错误!API 响应中返回的数据是正确的,但创建视图是一个问题。
它说我需要removeView()
。我这样做了,但现在没有数据打印。
我的代码:
package com.example.englishapp;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class LessonsActivity extends AppCompatActivity {
private TextView textViewResult;
String URL = "http://kurchanovenglish.ru/data/";
private String[] name = new String[18];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lessons);
final LinearLayout layout = new LinearLayout(this);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
params.setMargins(250, 10, 250, 50);
layout.setOrientation(LinearLayout.VERTICAL);
int i;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<List<Lesson>> call = jsonPlaceHolderApi.getPosts();
final LinearLayout row = new LinearLayout(this);
final Button button = new Button(this);
call.enqueue(new Callback<List<Lesson>>() {
@Override
public void onResponse(Call<List<Lesson>> call, Response<List<Lesson>> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
List<Lesson> lessons = response.body();
int b = 0;
String[] name = new String[18];
for (Lesson lesson : lessons) {
button.setText(lesson.getName());
button.setId(lesson.getNumber());
button.setLayoutParams(params);
button.setTextSize(20);
/*button.setBackground(this.getResources().getDrawable(R.drawable.buttons));*/
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
Intent intent = new Intent(LessonsActivity.this, WelcomeActivity.class);
startActivity(intent);
}
});
row.removeView(layout);
row.addView(button);
}
layout.addView(row);
}
@Override
public void onFailure(Call<List<Lesson>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
}
错误:
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.englishapp, PID: 19385
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4915)
at android.view.ViewGroup.addView(ViewGroup.java:4746)
at android.view.ViewGroup.addView(ViewGroup.java:4686)
at android.view.ViewGroup.addView(ViewGroup.java:4659)
at com.example.englishapp.LessonsActivity$1.onResponse(LessonsActivity.java:92)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
请帮帮我。我不知道该怎么办。
解决方案
这是你做错了什么;您正在使用相同的Button
实例并将其再次添加到row
( LinearLayout
)。
如果我理解正确,这就是您想要的视图层次结构。
LinearLayout
--LinearLayout
--Button
--Button
.
--LinearLayout
--Button
--Button
.
.
您需要了解的第一件事是,您不能View
在视图层次结构中的多个位置重用 a。其次,aView
不能是多个父母的孩子,即同一个Button
不能出现在两行中。让我们想想这种限制的原因;比如说,您被允许将相同的内容添加Button
到另一个父母,然后您将如何识别其中的点击,您如何知道您点击了哪个。此外,假设您有一个文本要显示在一个文本上,而另一个文本显示另一个文本,Button
如果/当您共享参考时,您将如何更改每个文本。你看到问题了吗?
但是,由于您正在实例化并添加Button
Java 代码。您需要首先创建一个新实例Button
以添加到 parent row
。layout
因此,如果要添加多行,还需要将新的行实例添加到父级。
然后您的代码变为:
for (Lesson lesson : lessons) {
Button button = new Button(LessonsActivity.this);
button.setText(lesson.getName());
button.setId(lesson.getNumber());
button.setLayoutParams(params);
button.setTextSize(20);
/*button.setBackground(this.getResources().getDrawable(R.drawable.buttons));*/
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
Intent intent = new Intent(LessonsActivity.this, WelcomeActivity.class);
startActivity(intent);
}
});
row.addView(button);
}
layout.addView(row);
}
您将需要layout.addView(row);
相应地进行调整,但遵循相同的原则。
推荐阅读
- node.js - 在nodejs rest api中得到404
- python - CS50 DNA:使用 next() 选择 STR
- c - 溢出的变量的值是多少?
- reactjs - Firebase React - 路由在 firebase 模拟器上不起作用
- ruby-on-rails - 资产重定向到 https 而不是 http
- angular - 无法在 ngOnInit() 中为变量赋值
- javascript - 减小窗口宽度时,Bootstrap 导航栏切换按钮不起作用
- spring - Sprint RESTful 服务器能否支持 Accept=application/xml
- javascript - 有没有办法在 ES6 中读取本地 JSON 数组?
- excel - excel用户表单中列表框的动态行源