Tag: android fragments

膨胀类碎片android kotlin的错误

所以,首先我回答你的问题:“你真的吗?答案很多。” 是的,我知道,但是我真的试图像第三天那样解决这个问题:在所有的SOF中,我的错误是没有问题的。 我有两个类似的项目:Java和Kotlin。 但是这个f @#^%!@ ing只能在Kotlin中出错。 但XML是身份的100%。 所以,错误是: android.view.InflateException: Binary XML file line #8: Error inflating class fragment 。 我的activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layoutMain" android:orientation="vertical"> <fragment android:id="@+id/top_fragment" android:name="com.example.weatherapp_kotlin.fragments.TopFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </fragment> <fragment android:id="@+id/bottom_fragment" android:name = "com.example.weatherapp_kotlin.fragments.BottomFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="4"> </fragment> </LinearLayout> 我试图做的事情: 要将XML中的android:name =""更改为class = "" 在第一个位置设置android:name或class 将LinearLayout更改为RelativeLayout和ConstraintLayout 要改变类extends到FragmentActivity (现在是Fragment和在Java中工作正常) 清理项目 我的LinearLayout有一个id ,一个orientation […]

简化在Kotlin中添加和替换碎片

我正在做一个小应用程序,只有几个屏幕,我想要作为一个单一的活动调用几个片段。 我有一些Android的Java的经验,但使用Kotlin是一个新的领域。 我已经理解了如何基本上在Kotlin中添加片段,但我想以可重用的方式来完成。 我遇到了这里提到的解决方案,它的工作,但有一些我不明白。 我的下面MainActivity到目前为止,但我必须在onCreate方法中创建var welcomeFragment,就像我在这里: class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val toggle = ActionBarDrawerToggle( this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) drawer_layout.addDrawerListener(toggle) toggle.syncState() nav_view.setNavigationItemSelectedListener(this) var welcomeFragment = Fragment.instantiate(this@MainActivity, WelcomeFragment::class.java!!.getName()) addFragment( welcomeFragment, welcome) } inline fun android.support.v4.app.FragmentManager.inTransaction(func: android.support.v4.app.FragmentTransaction.() -> android.support.v4.app.FragmentTransaction) { beginTransaction().func().commit() } fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int){ […]

Kotlin:找不到符号类的片段或其他Android类

我有一个java片段与viewPager在它.. public class FragmentWithViewPager extends Fragment { private class ViewPagerAdapter extends FragmentStatePagerAdapter { ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { Fragment fragment = new DeshFalView(); //<– Problem here Bundle args = new Bundle(); args.putInt("index", i); fragment.setArguments(args); return fragment; } } } 现在我有另一个片段,将被填充在上面的片段,并写在kotlin像这样: class DeshFalView : Fragment(), DeshfalContract.View { //More code… } […]

如何在android kotlin的适配器中调用片段

我正面临着如何从kotlin中的适配器中调用片段的问题。 请解决我的问题,而不使用活动上下文。 我正面临着如何从kotlin中的适配器中调用片段的问题。 请解决我的问题,而不使用活动上下文。 import android.app.FragmentManager import android.content.Context import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import com.bumptech.glide.Glide import com.tk.amp.foodieflavours.Adapters.DashBoardAdapter.ViewHolder import com.tk.amp.foodieflavours.Fragments.FragmentOneList import com.tk.amp.foodieflavours.ModelClass.DashBoardModel import com.tk.amp.foodieflavours.R class DashBoardAdapter (var context: Context,var arrayList: ArrayList<DashBoardModel>) : RecyclerView.Adapter<ViewHolder>(){ override fun onBindViewHolder(holder: ViewHolder?, position: Int) { var dashBoardModel = arrayList.get(position) holder?.textView?.setText(dashBoardModel.title) Glide.with(context).load(dashBoardModel.images).into(holder?.imageView) holder?.itemView?.setOnClickListener { if […]

Kotlin Android扩展和保留片段

我在项目中使用Kotlin Android扩展 ,遇到了一些我无法理解的行为。 我使用这段代码来保留我的片段: val fragment = fragmentManager.findFragmentByTag("hello") ?: HelloFragment() fragmentManager.beginTransaction() .replace(R.id.fragment_container, fragment, "hello") .commit() 这是保留的Fragment : import kotlinx.android.synthetic.hello.* public class HelloFragment : Fragment() { val text = "Hello world!" override fun onCreate(savedInstanceState: Bundle?) { super<Fragment>.onCreate(savedInstanceState) setRetainInstance(true) } override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater?.inflate(R.layout.hello, container, false) } override fun […]

FragmentTransaction不会在Kotlin Android项目中编译

在用Android学习Kotlin的过程中,编译失败和一般无用的错误文本已经让我难住了。 错误文字说明如下: 所提供的参数都不能调用以下函数。 在android.app.FragmentTransaction中定义的add(Fragment !, String!)add(Int,Fragment!)定义在android.app.FragmentTransaction 在这两个例子中的片段! 文字以红色突出显示。 我知道Kotlin是指带有!的Java类,但我似乎无法理解为什么它不满意我提供输入的方式。 任何有识之士将不胜感激。 fun displayEditRoutine(){ //Set our variables var ft = fragmentManager.beginTransaction() //Basic "newInstance" constructor to avoid omitting necessary variables var frag = EditRoutine.newInstance(mRoutineID,this) //Here is where error occurs ft.add(R.id.are_container, frag).commit() } 正在引用的EditRoutine类: class EditRoutine : Fragment() { //Variables private var mRoutineID: String? = null private var mListener: […]

如何使用kotlin使用片段

我无法找到如何使用kotlin使用片段。 我在onCreateView方法中遇到错误,请帮助我。 ListaFragment.kt: class ListaFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle): View? { val view = inflater.inflate(R.layout.fragment_lista, container, false) return view } } fragment_lista.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAlignment="center" android:text="Hello"/> </LinearLayout> MainActivity.kt: class MainActivity : AppCompatActivity() { private var listaFragment: ListaFragment? = […]

在kotlin中找不到getChildFragmentManager()

如何在kotlin片段类中使用getChildFragmentManager() ? 我知道它非常基本的问题。 但没有找到这个文件。 我在谷歌搜索搜索,但没有找到结果。

类型在kotlin中不匹配

我是Kotlin语言和面向对象编程的新手,所以我想知道为什么在Android中从类Fragment()继承的类的实例类型是错误的。 这些是我的代码 Fragment.kt class MyFragment: Fragment() { //do something } MainActivity.kt class MainActivity : AppCompatActivity() { val fm : Fragment? = MyFragment() … } 为什么返回MyFragment类型,而不是片段? ?? 谢谢

Kotlin webview崩溃

我想从一个Fragment访问一个WebView和kotlin不停地说: kotlin.TypeCastException: null cannot be cast to non-null type android.webkit.WebView 我试图使其为默认,然后初始化它,但也不起作用。 任何建议,我怎样才能使其工作? 我的代码: private lateinit var webView: WebView override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater!!.inflate(R.layout.editor_edit_fragment, container, false) webView = view.findViewById(R.id.webview) as WebView resumeWebView() return view } XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webview" […]