java - 尝试在空对象引用新活动上调用虚拟方法“java.lang.String android.content.Context.getPackageName()”
问题描述
当我尝试使用此代码开始新活动时,出现错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
class ApiConnection {
var baseApiURl = "http://localhost:8081"
var data = arrayListOf<User>()
fun connectApi(): ApiService {
val retrofit = Retrofit.Builder()
.baseUrl(baseApiURl)
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(ApiService::class.java)
}
fun loginRequest(
login: String,
password: String
){
val service = connectApi()
val call = service.login(login, password)
call.enqueue(object: Callback<UserResponse> {
override fun onFailure(call: Call<UserResponse>?, t: Throwable?) {
Log.v("retrofit", "call failed")
}
override fun onResponse(call: Call<UserResponse>?, response: Response<UserResponse>?) {
addDataToList(response)
}
})
}
fun addDataToList(response: Response<UserResponse>?) {
var mainActivity = MainActivity()
data.add(
User(
response!!.body()!!.idUser,
response!!.body()!!.login,
response!!.body()!!.password,
response!!.body()!!.name,
response!!.body()!!.surname,
response!!.body()!!.lastLogin
)
)
mainActivity.loginIntoServer(data)
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val api = ApiConnection()
button_checkIfThisAccountExit_mainActivity.setOnClickListener {
api.loginRequest(editText_login_activityMain.text.toString(), editText_password_mainActivity.text.toString())
}
}
fun loginIntoServer(data: ArrayList<User>) {
val context = this
val intent = Intent(context, Main2Activity::class.java)
startActivity(intent)
}
}
有什么想法会导致此错误吗?我正在使用改造来连接我的 API,在解析结果数据并将其添加到列表中后,我想运行新活动(类似于登录系统。输入用户名/密码后,我想更改活动)。
解决方案
您的问题的根源是var mainActivity = MainActivity()
,您永远不会通过调用来创建活动实例,constructor
因为那是系统的工作,请android
参阅
您的问题的正确解决方案是LiveData。但是如果你想让你当前的代码按原样工作,你必须context
object
在你的 ApiConnection中传递 a class
,
fun addDataToList(context: Context, response: Response<UserResponse>?) {
data.add(
User(
response!!.body()!!.idUser,
response!!.body()!!.login,
response!!.body()!!.password,
response!!.body()!!.name,
response!!.body()!!.surname,
response!!.body()!!.lastLogin
)
)
val intent = Intent(context, Main2Activity::class.java)
context.startActivity(intent)
}
还要更新 的签名loginRequest
以接受Context
来自您的对象activity
,使其如下所示
fun loginRequest(context: Context, login: String, password: String)
现在从您的活动中将其称为
api.loginRequest((this as Context), editText_login_activityMain.text.toString(), editText_password_mainActivity.text.toString())
推荐阅读
- javascript - 如何让 Material UI 过渡组件完全填充其父组件?
- node.js - 无法设置简单的 Node.js 服务器
- angular - 在我的 Angular 应用程序中对 JokeAPI 的 API 调用进行观察/订阅时遇到问题
- shopify - Shopify-.liquid 文件:如何添加 js 验证(使用我拥有的 onsubmit)以存在表单?
- php - Reactjs:生产环境:net::ERR_CERT_COMMON_NAME_INVALID
- go - 如何使用 go 的 net/http 或类似替代方法保存在 multipart/form-data POST 请求中收到的文件?
- sql - SSMS 数据透视表/取消重复
- c++ - 如何将模板函数传递给 POSIX 线程(pthread)
- c# - 命名空间“枚举”中不存在类型或命名空间名称“Parse”
- javascript - 只有第一次打开屏幕才有效?