我正在与Kotlin一起使用android应用。我在AddDebtFragment中设置了3个微调器,在资源文件中设置了3个String数组。但是当我尝试从HomeFragment导航到该片段时,该应用程序终止,并且在Logcat的片段中显示了问题
这是我在fragment_add_debt.xml中的微调器
<Spinner
android:id="@+id/debtCategorySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/SpinnerStyle"
android:spinnerMode="dialog"
android:entries="@array/debtCategoryList"/>
<Spinner
android:id="@+id/financialNameSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
style="@style/SpinnerStyle"
android:entries="@array/financialNameList"/>
<Spinner
android:id="@+id/rateTypeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/SpinnerStyle"
android:spinnerMode="dialog"
android:entries="@array/rateTypeList"/>
资源文件中的字符串数组
<string-array name="financialNameList">
<item>Bangkok Bank</item>
<item>Kasikorn Bank</item>
<item>Krung Thai Bank</item>
<item>The Siam Commercial Bank</item>
<item>TMB Bank</item>
<item>Bank of Ayudhya</item>
</string-array>
<string-array name="debtCategoryList">
<item>Personal Loan</item>
<item>Vehicle Loan</item>
<item>Indonesia</item>
<item>Mortgage</item>
<item>Student Loan</item>
<item>Credit Card</item>
</string-array>
<string-array name="rateTypeList">
<item>Effective Rate</item>
<item>Flat Rate</item>
<item>Fixed Rate</item>
<item>MRR</item>
<item>MOR</item>
<item>MLR</item>
<item>Student Rate</item>
<item>Credit Card</item>
<item>Ceiling</item>
<item>Default</item>
</string-array>
AddDebtFragment
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.example.debtrearragement.R
import kotlinx.android.synthetic.main.fragment_add_debt.*
class AddDebtFragment : Fragment(), AdapterView.OnItemSelectedListener {
private val financialSpinner = financialNameSpinner
private val debtSpinner = debtCategorySpinner
private val rateSpinner = rateTypeSpinner
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_add_debt, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ArrayAdapter.createFromResource(context, R.array.financialNameList, android.R.layout.simple_spinner_item)
.also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
financialSpinner.adapter
financialSpinner.onItemSelectedListener
}
ArrayAdapter.createFromResource(this.context, R.array.debtCategoryList, android.R.layout.simple_spinner_dropdown_item)
.also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
debtSpinner.adapter
debtSpinner.onItemSelectedListener
}
ArrayAdapter.createFromResource(this.context, R.array.rateTypeList, android.R.layout.simple_spinner_dropdown_item)
.also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
rateSpinner.adapter
rateSpinner.onItemSelectedListener
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
override fun onItemSelected(parent: AdapterView<*>, view: View,position: Int, id: Long) {
}
}
编辑:1-这是Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.debtrearragement, PID: 29305
java.lang.IllegalStateException: financialSpinner must not be null
at com.example.debtrearragement.view.ui.debt.AddDebtFragment.onViewCreated(AddDebtFragment.kt:34)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2097)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1871)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1827)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 29305 SIG: 9
Application terminated.
编辑:2-我也看到它也显示在编译器上。
答案 0 :(得分:0)
使用kotlin合成属性时,无需为Spinners声明新变量。只需删除
private val financialSpinner = financialNameSpinner
private val debtSpinner = debtCategorySpinner
private val rateSpinner = rateTypeSpinner
并在代码中将financialSpinner
替换为financialNameSpinner
,将debtSpinner
替换为debtCategorySpinner
,将rateSpinner
替换为rateTypeSpinner
。
您可以从docs
中找到有关Kotlin Android扩展的更多信息修正警告
之所以收到此警告,是因为ArrayAdapter.createFromResource
需要一个Context对象,而context
(来自getContext)返回一个可为空的Context对象。您可以使用!!
ArrayAdapter.createFromResource(context!!, R.array.financialNameList, android.R.layout.simple_spinner_item)
阅读doc,以了解有关科特林的零安全的更多信息