java - 如何更改弹出窗口 TextView 中的文本?
问题描述
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_attendance);
Intent intent = getIntent();
final ArrayList<String> names = intent.getStringArrayListExtra("names");
final ListView name_list = findViewById(R.id.name_list);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, names);
name_list.setAdapter(adapter);
name_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("Name",names.get(i));
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupview = inflater.inflate(R.layout.popup_window, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupview, width, height, true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0,0);
}
});
}
在上面的代码中,一切都很好,除了我想将弹出窗口文本设置为列表视图中的单击元素。
我使用Log.i()
,并且正在打印单击的元素,但是当我尝试将文本设置为此单击的元素时,例如:
TextView sub_name = findViewById(R.id.subject_name);
sub_name.setText(names.get(i));
我收到以下错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”
弹出窗口的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="161dp"
android:layout_marginTop="587dp"
android:layout_marginEnd="162dp"
android:layout_marginBottom="96dp"
android:onClick="quit"
android:text="Close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginTop="258dp"
android:layout_marginEnd="176dp"
android:layout_marginBottom="455dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
在你给 View 充气之后放上它
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupview = inflater.inflate(R.layout.popup_window, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupview, width, height, true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0,0);
TextView sub_name = popupview.findViewById(R.id.subject_name);
sub_name.setText(names.get(i));
推荐阅读
- python - 'float' 对象没有属性 'tanh'
- python-3.x - Web Scraping: My first project and no idea where to start
- angular - 修改本机元素时未更新与 ngModel 绑定的变量
- google-cloud-platform - 为什么 kompose up 产生“连接被拒绝”?
- php - Cpanel 在 paypal api 中找不到我的 bootsrap.php 文件(内置于 netbeans)
- c# - TargetFrameworkVersion 与 Xamarin
- php - 如何在 Laravel 中使用两个表创建自定义注册?
- python - 在 Python 中计算数组的“导数”
- angularjs - Angularjs - 插入表单内的下拉列表:如何在提交表单之前将项目添加到下拉列表中?
- php - 使用 PHP 和 AJAX 登录:不工作。接收失败的警报不正确的凭据 - 但是没有收到任何错误