| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 835 人关注过本帖
标题:在控件TextView中添加自己的文字时遇到的问题
只看楼主 加入收藏
liangkenan
Rank: 1
等 级:新手上路
帖 子:79
专家分:7
注 册:2011-9-16
结帖率:100%
收藏
 问题点数:0 回复次数:3 
在控件TextView中添加自己的文字时遇到的问题
这个问题就是,我已经给myTextView建立了一个ID,但是在使用findViewById查找时遇到问题,将函数findViewById的返回值强行转化为TextView类,代码为TextView myTextView=(TextView)findViewById(R.id.myTextView);
这行代码的两个TextView都显示TextView不能被解析成一个变量,我的SRC源文件如下
package hi.hello;

import android.app.Activity;
import android.os.Bundle;

public class HiActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   TextView myTextView=(TextView)findViewById(R.id.myTextView);//从这里出现问题
   myTextView.setText("我的第一个TextView");
   }
}
Main.xml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/myTextView"/>

</LinearLayout>
R.JAVA如下
/* AUTO-GENERATED FILE. DO NOT MODIFY.
  *
  * This class was automatically generated by the
  * aapt tool from the resource data it found. It
  * should not be modified by hand.
  */

package hi.hello;

public final class R {
   public static final class attr {
   }
   public static final class drawable {
   public static final int ic_launcher=0x7f020000;
   }
   public static final class id {
   public static final int myTextView=0x7f050000;
   }
   public static final class layout {
   public static final int main=0x7f030000;
   }
   public static final class string {
   public static final int app_name=0x7f040001;
   public static final int hello=0x7f040000;
   }
}
不知问题出在哪里,我初学
搜索更多相关主题的帖子: class void activity created package 
2012-01-30 21:04
laughing2011
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-10-26
收藏
得分:0 
在出错代码前加final:
final TextView myTextView=(TextView)findViewById(R.id.myTextView);
2012-02-27 21:03
琴之弦
Rank: 2
等 级:论坛游民
威 望:2
帖 子:651
专家分:88
注 册:2007-9-6
收藏
得分:0 
你没有导入   TextView 这个包   
应该加入一句   import android.widget.TextView;

琴舞梦幻独为君,弦断醉醒泪无数。jerry0908mo.blog.
2012-03-01 02:55
wayxwn
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-10-25
收藏
得分:0 
ddddddddddddddd
2012-10-25 13:39
快速回复:在控件TextView中添加自己的文字时遇到的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028295 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved