8

I have a small Java Android Studio project, and I used the Kotlin converter in Android Studio 3.0 Canary 3 to convert all four source files to Kotlin. I then made some small modifications to get it to compile. I have only one activity with the following code:

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
        }
    }

}

When I run this I get the following error:

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)

I'm not sure what the problem here is. Clearly gridview is null, but I don't know why. When I comment out the gridview.adapter and gridview.onItemClickListener lines, at least the program doesn't crash. I had the same error message before I used kotlin-android-extensions and defined the variable in question as val gridView = findViewById(R.id.gridview) as GridView.

The corresponding XML file has the following content:

<?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>

My original Java code which worked just fine for this class was:

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
            }
        });
    }

}
Eugene
  • 3,417
  • 5
  • 25
  • 49
  • 1
    There's an adapter somewhere. In `getView()`, `convertView` parameter should be nullable, i. e. `convertView: View?`. Otherwise Kotlin asserts it is not null. – Miha_x64 Jun 05 '17 at 16:01
  • Use great tool Anko for simpler life)) - https://github.com/Kotlin/anko – Silvestr Jun 05 '17 at 16:05
  • @Eugene It's just advise), about your problem - I think you are not import kotlinx plugin(you need to import it in main gradle file and apply in app gradle file, see my answer) correctly or problem was in beta studio and android plugin – Silvestr Jun 05 '17 at 16:40
  • 1
    The problem is not with this activity. You need to share your adapter, since I suppose it's the one that owns the convert view – Kingsley Adio Jun 05 '17 at 21:30
  • 3
    I'm facing the same issue with a recyclerview, it seems super random, Kotlin can find other views with no problem. Did you find a solution? clean/rebuild doesnt help – frankelot Dec 05 '17 at 04:09
  • Same here. I got issue for recyclerview. I use kotlin extension. If I call in onCreate, it crash but if i call in onViewCreated, it is okay. How shall I solve? – Khant Thu Linn Apr 30 '19 at 15:28
  • Can you also post your adapter class – Yash Joshi May 28 '21 at 10:09

3 Answers3

0

Kotlin provide null check using safe call operator. You can use it before accessing the view. Safe Calls. For example,

gridview?.adapter = storyboardAdapter
zzas11
  • 1,115
  • 9
  • 16
0

The exception seems to be with the null value you are supplying to your storyboardAdapter. As the error line,

Parameter specified as non-null is null

The items is somehow null. Check your code. And also try to call setContentView before the array initialization, as I suspect there might have something to do with the applicationContext

Aziz
  • 1,976
  • 20
  • 23
-1

I think problem is in kotlin-android-extensions, your code looks good, just try to clean project and change grid view id. Also you can read great article about configuring kotlin-android-extensions, this is realy cool tool, you will forget about findViewById. Also please try it in stable version of studio and android plugin and make sure that you import it in main gradle file and apply in app gradle file.

Silvestr
  • 704
  • 4
  • 13