提问人:Attaroqqi 提问时间:4/2/2020 最后编辑:Attaroqqi 更新时间:4/2/2020 访问量:270
java.lang.RuntimeException:使用不同版本的库和 sdk 时无法启动活动 ComponentInfo
java.lang.RuntimeException: Unable to start activity ComponentInfo when using different version of library and sdk
问:
问题出在下面的日志上。它实际上运行良好,但是当我使用相同的代码并在日志以下出现这些错误时运行它。当我尝试打开它时,我的应用程序总是强制关闭,因此我根本无法打开我的应用程序。我需要升级版本,因为 android studio 上的自适应图标仅在或更高版本上可用buildToolsVersion "25.0.3"
buildToolsVersion "29.0.2"
buildToolsVersion "26.0.0"
我在尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”时发现了相同的错误,但仍然没有正确答案,并且由于我使用了butterknife库,因此错误可能是因为库的版本,或者我的代码有问题。
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aat.simenwa, PID: 23292
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aat.simenwa/com.aat.simenwa.activity.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2976)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3113)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.aat.simenwa.activity.LoginActivity.onCreate(LoginActivity.java:55)
at android.app.Activity.performCreate(Activity.java:7224)
at android.app.Activity.performCreate(Activity.java:7213)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3113)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
I/com.aat.simenw: ProcessProfilingInfo new_methods=0 is saved saved_to_disk=0 resolve_classes_delay=8000
Process 23292 terminated.
LoginActivity 类:
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.aat.simenwa.R;
import com.aat.simenwa.util.SharedPrefManager;
import com.aat.simenwa.util.api.BaseApiService;
import com.aat.simenwa.util.api.UtilsApi;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
@BindView(R.id.etEmail) EditText etEmail;
@BindView(R.id.etPassword) EditText etPassword;
@BindView(R.id.btnLogin) Button btnLogin;
@BindView(R.id.btnRegister) Button btnRegister;
ProgressDialog loading;
Context mContext;
BaseApiService mApiService;
SharedPrefManager sharedPrefManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
ButterKnife.bind(this);
mContext = this;
mApiService = UtilsApi.getAPIService(); // meng-init yang ada di package apihelper
sharedPrefManager = new SharedPrefManager(this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loading = ProgressDialog.show(mContext, null, "Harap Tunggu...", true, false);
requestLogin();
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, RegisterActivity.class));
}
});
// Code berikut berfungsi untuk mengecek session, Jika session true ( sudah login )
// maka langsung memulai MainActivity.
if (sharedPrefManager.getSPSudahLogin()){
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
}
}
private void requestLogin(){
mApiService.loginRequest(etEmail.getText().toString(), etPassword.getText().toString())
.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
loading.dismiss();
try {
JSONObject jsonRESULTS = new JSONObject(response.body().string());
if (jsonRESULTS.getString("error").equals("false")){
// Jika login berhasil maka data nama yang ada di response API
// akan diparsing ke activity selanjutnya.
Toast.makeText(mContext, "BERHASIL LOGIN", Toast.LENGTH_SHORT).show();
String nama = jsonRESULTS.getJSONObject("user").getString("nama");
sharedPrefManager.saveSPString(SharedPrefManager.SP_NAMA, nama);
// Shared Pref ini berfungsi untuk menjadi trigger session login
sharedPrefManager.saveSPBoolean(SharedPrefManager.SP_SUDAH_LOGIN, true);
startActivity(new Intent(mContext, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
} else {
// Jika login gagal
String error_message = jsonRESULTS.getString("error_msg");
Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
loading.dismiss();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("debug", "onFailure: ERROR > " + t.toString());
loading.dismiss();
}
});
}
}
activity_login布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/background_gradient"
tools:context="com.aat.simenwa.activity.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-medium"
android:text="SIMENWA"
android:textColor="#fff"
android:textSize="48sp"
android:textStyle="normal|bold|italic" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif"
android:text="LOGIN"
android:textColor="#fff"
android:textSize="24sp" />
<EditText
android:id="@+id/etEmail"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:textColorHint="#80FFFFFF"
android:padding="8dp"
android:inputType="textEmailAddress"
android:background="@drawable/bg_rectangle_edittext"
android:textColor="#80FFFFFF"/>
<EditText
android:id="@+id/etPassword"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#80FFFFFF"
android:inputType="textPassword"
android:padding="8dp"
android:background="@drawable/bg_rectangle_edittext"
android:textColor="#80FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif"
android:text="Sekolah Tinggi Ilmu Statistik"
android:textColor="#fff"
android:textSize="18sp"/>
<Button
android:id="@+id/btnLogin"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="LOG IN"
android:background="@drawable/bg_rectangle_button"/>
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:fontFamily="cursive"
android:text="OR"
android:layout_gravity="center"/>
<Button
android:id="@+id/btnRegister"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="REGISTER"
android:background="@drawable/bg_rectangle_button"/>
</LinearLayout>
</RelativeLayout>
答: 暂无答案
评论
R2.id.my_viewId
R.id.my_viewId
findViewById
Kotlin