I'm inflating linearlayout
to display facebook's banner ad. It is showing following error:
I added the same codes, same dependency to show fb's banner ad in another project and it is working there but it is showing this error in this project. I also replaced the linear layout with relativelayout
and it is showing same error.
These are my codes:
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AudienceNetworkAds.initialize(this);
adView = new com.facebook.ads.AdView(this, getResources().getString(R.string.facebook_banner), AdSize.BANNER_HEIGHT_50);
RelativeLayout adContainer = findViewById(R.id.fbAdsView);
adContainer.addView(adView);
adView.loadAd();
RateItDialogFragment.show(this, getSupportFragmentManager());
initVar();
initView();
loadData();
initListener();
}
@Override
protected void onPause() {
super.onPause();
//unregister broadcast receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(newNotificationReceiver);
}
@Override
protected void onResume() {
super.onResume();
//register broadcast receiver
IntentFilter intentFilter = new IntentFilter(AppConstant.NEW_NOTI);
LocalBroadcastManager.getInstance(this).registerReceiver(newNotificationReceiver, intentFilter);
initNotification();
}
// received new broadcast
private BroadcastReceiver newNotificationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
initNotification();
}
};
@Override
public void onBackPressed() {
AppUtilities.tapPromptToExit(mActivity);
}
private void initVar() {
mActivity = MainActivity.this;
mContext = getApplicationContext();
mContentList = new ArrayList<>();
mFavoriteList = new ArrayList<>();
}
private void initView() {
setContentView(R.layout.activity_main);
mNotificationView = (RelativeLayout) findViewById(R.id.notificationView);
mImgBtnSearch = (ImageButton) findViewById(R.id.imgBtnSearch);
mRecycler = (RecyclerView) findViewById(R.id.rvContent);
mRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mAdapter = new ContentAdapter(mContext, mActivity, mContentList);
mRecycler.setAdapter(mAdapter);
initToolbar(false);
initDrawer();
initLoader();
}
private void initListener() {
//notification view click listener
mNotificationView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityUtilities.getInstance().invokeNewActivity(mActivity, NotificationListActivity.class, false);
}
});
// Search button click listener
mImgBtnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityUtilities.getInstance().invokeNewActivity(mActivity, SearchActivity.class, false);
}
});
mFavoriteDbController.insertData(model.getTitle(), model.getSubTitle(), model.getDetails(), model.getImageUrl());
mContentList.get(position).setFavorite(true);
mAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), getString(R.string.added_to_fav), Toast.LENGTH_SHORT).show();
}
break;
case R.id.card_view_top:
ActivityUtilities.getInstance().invokeDetailsActiviy(mActivity, DetailsActivity.class, model, false);
break;
default:
break;
}
}
});
}
private void loadData() {
showLoader();
// Initialize Favorite Database
mFavoriteDbController = new FavoriteDbController(mContext);
mFavoriteList.addAll(mFavoriteDbController.getAllData());
loadJson();
}
private void loadJson() {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(AppConstant.CONTENT_FILE)));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
parseJson(sb.toString());
}
private void parseJson(String jsonData) {
try {
JSONObject jsonObjMain = new JSONObject(jsonData);
JSONArray jsonArray1 = jsonObjMain.getJSONArray(AppConstant.JSON_KEY_ITEMS);
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject jsonObj = jsonArray1.getJSONObject(i);
String title = jsonObj.getString(AppConstant.JSON_KEY_TITLE);
String subTitle = jsonObj.getString(AppConstant.JSON_KEY_SUB_TITLE);
String imageUrl = jsonObj.getString(AppConstant.JSON_KEY_IMAGE_URL);
String details = jsonObj.getString(AppConstant.JSON_KEY_DETAILS);
// Check for favorite
boolean isFavorite = false;
for (int j = 0; j < mFavoriteList.size(); j++) {
if (mFavoriteList.get(j).getTitle().equals(title)) {
isFavorite = true;
break;
}
}
mContentList.add(new Contents(title, subTitle, imageUrl, details, isFavorite));
}
} catch (JSONException e) {
e.printStackTrace();
}
hideLoader();
mAdapter.notifyDataSetChanged();
}
public void initNotification() {
NotificationDbController notificationDbController = new NotificationDbController(mContext);
TextView notificationCount = (TextView) findViewById(R.id.notificationCount);
notificationCount.setVisibility(View.INVISIBLE);
ArrayList<NotificationModel> notiArrayList = notificationDbController.getUnreadData();
if (notiArrayList != null && !notiArrayList.isEmpty()) {
int totalUnread = notiArrayList.size();
if (totalUnread > 0) {
notificationCount.setVisibility(View.VISIBLE);
notificationCount.setText(String.valueOf(totalUnread));
} else {
notificationCount.setVisibility(View.INVISIBLE);
}
}
}
}
This is layout:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/fbAdsView"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
tools:targetApi="lollipop" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_3dp"
android:layout_below="@+id/rvContent" />
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/fbAdsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_centerHorizontal="true"
app:layout_constraintBottom_toBottomOf="parent"
/>
</RelativeLayout>
I am so confused why am I getting this error.
In order to find the views inside, you have to define the initView(), right below the oncreate as follows, then you can find the views inside the layout.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
// follows with rest of your code
}