英文:
How to change the gravity of a TextView in another xml file from Main Activity?
问题
我有一个XML文件,其中有一个TextView,我想从主活动更改它的对齐方式。我在主活动中使用了以下代码:
// 我只复制了相关的代码
TextView tvValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvValue = findViewById(R.id.tv_value);
// the tv_value id is from the (word_item.xml) file
}
private void OnClickFunc(View view) {
tvValue.setGravity(Gravity.RIGHT);
}
问题是我得到了一个NullPointerException错误,指向一个null对象引用。我知道我应该以某种方式将xml文件与主活动连接起来。
word_item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word" />
</LinearLayout>
谢谢,非常感谢您的帮助。
英文:
I have an XML file that has a TextView, and I want to change its gravity from the main activity.
I used this code in the main activity:
// I just coppied the related codes
TextView tvValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvValue = findViewById(R.id.tv_value);
// the tv_value id is from the (word_item.xml) file
}
private void OnClickFunc(View view) {
tvValue.setGravity(Gravity.RIGHT);
}
and the problem is that I get a NullPointerException error on a null object reference.
I know that I should somehow connect the xml file with the main activity.
word_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word" />
</LinearLayout>
Thank you and I appreciate your help.
答案1
得分: 1
if you havent included the word_item.xml
in your activity_main.xml
then the error is obvious
so this is how you can include another file in any xml file in android
<include layout="@layout/word_item.xml" />
then things ll work as you expect
英文:
if you havent included the word_item.xml
in your activity_main.xml
then the error is obvious
so this is how you can include another file in any xml file in android
<include layout="@layout/word_item.xml" />
then things ll work as you expect
答案2
得分: 1
尝试首先按照以下方式填充另一个布局,然后初始化视图:
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View v = inflater.inflate(R.layout.word_item, null);
然后:
Textview tvValue = findViewById(R.id.tv_value);
private void OnClickFunc() {
tvValue.setGravity(Gravity.RIGHT);
英文:
Try to first inflate the other layout as follows the initialize the view
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View v = inflater.inflate(R.layout.word_item, null);
Then
Textview tvValue = findViewById(R.id.tv_value);
private void OnClickFunc() {
tvValue.setGravity(Gravity.RIGHT);
答案3
得分: 0
你在哪里初始化了textView变量?变量必须在Activity的OnCreate方法中的setContentView之后进行初始化。
英文:
Where did you initialize textView variable?? Variable must be initialize after setContentView in OnCreate method in Activity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论