2017-11-11 2 views
-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 
+0

Adding a fragment to an activityの下であなたはおそらくエラー – MadScientist

+1

を投稿することができます 'bu'は –

+0

あなたのlogcatを入れnullです。 – Deepa

答えて

0

Fragmentを追加するには2つの方法があります。ここ

は、活動の規範でありますActivity

  1. あなたはここに行っているように、静的にそれを追加します。

    <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" /> 
    
  2. をこのようなViewGroupにそれを動的に追加します。

    ExampleFragment fragment = new ExampleFragment(); 
    fragmentTransaction.add(R.id.fragment_container, fragment); 
    fragmentTransaction.commit(); 
    

あなたがしようとしてきたようです両方ともここに。だから、あなたは安全にあなたが静的にそれを追加したいと仮定すると、このコードのすべてを削除することができます。詳しくhereで説明

// Remove all this code 
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() 
} 

関連する問題