带有注释的空对象引用上的 setText
setText on null object reference with Annotations
它不是重复的,没有 ANDROIDANNOTATIONS 的同一个应用程序可以正常工作。
由于使用 AndroidAnnotations Framework 修改项目,我的工作应用已停止工作。在我看来,一切看起来都很好,AndroidStudio 中没有警告,直到 phone 上的 运行,应用程序出错:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.annotationstest/com.example.annotationstest.MainActivity_}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
我应该修复或添加到代码中的哪些内容?
MainActivity
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private static final String SHARED_PREFS_NAME = "abc";
public static final int REQUEST_CODE = 100;
private int number;
@ViewById(R.id.tv2)
TextView tv2;
@ViewById(R.id.tv3)
TextView tv3;
@ViewById(R.id.switch1)
Switch switch1;
@Click
void b1() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
number = preferences.getInt("NUMBER", 0);
tv2.setText(""+number);
switch1.setOnCheckedChangeListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
number = data.getExtras().getInt("number");
tv2.setText("" + number);
saveNumberToSharedPrefs(number);
}
}
}
private void saveNumberToSharedPrefs(int num) {
SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
preferences.edit().putInt("NUMBER", num).apply();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.switch1:
if (isChecked == true) {
tv3.setVisibility(View.VISIBLE);
} else {
tv3.setVisibility(View.INVISIBLE);
}
break;
}
}
}
根据文档,视图可能尚未注入 onCreate()
方法(参见 here):
When onCreate() is called, @ViewById fields are not set yet. Therefore, you can use @AfterViews on methods to write code that depends on views.
也就是说,您应该将代码从 onCreate()
移至新方法并使用 @AfterViews
对其进行注释
它不是重复的,没有 ANDROIDANNOTATIONS 的同一个应用程序可以正常工作。
由于使用 AndroidAnnotations Framework 修改项目,我的工作应用已停止工作。在我看来,一切看起来都很好,AndroidStudio 中没有警告,直到 phone 上的 运行,应用程序出错:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.annotationstest/com.example.annotationstest.MainActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我应该修复或添加到代码中的哪些内容?
MainActivity
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private static final String SHARED_PREFS_NAME = "abc";
public static final int REQUEST_CODE = 100;
private int number;
@ViewById(R.id.tv2)
TextView tv2;
@ViewById(R.id.tv3)
TextView tv3;
@ViewById(R.id.switch1)
Switch switch1;
@Click
void b1() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
number = preferences.getInt("NUMBER", 0);
tv2.setText(""+number);
switch1.setOnCheckedChangeListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
number = data.getExtras().getInt("number");
tv2.setText("" + number);
saveNumberToSharedPrefs(number);
}
}
}
private void saveNumberToSharedPrefs(int num) {
SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
preferences.edit().putInt("NUMBER", num).apply();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.switch1:
if (isChecked == true) {
tv3.setVisibility(View.VISIBLE);
} else {
tv3.setVisibility(View.INVISIBLE);
}
break;
}
}
}
根据文档,视图可能尚未注入 onCreate()
方法(参见 here):
When onCreate() is called, @ViewById fields are not set yet. Therefore, you can use @AfterViews on methods to write code that depends on views.
也就是说,您应该将代码从 onCreate()
移至新方法并使用 @AfterViews