繁体   English   中英

为什么我在Android设备上看不到布局更改并得到未解决的参考错误?

[英]Why don't I see layout changes on Android device and get unresolved reference error?

我正在编写第二个Android应用程序(初学者),但遇到以下问题:

一种。 当我对XML(文本视图/图像视图)进行更改时,它会在android studio设计预览中更改。 但是不在我的手机或模拟器上。 我对C ++中的Hello感到困惑。

我最近尝试了生日贺卡应用程序,并且在添加新的文本视图或图像视图时遇到错误。

卡住的任何帮助将不胜感激谢谢。

我已经尝试过在这里看到的解决方案; 对MainActivity.kt进行更改。

//  sample_text.text = stringFromJNI() 
//  val sample_text : TextView = findViewById<TextView>(R.id.sample_text) 
TextViewsample_text.text = "Happy Birthday"
//got errors 

原始码

我的activity_main文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextView
android:text="Happy Birthday Jimmy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
app:srcCompat="@drawable/androidparty" 
android:id="@+id/imageView"/>

<TextView
android:text="from Osas"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:id="@+id/textView" 
android:layout_alignParentBottom="true"/> </RelativeLayout>

我的mainactivity.kt文件:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Example of a call to a native method
        sample_text.text = stringFromJNI()
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {

        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
        }
    }
}

编译器错误:

build failed    2 s 564 ms
Run build   2 s 405 ms
Load build  41 ms
Configure build 793 ms
Calculate task graph    46 ms
Run tasks   1 s 433 ms
null    
/Users/user/AndroidStudioProjects/HappyBirthday 
app/src/main/java   
com/example/android/happybirthday/MainActivity.kt   
Unresolved reference: sample_text   

在应用我在这里看到的解决方案并对mainactivity.kt进行更改之后,这里的一些屏幕截图。

主要活动编译器手机模拟器activity_main.xml中

当您想要显示或修改任何视图的值(例如在TextView中设置文本或在ImageView中更改图像)时,您需要以XML或以编程方式为这些视图分配ID。

android:layout_id="@+id/give_Some_ID" 

现在,您需要在Java文件中使用视图ID。

TextView tv =findViewById(R.id.give_Some_ID);

希望能帮助到你。

暂无
暂无

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

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