2012-09-25 41 views
0

我只是想使用java代碼在strings.xml中設置textView中的文本,但我只是不能,eclipse給了我這個錯誤:「textView1不能解決「和」textView2無法解析「。通過java代碼設置文本到strings.xml文本的錯誤

這裏是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; 
    } 
} 
} 

我beeing尋找約3小時的解決方案,但我不能FID它。

回答

1

1)您沒有定義TextView

2)你沒有做findViewById得到相應TextView

如何做到這一點?

您的代碼本身對如何做到這一點,例如線索:

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

在這裏,你定義ImageView,並指出,在layout.xml

這樣定義的,你需要得到TextViewR.id.full_image_view,例如:

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

我已經定義了我的TextVi但現在我有這個錯誤: 類型TextView中的方法setText(CharSequence,TextView.BufferType)不適用於參數(activity,String)\t activity.java \t/aplicacion/src/com/example/memepedia \t第31行\t Java問題 – Jacobo

+0

爲什麼這是第一個參數?不應該只是setText(getString(R.string.case1))? – kosa

+0

非常感謝!我只是做我的第一個應用程序的Android,在Objective-C中,你必須在其他文件中定義標籤和按鈕,我忘了在Java中它是不同的= D – Jacobo

3

我看不到你在哪裏宣佈你的TextView中的,如:

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

非常感謝!我只是做我的第一個應用程序的Android,在Objective-C中,你必須在其他文件中定義標籤和按鈕,我忘記了在Java中它是不同的= D – Jacobo

+0

無後顧之憂,我們都在那裏。我從PHP到Java,談論學習曲線:) – jnthnjns

0

你忘了定義變量textView1textView2。除此之外,你需要使用findViewById來獲得對他們的引用。奇怪的是,你爲imageView1做了這個,忘記了爲TextViews做。

添加下面的一段代碼如下etContentView(R.layout.descripcion)

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

+0

非常感謝!我只是做我的第一個應用程序的android,在objective-c中你必須在其他文件中定義標籤和按鈕,我忘了在java中它是不同的= D – Jacobo

0

這是因爲textView1和textView2沒有定義....

你看你用的ImageView做什麼呢?

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

你有相同的概念應用於文本的意見。

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

然後您可以對該文本視圖執行操作,例如設置文本!

+0

非常感謝!我只是做我的第一個應用程序的Android,在objective-c中你必須定義其他文件中的標籤和按鈕,我忘了在java中它是不同的= D – Jacobo

0

與您的imageview一樣定義文字瀏覽。

可以使用

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

其中

R.id.textView1 

是你textview1的ID在你的XML佈局

R.id.textVie2 

是你textview2的ID在你的XML佈局

+0

非常感謝!我只是做我的第一個應用程序的Android,在Objective-C中,你必須在其他文件中定義標籤和按鈕,我忘了在Java中它是不同的= D – Jacobo

相關問題