我想夸大一个简单的观点。但是总是空!
我的代码是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btStandardBanner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ViewGroup bannerContainer = findViewById(R.id.standardBanner);
if (bannerContainer == null){
Log.e("BANNNNNER","IS NULL"); //ALWAYS RUNS HERE
}else{
Log.e("BANNNNNER","IS NOT NULL");
}
}
});
}
更新:
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/standardBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" />
</androidx.appcompat.widget.LinearLayoutCompat>
答案 0 :(得分:1)
尝试使用此代码-
axios
.get("https://jsonplaceholder.typicode.com/posts",{ params: {
page: state.page,
pageSize: state.pageSize,
sorted: state.sorted,
filtered: state.filterd
}})
您的代码表明您没有初始化protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.standardBanner).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ViewGroup bannerContainer = findViewById(R.id.standardBanner);
if (bannerContainer == null){
Log.e("BANNNNNER","IS NULL"); //ALWAYS RUNS HERE
}else{
Log.e("BANNNNNER","IS NOT NULL");
}
}
});
}
。
这就是为什么您得到standardBanner
的原因。
另一种尝试方式-
NullPointerException