gpt4 book ai didi

Java 到 Kotlin 以不同的方法访问类特定的变量

转载 作者:行者123 更新时间:2023-12-02 13:22:40 28 4
gpt4 key购买 nike

我来自 Objective-C 背景。我必须以编程方式创建对象,因此尝试在 Java 和 Kotlin 中探索它。

在 Objective-C 中,我们曾经在 .h 文件中声明实例变量并在 .m 文件中的任何位置访问它们,是否有类似的接口(interface),我们在某处声明变量和对象类型,然后在我们的 kotlin 类中访问它们?

我正在尝试声明一个变量,我可以在类内的不同方法中访问该变量(例如 MainActivity)。在 Java 中,它工作得很好,但是在 Kotlin 中,我想我在不初始化它的情况下声明它时遇到了麻烦。需要帮忙。

//Here is the Java Code that works fine.
public class MainActivity extends AppCompatActivity {

**LinearLayout box;**

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

**box = new LinearLayout(this);**

}

private void SampleMethod () {
**box.setBackgroundColor(Color.RED);**
}
}

如果我尝试在 Kotlin 中实现这一点,它会使用堆栈跟踪中提供的 ERROR 停止应用程序。
//Here is Kotlin code that is stopping the app. 
class MainActivity : AppCompatActivity() {

**val sample = TextView(this)** // I need to declare variable here.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
**sample.setText("Hello!")**
MainContainer.addView(sample)
}

fun Test() {
**sample.setBackgroundColor(Color.RED)**
}
}

这是堆栈跟踪
07-04 13:16:14.690 12663-12663/? I/zygote: Not late-enabling -Xcheck:jni (already on)
07-04 13:16:15.230 12663-12663/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
07-04 13:16:16.639 12663-12663/com.example.sample.kotlinproject I/InstantRun: starting instant run server: is main process
07-04 13:16:16.938 12663-12663/com.example.sample.kotlinproject D/AndroidRuntime: Shutting down VM
07-04 13:16:16.940 12663-12663/com.example.sample.kotlinproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sample.kotlinproject, PID: 12663
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sample.kotlinproject/com.example.sample.kotlinproject.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.view.View.<init>(View.java:4563)
at android.view.View.<init>(View.java:4706)
at android.view.ViewGroup.<init>(ViewGroup.java:597)
at android.widget.LinearLayout.<init>(LinearLayout.java:234)
at android.widget.LinearLayout.<init>(LinearLayout.java:230)
at android.widget.LinearLayout.<init>(LinearLayout.java:226)
at android.widget.LinearLayout.<init>(LinearLayout.java:222)
at com.example.sample.kotlinproject.MainActivity.<init>(MainActivity.kt:18)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

最佳答案

class MainActivity : AppCompatActivity() {


val sample = TextView(this)

这是行不通的——你的 Android 书籍或教程应该说明原因。实例化 AppCompatActivity 是不够的使用构造函数获取 Activity 的完整实例.

一般来说,最好等到 onCreate在尝试以编程方式实例化 View 之前。
class MainActivity : AppCompatActivity() {

lateinit var myTextView: TextView

override fun onCreate(savedInstanceState: Bundle) {
//instantiate myTextView here if you must
}
}

关于Java 到 Kotlin 以不同的方法访问类特定的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51169392/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com