Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么这个变量是空的?_Android_Kotlin - Fatal编程技术网

Android 为什么这个变量是空的?

Android 为什么这个变量是空的?,android,kotlin,Android,Kotlin,我正在开发墙纸应用程序。当我单击图像时,Activy正在更改,图像的id将节省内存。在第二个活动中,id为true,但FindViewByDid始终为null。顺便说一下,当我将setWallpaper函数移到MainActivity.kt时,代码工作正常 MainActivity.kt fun clickImage(v: View) { val intent = Intent(this, SecActivity::class.java) intent.putExtr

我正在开发墙纸应用程序。当我单击图像时,Activy正在更改,图像的id将节省内存。在第二个活动中,id为true,但FindViewByDid始终为null。顺便说一下,当我将setWallpaper函数移到MainActivity.kt时,代码工作正常

MainActivity.kt

fun clickImage(v: View) {
       val intent = Intent(this, SecActivity::class.java)
       intent.putExtra("id", v.id)
       startActivity(intent)
}
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)
    }
}
SecActivity.kt

fun clickImage(v: View) {
       val intent = Intent(this, SecActivity::class.java)
       intent.putExtra("id", v.id)
       startActivity(intent)
}
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>
活动_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>
图像:

您的图像视图属于MainActivity。在本例中,您无法从其他活动的SecActivity中找到视图。

如果您正在存储第一个活动和第一个布局中使用的id,并且您正试图在第二个活动中使用不同的布局获取具有相同id的视图,请执行以下操作:

var img  = findViewById<ImageView>(id)

您将找不到该视图,因为第二个活动布局没有该id。

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

尝试去掉findViewById。您可以直接引用btn id

大概是这样的:

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

您能显示您的xml吗?这样我们就可以检查您是否附加了任何图像。因为第二个活动布局没有该id。您必须确保在第二个活动的布局中有一个id为btn的按钮。@Ashish添加了…那么,我怎么做呢?既然您只想显示位图,传递您可以用来获取所选图像的内容url、名称、资源名称、db上的键。。。这取决于您的项目,而不是布局id。传递图像资源id,而不是图像视图id。val intent=Intentthis,SecActivity::class.java intent.putExtraid,R.drawable.EXAMPLE_image startactivityintent,然后在SecActivity中创建如下位图。val bitmap=BitmapFactory.decodeResourceresources,id