我试图从上下文(在类的主构造函数中传递)中获取包引用,以获取不同类中的参数。不幸的是,我总是得到空指针异常。我不太确定为什么会这样,因为我以同样的方式获得fragmentManager的成功引用
这里是Fragement“ Config”中的“ Request”类的调用。我检查过了,那个背景!!不为空
答案 0 :(得分:0)
已解决:问题出在我的Intent课堂上,我忘了在此intent中放一些额外内容。
意图类:
fun putExtras(extras: Bundle){
intent.putExtras(extras)
}
在FragmentOperation类中
Intent(context).putExtras(bundle)
然后其余代码起作用
原始代码
1)
配置类
class Config : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.config)
FragmentOperation(this@Config).organizationNameConfig()
}
override fun onBackPressed() {
val dialog = Dialog(this@Config, getString(R.string.titleCancelConfiguration), getString(R.string.messageCancelConfiguration), R.drawable.round_exit_to_app_white_48, 2)
dialog.setLeftButtonText(R.string.yes)
dialog.getLeftButton().setOnClickListener {
dialog.dismiss()
finish()
}
dialog.setRightButtonText(R.string.back)
dialog.getRightButton().setOnClickListener {
dialog.dismiss()
}
}
}
ConfigFindOrganization片段
class ConfigFindOrganization{
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.config_database, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity!!.findViewById<Button>(R.id.configConfirm).setOnClickListener {
if (inputIsValid()) {
Request(context!!).findOrganization(organizationName.text.toString())
}
}
}
private fun inputIsValid(): Boolean {
val validation = Validation(context!!)
validation.checkEditTextInput(organizationName, R.string.inputDatabaseAddress)
return validation.inputIsValid()
}
请求类
public class Request {
private Context context;
private Arguments arguments;
public Request(Context context) {
this.context = context;
this.arguments = new Arguments(((Activity) context).getIntent().getExtras()); -> Here is the error
try {
} catch (Exception e) {
Log.i("output", "no arguments has been found " + e.toString());
}
setBasicParams();
}
}
参数类
class Arguments(private val bundle: Bundle) {} -> null value is passed
2)
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flashit.attendance, PID: 9636
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter bundle
at com.flashit.attendance.Arguments.<init>(Arguments.kt:5)
at com.flashit.attendance.Request.<init>(Request.java:48)
at com.flashit.attendance.ConfigFindOrganization$onViewCreated$1.onClick(ConfigFindOrganization.kt:24)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
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)
3)是的,但是我在代码中看不到任何错误
答案 1 :(得分:0)
尝试在调试器下逐步完成此操作:
public class Request {
private Context context;
...
public Request(Context context) {
this.context = context;
try {
Intent intent = context.getIntent();
Bundle extras = intent.getExtras(); // <-- I'll betcha' this is returning "null"
this.arguments = new Arguments(extras);
...
} catch (Exception e) {
Log.i("output", "no arguments has been found " + e.toString());
}
...