gpt4 book ai didi

android - Kotlin View 变量在 Activity onCreate 方法中意外为空

转载 作者:IT老高 更新时间:2023-10-28 13:46:14 26 4
gpt4 key购买 nike

我有一个小型 Java Android Studio 项目,我使用 Android Studio 3.0 Canary 3 中的 Kotlin 转换器将所有四个源文件转换为 Kotlin。然后我做了一些小的修改来编译它。我只有一个带有以下代码的 Activity :

package com.ebelinski.soundboard

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.AdapterView
import kotlinx.android.synthetic.main.activity_storyboard.gridview;

class StoryboardActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val itemManager = ItemManager(applicationContext)
val items = itemManager.getItems().toTypedArray<Item>()

setContentView(R.layout.activity_storyboard)
val storyboardAdapter = StoryboardAdapter(this, items)
gridview.adapter = storyboardAdapter

gridview.onItemClickListener = AdapterView.OnItemClickListener { parent, v, position, id ->
// todo
}
}

}

当我运行它时,我收到以下错误:

06-05 10:25:42.758 5668-5668/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ebelinski.soundboard, PID: 5668
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
at com.ebelinski.soundboard.StoryboardAdapter.getView(StoryboardAdapter.kt)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.GridView.onMeasure(GridView.java:1065)
at android.view.View.measure(View.java:19857)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我不确定这里的问题是什么。显然 gridview 为空,但我不知道为什么。当我注释掉 gridview.adaptergridview.onItemClickListener 行时,至少程序不会崩溃。在使用 kotlin-android-extensions 并将有问题的变量定义为 val gridView = findViewById(R.id.gridview) as GridView 之前,我收到了相同的错误消息。

对应的XML文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_storyboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ebelinski.soundboard.StoryboardActivity">

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="24dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidthUniform"
/>
</RelativeLayout>

我的原始 Java 代码对这个类工作得很好:

package com.ebelinski.soundboard;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class StoryboardActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ItemManager itemManager = new ItemManager(getApplicationContext());
final Item[] items = itemManager.getItems().toArray(new Item[itemManager.getItems().size()]);

setContentView(R.layout.activity_storyboard);
GridView gridView = (GridView)findViewById(R.id.gridview);
StoryboardAdapter storyboardAdapter = new StoryboardAdapter(this, items);
gridView.setAdapter(storyboardAdapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// todo
}
});
}

}

最佳答案

Kotlin 使用安全调用运算符提供空值检查。您可以在访问 View 之前使用它。 Safe Calls .例如,

gridview?.adapter = storyboardAdapter

关于android - Kotlin View 变量在 Activity onCreate 方法中意外为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372432/

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