-1
、誰もがそれが言うように、私はこのコードでエラーを見つけるのを助けることができる、それが主な活動でフラグメントを膨らませることができず、途中で、私はアンドロイドに新しいです。私はのonCreate関数の最後の一つであることが、コードの膨張ラインを変更しようとしましたが、それは何もしませんでした。フラグメントエラー:XMLバイナリファイル:エラー膨らまフラグメントクラス
package com.voicenoteinc.tictactou
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v4.app.Fragment
import android.view.View
import android.widget.Button
import kotlinx.android.synthetic.main.fragment_test.*
class MainActivity : AppCompatActivity() {
var tstFragment:TestFragment? = null
val manager = supportFragmentManager
val FRAGMENT_TAG = "fragment_tag"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (manager.findFragmentByTag(FRAGMENT_TAG) != null) {
tstFragment = manager.findFragmentByTag(FRAGMENT_TAG) as TestFragment
}
if (tstFragment == null) {
tstFragment = TestFragment()
manager.beginTransaction().add(tstFragment, FRAGMENT_TAG).commit()
}
}
}
XML活動:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
tools:context="com.voicenoteinc.tictactou.MainActivity">
<fragment
android:id="@+id/fragment"
android:name="com.voicenoteinc.tictactou.TestFragment"
android:tag="fragment_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_test" />
</LinearLayout>
フラグメントクラス:
package com.voicenoteinc.tictactou
import android.content.Context
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_test.*
class TestFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
bu.setOnClickListener { bu.setBackgroundColor(Color.CYAN) }
return inflater!!.inflate(R.layout.fragment_test, container, false)
}
}// Required empty public constructor
Adding a fragment to an activity
の下であなたはおそらくエラー – MadScientistを投稿することができます 'bu'は –
あなたのlogcatを入れnullです。 – Deepa