gpt4 book ai didi

java - SetText 从类到主 XML 文件

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:52 24 4
gpt4 key购买 nike

我正在尝试在 java 类中使用 .setText 来尝试更改 activity_main XML 文件上的 TextView 的值,到目前为止,我收到了 NullpointerExeption 错误,并且我已经了解到这是由于声明变量时出现错误而导致的。我怎样才能做到这一点?我需要先在 mainActivity.java 中声明它吗?

在我的activity_main.xml上,我有一个按钮 -> 它会打开一个自定义listView -> 如果您按 ListView 上的第2个项目 -> 它会打开一个自定义警报对话框 -> 自定义警报对话框包含2个按钮 -> 如果您按第二个按钮 -> 它必须设置位于activity_main.xml上的TextView的文本

感谢任何帮助!

MainActivity.java

final TextView KMLabel = (TextView)findViewById(R.id.KMlabel);

activity.main.xml

   <TextView
android:id="@+id/KMlabel"
android:layout_alignBottom="@+id/TVKm"
android:layout_toRightOf="@+id/TVKm"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#e6c009"
android:text="KM/H"
android:textStyle="italic"/>

自定义.java

public class custom extends BaseAdapter{
Context context;
String Item[];
String SubItem[];
int flags[];
LayoutInflater inflter;

public custom(Context applicationContext, String[] Item, String[] SubItem , int[] flags) {
this.context = context;
this.Item = Item;
this.SubItem = SubItem;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
return Item.length;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {


view = inflter.inflate(R.layout.activity_items, null);
//TextView Prueba = (TextView)view.findViewById(R.id.KMlabel);
TextView item = (TextView) view.findViewById(R.id.item);
TextView subitem = (TextView) view.findViewById(R.id.subitem);
ImageView image = (ImageView) view.findViewById(R.id.image);
item.setText(Item[i]);
subitem.setText(SubItem[i]);
image.setImageResource(flags[i]);
return view;
}

viewdialog.java

public class ViewDialog {
public void showDialog(Activity activity, String msg){

final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_dialog);

//I'm declaring it like this
final TextView KMLabel = (TextView)activity.findViewById(R.id.KMlabel);

Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

Button KmPerHr = (Button)dialog.findViewById(R.id.KmPerH);
KmPerHr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//and calling it this way:
KMLabel.setText("MLL/H");
}
});
dialog.show();
}
}

日志:

FATAL EXCEPTION: main
Process: com.example.dell.getspeed, PID: 3925
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.dell.getspeed.ViewDialog$2.onClick(ViewDialog.java:38)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10936)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

您必须移动代码

Button KmPerHr = (Button)dialog.findViewById(R.id.KmPerH);
KmPerHr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//and calling it this way:
KMLabel.setText("MLL/H");
}
});

final TextView KMLabel = (TextView)dialog.findViewById(R.id.KMlabel);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

位于dialog.show()代码下方,因为findViewById代码仅在弹出对话框后才起作用。

关于java - SetText 从类到主 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com