Java Kotlin应用程序打开活动2,按钮崩溃
这就是logcat中的错误,但是当我试图在清单中声明“CreateNoteFragment”时,只有.MainActivity&.SplashScreen出现,不允许我声明.CreateNoteFragment 我的CreateNoteFragment还引用了“BaseFragment”活动,如下所示。 我的按钮点击代码在。MainActivity是唯一不会被破坏的地方,下面是代码。当点击它崩溃的应用程序,任何帮助将不胜感激Java Kotlin应用程序打开活动2,按钮崩溃,java,android,android-studio,kotlin,Java,Android,Android Studio,Kotlin,这就是logcat中的错误,但是当我试图在清单中声明“CreateNoteFragment”时,只有.MainActivity&.SplashScreen出现,不允许我声明.CreateNoteFragment 我的CreateNoteFragment还引用了“BaseFragment”活动,如下所示。 我的按钮点击代码在。MainActivity是唯一不会被破坏的地方,下面是代码。当点击它崩溃的应用程序,任何帮助将不胜感激 android.content.ActivityNotFoun
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.moradi.moradinotespro/com.moradi.moradinotespro.CreateNoteFragment}; have you declared this activity in your AndroidManifest.xml?
包括舱单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moradi.moradinotespro">
<!-- Permissions
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"/>
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
CreateNoteFragment活动
class CreateNoteFragment : BaseFragment() {
var currentDate:String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_create_note, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
CreateNoteFragment().apply {
arguments = Bundle().apply {
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val sdf = SimpleDateFormat("dd/m/yyyy hh:m:ss")
currentDate = sdf.format(Date())
tvDateTime.text = currentDate
imgDone.setOnClickListener {
//Save Note
saveNote()
}
imgBack.setOnClickListener {
replaceFragment(HomeFragment.newInstance(), false)
}
}
private fun saveNote() {
if (etNoteTitle.text.isNullOrEmpty()) {
Toast.makeText(context, "Title Required", Toast.LENGTH_SHORT).show()
}
if (etNoteSubTitle.text.isNullOrEmpty()) {
Toast.makeText(context, "SubTitle Required", Toast.LENGTH_SHORT).show()
}
if (etNoteDesc.text.isNullOrEmpty()) {
Toast.makeText(context, "Note Required", Toast.LENGTH_SHORT).show()
}
launch {
val notes = Notes()
notes.title = etNoteTitle.text.toString()
notes.subTitle = etNoteSubTitle.text.toString()
notes.noteText = etNoteDesc.text.toString()
notes.dateTime = currentDate
context?.let {
NotesDatabase.getDatabase(it).noteDao().insertNotes(notes)
etNoteTitle.setText("")
etNoteSubTitle.setText("")
etNoteDesc.setText("")
}
}
}
private fun replaceFragment(fragment: Fragment, istransition: Boolean) {
val fragmentTransition = activity!!.supportFragmentManager.beginTransaction()
if (istransition) {
fragmentTransition.setCustomAnimations(
android.R.anim.slide_out_right,
android.R.anim.slide_in_left
)
}
fragmentTransition.replace(R.id.frame_layout, fragment)
.addToBackStack(fragment.javaClass.simpleName)
}
}
这里的问题是:
fabBtnCreateNote.setOnClickListener {
val intent = Intent(this, CreateNoteFragment::class.java)
startActivity(intent)
}
CreateNoteFragment不是活动,因此不能将其作为活动启动
首先,您需要创建main_activity.xml文件和带有“frame_layout”的Framelayout的单独id,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
您需要将HomeFragment替换为CreateNoteFragment:
fabBtnCreateNote.setOnClickListener {
val newFragment = CreateNoteFragment.newInstance()
activity?.let {activity->
val transaction = activity.supportFragmentManager.beginTransaction()
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment,CreateNoteFragment::class.simpleName);
// Commit the transaction
transaction.commit();
}
}
您的CreateNoteFragment已添加到后堆栈中,所以单击后退按钮时只需调用onBackPress():
更改您的代码:
imgBack.setOnClickListener {
activity.onBackPress()
}
检查是否在清单中定义了MainActivity。如果您显示splashscreen代码,如果您要从splashwell转到mainactivity,它很复杂,我想我正试图转到CreateNoteFragment活动,但在清单中,它不允许我将CreateNoteFragment添加到清单中,仅允许Splash和Main,但从Splash转到主片段,然后当我单击add note按钮转到CreateNoteFragment时,它崩溃我用更多的代码更新了这篇文章当我更改代码时,你不能在清单中添加片段,但它表示未解析引用:newFragment newFragment=new CreateNoteFragment();和未解决的参考:FragmentTransaction我刚刚用Kotlin版本更新了我的答案,希望它能解决您的问题谢谢,不幸的是,更新后的代码仍在崩溃,这很奇怪,但我认为这与其他原因有关,因为logcat现在显示了java.lang.ClassCastException:com.google.android.material.floatingactionbutton.floatingactionbutton无法转换为android.view.ViewGroup。请显示您的新代码和main_activity.xml代码!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}
fabBtnCreateNote.setOnClickListener {
val newFragment = CreateNoteFragment.newInstance()
activity?.let {activity->
val transaction = activity.supportFragmentManager.beginTransaction()
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment,CreateNoteFragment::class.simpleName);
// Commit the transaction
transaction.commit();
}
}
imgBack.setOnClickListener {
activity.onBackPress()
}