我是Android的新手,只是试图通过参考在线教程来实现这些片段。 我在Fragment.kt的第一行收到错误。我完全按照所示步骤完成了所有步骤,甚至尝试在谷歌上搜索错误但似乎没有任何对我有用。我得到的错误如下。
12-23 19:15:53.369 11656-11656/com.example.mandar.testapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mandar.testapp, PID: 11656
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mandar.testapp/com.example.mandar.testapp.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2687)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$1100(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at com.example.mandar.testapp.BlankFragment.onCreateView(BlankFragment.kt:0)
at android.app.Fragment.performCreateView(Fragment.java:2236)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:986)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1161)
at android.app.BackStackRecord.run(BackStackRecord.java:800)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1575)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6481)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$1100(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
MainActivity.kt
package com.example.mandar.testapp
import android.app.Activity
import android.app.Fragment
import android.os.Bundle
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var manager = fragmentManager
var fragment : Fragment? = manager.findFragmentById(R.id.lll)
if(fragment == null){
fragment = BlankFragment()
manager.beginTransaction().replace(R.id.lll,fragment).commit()
}
}
}
activity_main.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"
tools:context="com.example.mandar.testapp.MainActivity">
<FrameLayout
android:id="@+id/lll"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
BlankFragment.kt
package com.example.mandar.testapp
import android.os.Bundle
import android.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* A simple [Fragment] subclass.
* Use the [BlankFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class BlankFragment : Fragment() {
// TODO: Rename and change types of parameters
private var mParam1: String? = null
private var mParam2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments != null) {
mParam1 = arguments.getString(ARG_PARAM1)
mParam2 = arguments.getString(ARG_PARAM2)
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false)
}
companion object {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private val ARG_PARAM1 = "param1"
private val ARG_PARAM2 = "param2"
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
fun newInstance(param1: String, param2: String): BlankFragment {
val fragment = BlankFragment()
val args = Bundle()
args.putString(ARG_PARAM1, param1)
args.putString(ARG_PARAM2, param2)
fragment.arguments = args
return fragment
}
}
}// Required empty public constructor
和fragment_blank.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mandar.testapp.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
任何建议为什么这个简单的代码不起作用? 在这里,我用片段替换FrameLayout lll。
答案 0 :(得分:2)
错误说明:指定为非null的参数为null :方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数 savedInstanceState 。那么savedInstanceState可以为null,所以你必须添加?在片段中捆绑后。
print(result.head(10))
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 \
Aaron M 97.0 88.0 86.0 78.0 90.0 85.0 96.0 69.0 95.0 81.0
Ab M NaN 6.0 NaN 5.0 NaN NaN NaN NaN NaN NaN
Abb M 5.0 NaN NaN NaN NaN NaN 6.0 5.0 NaN NaN
Abbie F 98.0 88.0 84.0 104.0 137.0 107.0 140.0 124.0 119.0 115.0
M NaN NaN NaN 5.0 NaN NaN NaN 5.0 NaN NaN
Abbott M NaN NaN NaN NaN 6.0 NaN NaN NaN NaN NaN
Abby F 6.0 5.0 7.0 5.0 9.0 8.0 6.0 12.0 6.0 10.0
Abe M 45.0 47.0 50.0 37.0 46.0 39.0 49.0 40.0 53.0 43.0
Abel M 14.0 6.0 16.0 11.0 8.0 9.0 14.0 10.0 8.0 13.0
Abelina F NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
... 1904 1905 1906 1907 1908 1909 1910 1911 1912 \
Aaron M ... 117.0 96.0 96.0 130.0 114.0 142.0 145.0 187.0 303.0
Ab M ... NaN NaN NaN NaN NaN NaN NaN NaN NaN
Abb M ... NaN NaN NaN NaN NaN NaN NaN NaN NaN
Abbie F ... 80.0 73.0 72.0 79.0 84.0 57.0 79.0 77.0 90.0
M ... NaN 5.0 NaN NaN NaN NaN 8.0 NaN NaN
Abbott M ... NaN NaN NaN NaN NaN 5.0 NaN 8.0 8.0
Abby F ... NaN NaN 6.0 5.0 NaN 8.0 6.0 13.0 8.0
Abe M ... 52.0 65.0 50.0 65.0 57.0 67.0 74.0 94.0 172.0
Abel M ... 10.0 6.0 11.0 10.0 15.0 16.0 11.0 30.0 40.0
Abelina F ... NaN NaN NaN NaN NaN NaN NaN NaN NaN
1913
Aaron M 417.0
Ab M NaN
Abb M 5.0
Abbie F 121.0
M 7.0
Abbott M 5.0
Abby F 11.0
Abe M 202.0
Abel M 48.0
Abelina F NaN
[10 rows x 30 columns]
答案 1 :(得分:0)
请使用AppCompatActivity
代替Activity
并导入import android.support.v4.app.Fragment
代替import android.app.Fragment
对于BlankFragment
片段并将fragmentManager
替换为supportFragmentManager
,它的效果非常好。
这是您修改后的代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var manager = supportFragmentManager
var fragment : Fragment? = manager.findFragmentById(R.id.lll)
if(fragment == null){
fragment = BlankFragment()
manager.beginTransaction().replace(R.id.lll,fragment).commit()
}
}
}
和片段
import android.os.Bundle
import android.support.annotation.Nullable
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class BlankFragment : Fragment() {
// TODO: Rename and change types of parameters
private var mParam1: String? = null
private var mParam2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments != null) {
mParam1 = arguments.getString(ARG_PARAM1)
mParam2 = arguments.getString(ARG_PARAM2)
}
}
override fun onCreateView(inflater: LayoutInflater?, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater?.inflate(R.layout.fragment_blank, container, false)
}
companion object {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private val ARG_PARAM1 = "param1"
private val ARG_PARAM2 = "param2"
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
fun newInstance(param1: String, param2: String): BlankFragment {
val fragment = BlankFragment()
val args = Bundle()
args.putString(ARG_PARAM1, param1)
args.putString(ARG_PARAM2, param2)
fragment.arguments = args
return fragment
}
}
}