我是一个全新的移动的应用程序开发和排序跌跌撞撞我的方式通过这个应用程序,但我一直在努力尝试和解决这个问题。我想要无数的教程,寻找开发者网站和搜索Stack Overflow,但我做的似乎没有工作,请你能帮助我吗?我正在使用Android Studio //Dolphin | 2021.3.1 Patch 1
和编码在Kotlin
。
我所要做的就是在fragment_delivery
中单击按钮android:id="@+id/button_customer_sign"
时打开fragment_sign
,然后在客户签名后,在fragment_sign
中单击按钮android:id="@+id/button_load_sign"
时返回fragment_delivery
。
我的build.gradle
中有以下内容
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.annotation:annotation:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.kyanogen.signatureview:signature-view:1.2'
implementation 'androidx.fragment:fragment-ktx:1.6.0-alpha03'
implementation 'androidx.fragment:fragment:1.6.0-alpha03'
debugImplementation 'androidx.fragment:fragment-testing:1.6.0-alpha03'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
// google support library ---------------------------------------------------------------------
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.browser:browser:1.4.0'
}
目前这是我的DeliveryFragment.kt
中的内容
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.delivery.R
import com.example.delivery.databinding.FragmentDeliveryBinding
import com.example.delivery.ui.sign.Sign
import android.support.v4.app.Fragment //This import is an error
import android.support.v4.app.FragmentManager //This import is an error
import android.support.v4.app.FragmentTransaction //This import is an error
class DeliveryFragment : Fragment() {
private var _binding: FragmentDeliveryBinding? = null
private var customerSign: Button? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val deliveryViewModel =
ViewModelProvider(this).get(DeliveryViewModel::class.java)
_binding = FragmentDeliveryBinding.inflate(inflater, container, false)
val root: View = binding.root
val customer = resources.getStringArray(R.array.customer_list)
val arrayAdapter = ArrayAdapter(requireContext(), R.layout.dropdown_item, customer)
binding.autoCompleteTextView.setAdapter(arrayAdapter)
return root
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContectView(com.example.delivery.R.layout.content_main)
customerSign = findViewById(com.example.delivery.R.id.button_customer_sign) as Button
customerSign.setOnClickListener(View.OnClickListener { openSignFragment() })
} //This entire method is an error
fun openSignFragment() {
val intent = Intent(this, SignFragment::class.java)
startActivity(intent)
} //This entire method is an error
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
listener
和open arguments
只是一个又一个错误,所以是这个.kt
文件的最后3个导入。我需要找到这个,因为我还有很多其他的按钮要尝试和解决。
请能有人协助我,预先谢谢。
1条答案
按热度按时间t30tvxxf1#
Intent需要Activity示例和类名,而fragment需要主机容器Activity正在运行。如果你转到另一个Activity,你将不得不再次使用它来在Activity容器中显示你的fragment。
请从onCreate中删除setContentView(),因为您在片段的onCreateView中返回了视图。
setContentView()使用布局填充当前窗口。
片段视图显示在Activity窗口内的容器中。
在片段onCreateView中只返回一次视图,在您的示例中,它是
return root
如果使用导航组件,请使用此选项-〉
或者,如果您不使用导航组件,则使用此选项-〉