我正在使用ListViews和ArrayAdapters在Kotlin中开发一个应用程序,这是我编写的代码,似乎已经过时了。也许还有ArrayAdapters的替代品,但我不知道有任何替代品

class ListFrag : ListFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list2, container, false)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val data = arrayOf("1. Text", "2. Text", "3.Text");

        val itemsAdapter : ArrayAdapter<String> = ArrayAdapter(context, android.R.layout.simple_list_item_1);
        this.listAdapter = itemsAdapter;
    }

    override fun onListItemClick(l: ListView, v: View, position: Int, id: Long) {
        super.onListItemClick(l, v, position, id)
    }

}

这是我运行程序时收到的错误消息
 Process: com.example.fragments2, PID: 15030
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragments2/com.example.fragments2.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
        at androidx.fragment.app.ListFragment.ensureList(ListFragment.java:370)
        at androidx.fragment.app.ListFragment.onViewCreated(ListFragment.java:151)
        at androidx.fragment.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManagerImpl.java:1144)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:851)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1133)
        at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1393)
        at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3205)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.fragments2.MainActivity.onCreate(MainActivity.kt:10)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

最佳答案



您的LogCat响应告诉您代码有什么问题。遵循,因为它说此问题将得到解决。

关于android - Activity 无法启动,并且初始化数组适配器语法不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61611816/

10-09 06:02