膨胀线性线性布局时出错
Error while inflating linear linearlayout
我正在充气 linearlayout
以显示 facebook 的横幅广告。它显示以下错误:
我添加了相同的代码,相同的依赖项以在另一个项目中显示 fb 的横幅广告并且它在那里工作但它在这个项目中显示此错误。我还用 relativelayout
替换了线性布局,它显示了同样的错误。
这些是我的代码:
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);
}
}
}
}
这是布局:
<?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>
我很困惑为什么会收到此错误。
为了找到里面的视图,你必须如下定义oncreate下面的initView(),然后你就可以找到布局里面的视图。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
// follows with rest of your code
}
我正在充气 linearlayout
以显示 facebook 的横幅广告。它显示以下错误:
我添加了相同的代码,相同的依赖项以在另一个项目中显示 fb 的横幅广告并且它在那里工作但它在这个项目中显示此错误。我还用 relativelayout
替换了线性布局,它显示了同样的错误。
这些是我的代码:
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);
}
}
}
}
这是布局:
<?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>
我很困惑为什么会收到此错误。
为了找到里面的视图,你必须如下定义oncreate下面的initView(),然后你就可以找到布局里面的视图。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
// follows with rest of your code
}