简体   繁体   English

通过Java代码从strings.xml将文本设置为textView时出错

[英]Error setting text to a textView from strings.xml via java code

I just want to set a text in a textView from strings.xml using java code but I just can't, eclipse gives me this error: "textView1 cannot be resolved" and "textView2 cannot be resolved". 我只想使用Java代码在strings.xml中的textView中设置文本,但是我做不到,eclipse给了我这个错误:“无法解析textView1”和“无法解析textView2”。

Here is mi activity: 这是mi活动:

package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class Descripcion extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.descripcion);

    Intent i = getIntent();

    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    switch (position){
    case 0:

    ImageView imageView0 = (ImageView) findViewById(R.id.full_image_view);
    imageView0.setImageResource(imageAdapter.mThumbIds[position]);

    textView1.setText(this,getString(R.string.case0));
    textView2.setText(this,getString(R.string.case0b));

    break;

    case 1:

    ImageView imageView1 = (ImageView) findViewById(R.id.full_image_view);
    imageView1.setImageResource(imageAdapter.mThumbIds[position]);

    textView1.setText(this,getString(R.string.case1));
    textView2.setText(this,getString(R.string.case0b));

    break;  
    }
}
}

I've beeing looking for a solution for about 3 hours but I can't fid it. 我一直在寻找解决方案大约3个小时,但我无法解决。

我看不到您在哪里声明TextView,例如:

TextView textView1 = (TextView)findViewById(R.id.textView1);

1) You didn't define TextView 1)您没有定义TextView

2) You didn't do findViewById to get corresponding TextView 2)您没有执行findViewById来获取相应的TextView

How to do that? 怎么做?

Your code itself has clues on how to do that, example: 您的代码本身具有有关如何执行此操作的线索,例如:

ImageView imageView1 = (ImageView) findViewById(R.id.full_image_view);

Here you defined ImageView and pointed it to R.id.full_image_view defined in layout.xml 在这里,您定义了ImageView并将其指向在R.id.full_image_view定义的layout.xml

like this you need to get TextView , example: 这样,您需要获取TextView ,例如:

TextView textView1 = (TextView)findViewById(R.id.textView1);

You forgot to define the variables textView1 and textView2 . 您忘记定义变量textView1textView2 Besides that you need to obtain a reference to them using findViewById . 除此之外,您还需要使用findViewById获得对它们的引用。 Weird part is that you did this for imageView1 and forgot to do it for the TextViews . 奇怪的是,您对imageView1做了此操作,却忘记了对TextViews进行了此操作。

Add the following piece of code below etContentView(R.layout.descripcion) : etContentView(R.layout.descripcion)下面添加以下代码:

TextView textView1 = (TextView) findViewById(<<-- the id of textView1 -->>); TextView textView1 = (TextView) findViewById(<<-- the id of textView2 -->>);

That's because textView1 and textView2 aren't defined.... 这是因为未定义textView1和textView2。

You see what you do with the ImageView? 您看到使用ImageView做什么了吗?

ImageView imageView0 = (ImageView) findViewById(R.id.full_image_view);

You have to apply the same concept to the text views. 您必须将相同的概念应用于文本视图。

TextView textView1 = (TextView) findViewById(R.id.id_of_text_view_1);
TextView textView2 = (TextView) findViewById(R.id.id_of_text_view_2);

Then you can perform operations on that text view, such as setting the text! 然后,您可以在该文本视图上执行操作,例如设置文本!

define your textviews as you did your imageview. 定义textview就像定义imageview一样。

you can use 您可以使用

TextView textView1 = (TextView)findViewById(R.id.textView1);
TextView textView2 = (TextView)findViewById(R.id.textView2);

where 哪里

R.id.textView1

is the id of your textview1 in your xml layout 是XML布局中textview1的ID

R.id.textVie2

is the id of your textview2 in your xml layout 是XML布局中textview2的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM