2017-11-11 179 views
-1

任何人都可以幫助我找到此代碼中的錯誤,因爲它說,它不能膨脹主要活動中的片段,順便說一下,我是新來的android。我試圖將代碼的膨脹線更改爲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

你可以張貼錯誤 – MadScientist

+1

大概'bu'是空 –

+0

把你的logcat的。 – Deepa

回答

0

有兩種方法的Fragment增加一個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(); 
    

看來你已經嘗試做在這裏。所以,你可以安全地刪除所有這些代碼假設你想要靜態添加:

// 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() 
} 

詳細解釋hereAdding a fragment to an activity

相關問題