繁体   English   中英

为什么这个变量为空?

[英]Why this variable is null?

我正在研究墙纸应用程序。 当我单击图像时,活动正在更改,并且图像的id保存内存。 在第二个活动中,id为true,但findViewById(id)始终为null。 顺便说一句,当我将setWallpaper函数移动到MainActivity.kt时,代码可以正常工作。

MainActivity.kt

fun clickImage(v: View) {
       val intent = Intent(this, SecActivity::class.java)
       intent.putExtra("id", v.id)
       startActivity(intent)
}

SecActivity.kt

class SecActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sec)
        val button = findViewById<Button>(R.id.btn)
        button.setOnClickListener{
            setWallpaper()
        }
    }

    fun setWallpaper() {
        var id = intent.getIntExtra("id",0)
        var img  = findViewById<ImageView>(id)
        val bitmap: Bitmap = (img.getDrawable() as BitmapDrawable).getBitmap()
        val wallpaperManager: WallpaperManager = WallpaperManager.getInstance(this)
        wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_SYSTEM)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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" android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/a001"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:onClick="clickImage"
                android:scaleType="centerCrop"
                android:src="@drawable/a001"></ImageView>

            <ImageView
                android:id="@+id/a002"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:onClick="clickImage"
                android:scaleType="centerCrop"
                android:src="@drawable/a002"></ImageView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:orientation="horizontal">

            <ImageView
                android:id="@+id/a003"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:onClick="clickImage"
                android:scaleType="centerCrop"
                android:src="@drawable/a003"></ImageView>

            <ImageView
                android:id="@+id/a004"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:onClick="clickImage"
                android:scaleType="centerCrop"
                android:src="@drawable/a004"></ImageView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:orientation="horizontal">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:onClick="clickImage"
                android:scaleType="centerCrop"
                android:src="@drawable/a005"></ImageView>

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:onClick="clickImage"
                android:scaleType="centerCrop"
                android:src="@drawable/a006"></ImageView>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

activity_sec.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".SecActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent" android:scaleType="fitXY"
        android:id="@+id/wall"
        android:adjustViewBounds="true" android:src="@drawable/a004"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="@string/btn"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

图片: https : //i.stack.imgur.com/JEKCy.jpg

您的ImageView属于MainActivity 您找不到其他活动的视图(在本例中为SecActivity )。

如果您要存储第一个活动(和第一个布局)中使用的id ,并且尝试在第二个活动中使用不同的布局获取具有相同ID的视图,则该视图具有以下特征:

var img  = findViewById<ImageView>(id)

不会仅仅因为第二个活动布局没有该ID 找到该视图。

您应该获取选定的图像Uri并将其传递给第二个活动,并使用此图像Uri来显示图像并将其设置为墙纸。 基本上将Uri作为字符串传递给第二个活动,而不是我的图像视图。

尝试起飞findViewById。 您可以直接引用btn id

像这样的东西:

  ...
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_sec)
    btn.setOnClickListener{
        setWallpaper()
    }
  }
   ...

暂无
暂无

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

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