Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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 Studio Kotlin调用任何活动_Android_Android Studio_Kotlin_Android Fragments_Fragment - Fatal编程技术网

如何从Android Studio Kotlin调用任何活动

如何从Android Studio Kotlin调用任何活动,android,android-studio,kotlin,android-fragments,fragment,Android,Android Studio,Kotlin,Android Fragments,Fragment,我可以从片段按钮调用另一个活动吗? 我已经看到了stackoverflow中的所有问题,但还没有找到解决方案 这是我的MainActivity片段代码 package com.example.apptest import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup

我可以从片段按钮调用另一个活动吗? 我已经看到了stackoverflow中的所有问题,但还没有找到解决方案

这是我的MainActivity片段代码

package com.example.apptest

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_home.*

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [HomeFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class HomeFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false)
        

    }




    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment HomeFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            HomeFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

这是我的片段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"
    tools:context=".HomeFragment">

    <TextView
        android:textColor="#34495e"
        android:textSize="20sp"
        android:layout_marginTop="100dp"
        android:id="@+id/mytitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:text="BELAJAR BERSAMA NATSUZD"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:textColor="#34495e"
        android:textSize="20sp"
        android:id="@+id/japantitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日本語"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mytitle" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:layout_marginTop="50dp"
        android:id="@+id/buttonImage"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/custome_button"
        android:drawableStart="@drawable/book50"
        android:drawableLeft="@drawable/book50"
        android:gravity="center"
        android:padding="15dp"
        android:text="KOSAKATA"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/japantitle"
        tools:ignore="OnClick" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:text="BELAJAR SOAL"
        android:gravity="center"
        android:textSize="15sp"
        android:textStyle="bold"
        android:padding="15dp"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/exam"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:id="@+id/buttonImage1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custome_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonImage"
        android:drawableStart="@drawable/exam" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:text="PERCAKAPAN"
        android:gravity="center"
        android:textSize="15sp"
        android:padding="15dp"
        android:textStyle="bold"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/chat"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:id="@+id/buttonImage2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custome_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonImage1"
        android:drawableStart="@drawable/chat" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:text="POLA KALIMAT"
        android:gravity="center"
        android:textSize="15sp"
        android:padding="15dp"
        android:textStyle="bold"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/variable"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:id="@+id/buttonImage3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custome_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonImage2"
        android:drawableStart="@drawable/variable" />


</androidx.constraintlayout.widget.ConstraintLayout>
如何使用片段中的按钮连接到第二个活动?当然可以

为了从片段启动活动,请重写片段中的onViewCreated方法。在其内部,键入:

buttonImage2.setOnClickListener {
    val intent = Intent(context, SoalActivity::class.java)
    startActivity(intent)
}

就是这样:

只需使用startActivityintent,您就应该搜索如何从片段、startActivitycontext、SoalActivity::class.java```buttonImage.setOnClickListener{val intent=Intentcontext、SoalActivity::class.java startActivityintent}运行活动“``我把这段代码放在HomeFragment.kt上的onCreateView中,但不起作用是的,这就是答案,它只是使用java,昨天我还不知道已经有一个功能可以自动更改编程语言,只是为了澄清上面的内容:这在onCreateView中不起作用。这需要在onViewCreatedthank中完成,非常有帮助,非常有效
buttonImage2.setOnClickListener {
    val intent = Intent(context, SoalActivity::class.java)
    startActivity(intent)
}