android---TextView赋值问题

2019-03-25 13:38|来源: 网路

private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
text = (TextView)this.findViewById(R.id.jiegou);
setContentView(R.layout.lx);
Intent intent = this.getIntent();
int id=intent.getIntExtra("id", 0);
text.setText(id);
为什么就报错了呢?

问题补充:
苹果超人 写道
text = (TextView)this.findViewById(R.id.jiegou);
setContentView(R.layout.lx); 
这两句换一下位置:
setContentView(R.layout.lx); 
text = (TextView)this.findViewById(R.id.jiegou);

还是不行。

问题补充:
苹果超人 写道
引用
还是不行。

什么错误。看一下log。

07-27 09:22:36.580: ERROR/AndroidRuntime(279): FATAL EXCEPTION: main
07-27 09:22:36.580: ERROR/AndroidRuntime(279): java.lang.RuntimeException: Unable to start activity ComponentInfo{cms.lianxi.activity/cms.lianxi.activity.OtherActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.os.Looper.loop(Looper.java:123)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at java.lang.reflect.Method.invoke(Method.java:521)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at dalvik.system.NativeStart.main(Native Method)
07-27 09:22:36.580: ERROR/AndroidRuntime(279): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.content.res.Resources.getText(Resources.java:201)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.widget.TextView.setText(TextView.java:2817)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at cms.lianxi.activity.OtherActivity.onCreate(OtherActivity.java:21)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-27 09:22:36.580: ERROR/AndroidRuntime(279):     ... 11 more

问题补充:
苹果超人 写道
String resource ID #0x1 没有?clean一下你的项目。再运行

不行

问题补充:
苹果超人 写道
引用
不行

不可能 你把所有代码贴上去,我看看。

package cms.lianxi.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class OtherActivity extends Activity {
// private static final String TAG = "OtherActivity";
   private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lx);
text = (TextView)this.findViewById(R.id.jiegou);
Intent intent = this.getIntent();
int id=intent.getIntExtra("id", 0);
text.setText(id);



}


}















package cms.lianxi.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class LXActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button =(Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// 第一种写法
//表示我要干一件事,里面的参数表示打开LXActivity应用里的OtherActivity组件
Intent intent = new Intent(LXActivity.this,OtherActivity.class);
//通过intent给OtherActivity组件传一个id的参数
intent.putExtra("id", 1);
//通过startActivity把intent传给android操作系统,再由操作系统传给OtherActivity组件
LXActivity.this.startActivity(intent);
// 第二种写法
// Intent intent =new Intent();
// intent.setClass(LXActivity.this,OtherActivity.class);
// 第三种写法,三种写法都是用于打开LXActivity应用里的OtherActivity组件
// Intent intent = new Intent();
// intent.setComponent(new ComponentName(LXActivity.this,OtherActivity.class));
}
});
    }
}

问题补充:
苹果超人 写道
看一下你R文件中的#0x1这个ID 对应的string值有吗?



这个是我建的lx.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"   
    android:id="@+id/jiegou"
    />
</LinearLayout>







这个是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
   
    <Button
      android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button"
    android:id="@+id/button"
    />
</LinearLayout>


还有。。如果我把text.setText(id); 这个改成text.setText(“11”);这样就行

问题补充:
ranger1111 写道
你这里的settext(id)中的id,应该是对应的strings.xml中元素,生成的R.java中对应的资源id。你用intent传递的id值,应该是错误的。你如果想通过intent传递的id值,确定显示哪个字符串,就得直接传递R.java中的id值,或者就是判断你自己的id值,转换成相应的字符串资源id。

搞定。。大哥。。谢谢了

相关问答

更多
  • 请尝试这段代码..
    如果您放入TextView的文本较短,则不会自动扩展到四行。 如果您希望TextView 始终具有四行,无论文本的长度如何,请设置android:lines属性:
  • 因为所有这一切都发生在UI线程上。 当你用尽睡眠的UI线程时,框架没有机会运行并实际重绘整个事物。 Because all of this is happening on the UI thread. There's no chance for the framework to run and actually redraw the whole thing as you're using up the UI thread with sleeps.
  • 在textView中,使用以下属性android:layout_centerHorizontal="true"而不是android:layout_centerVertical="true"并添加android:alignParentBottom=true" 。还可以查看相关布局选项以了解如何使用它。 on the textView use the following attributes android:layout_centerHorizontal="true" instead of android:la ...
  • 您可以指定必须在ViewGroup添加TextView的索引... ViewGroup.addView(TextView, 0); 每次创建TextView并添加到mLayout ,请添加index 0,该index将添加到之前的TextView之上 mLayout.addView(createNewTextView("TextView"),0); You can specify the index where TextView has to be added in ViewGroup... ViewG ...
  • 不。如果您希望以不同方式格式化两位文本,请考虑在一个TextView中使用跨度 。 No. If you're looking to format the two bits of text differently, consider using spans within one TextView.
  • 我认为lblClickable为null,这行可能是错误的: TextView lblClickable = (TextView)findViewById(R.id.text); 更正: TextView lblClickable = (TextView)dialog.findViewById(R.id.text); I think lblClickable is null, this line is probably wrong: TextView lblClickable = (TextView ...
  • 这并不是Android真正支持的,而是设计的。 我们的想法是,您可以使用UI的全屏,当您需要选择它时,菜单会占用部分空间。 菜单启动时,您不必担心文本视图和其他内容。 这比iPhone好,你永远不得不放弃菜单项的屏幕空间。 如果某些事情非常重要,请将其从屏幕底部移开。 如果你真的喜欢移动东西,你可以拦截Activity.onPrepareOptionsMenu并在你的UI中移动东西,虽然这个功能并不是真正用于那个目的。 然后匹配调用onOptionsMenuClosed 。 That's not reall ...
  • 您的样式可以继承另一种样式,例如Android资源中的样式。 将父级更改为@android:style/Widget.Holo.Button for Holo style。 Your style can inherit another style, e.g. a style from the A ...
  • 我的猜测是你正在改变父视图的Background ,但是真的想要反转整个应用程序的Theme 。 如果我的假设是正确的,那么你想要做的就是将这一行添加到AndroidManifest.xml application标签中 - android:theme="@android:style/Theme.Light" 。 例如: