2015-06-24 2 views
0

Программа работает правильно, но когда я открываю этот фрагмент, приложение вылетает.При использовании cardView в фрагменте возникает ошибка при развороте макета?

HomeFragment.java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    TextView homeFeatured = (TextView) container.findViewById(R.id.featured_home); 
    TextView homeFeaturedViewAll = (TextView) container.findViewById(R.id.featured_view_all_home); 
    TextView homeNewReleases = (TextView) container.findViewById(R.id.new_releases_home); 
    TextView homeNewReleasesViewAll = (TextView) container.findViewById(R.id.new_releases_view_all_home); 
    TextView homeTopReads = (TextView) container.findViewById(R.id.top_reads_home); 
    TextView homeTopReadsViewAll = (TextView) container.findViewById(R.id.top_reads_view_all_home); 

    return inflater.inflate(R.layout.fragment_home, container, false); 

} 

fragment_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.pratilipi.www.pratilipi.HomeFragment"> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 
<!-- TODO: Update blank fragment layout --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="#808080" 
    android:id="@+id/featured_home_layout" 
    android:elevation="2dp"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/featured_home" 
     android:text="@string/home_featured" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:textSize="16dp" 
     android:textColor="#ffffff" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/featured_view_all_home" 
     android:text=">" 
     android:gravity="right" 
     android:textSize="15dp" 
     android:textColor="#ffffff" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="10dp"/> 
    </RelativeLayout> 

      <android.support.v7.widget.CardView 
       android:layout_width="85dp" 
       android:layout_height="140dp" 
       android:layout_gravity="center_horizontal"> 
       <TextView 
        android:text="Basic CardView" 
        android:layout_marginTop="0dp" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center" 
        android:layout_centerVertical="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentEnd="true" /> 
      </android.support.v7.widget.CardView> 

    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="400dp"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:id="@+id/new_releases_home_layout" 
      android:background="#808080" 
      android:elevation="2dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/new_releases_home" 
       android:text="@string/new_releases_home" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:textSize="16dp" 
       android:textColor="#ffffff" 
       /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/new_releases_view_all_home" 
       android:text=">" 
       android:gravity="right" 
       android:textSize="15dp" 
       android:textColor="#ffffff" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="10dp"/> 

     </RelativeLayout> 

       </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="400dp"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:id="@+id/top_reads_home_layout" 
      android:background="#808080" 
      android:elevation="2dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/top_reads_home" 
       android:text="@string/home_top_reads" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:textSize="16dp" 
       android:textColor="#ffffff" 
       /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/top_reads_view_all_home" 
       android:text=">" 
       android:gravity="right" 
       android:textSize="15dp" 
       android:textColor="#ffffff" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="10dp"/> 

     </RelativeLayout> 

    </RelativeLayout> 

</LinearLayout> 
</ScrollView> 
</LinearLayout> 

В журнал ошибок он указывает оператор возврата строки onCreateView. Без просмотра карты приложение работает правильно. Есть ли способ решить это? Что я должен сделать, чтобы решить проблему с этим типом приложения. Заранее большое спасибо .

+0

Вы также можете опубликовать журнал – Sree

+0

, вы должны опубликовать свой журнал ошибок – myatmins

+0

У вас есть библиотека карточек в вашем файле градации? compile 'com.android.support:cardview-v7:21.+' –

ответ

1

Вы импортируете библиотеку карточек в свой файл градации?

компиляции 'com.android.support:cardview-v7:21.+'

После добавления этой строки, синхронизировать Gradle проект.

+0

Спасибо тонну! это сработало ! Но я все еще получаю некоторые проблемы с рендерингом, такие как Missing Styles? выбрана правильная тема для вашего макета? –

+0

Если ваша проблема связана с рендером ... Иногда рендеринг показывает проблемы, которые на самом деле не существуют. Проверьте запуск приложения на своем устройстве. CardViews представляют некоторые различия в API ниже 21 (круглые углы и т. Д.) Из-за внутренних проблем с производительностью. Дайте ему время и терпение. Я рекомендую вам попробовать их во всех различных API, которые вы можете. –

+0

Большое спасибо, плохо попробуйте все API. Еще раз спасибо. –

0

Сначала необходимо раздуть макет.

View view = inflater.inflate(R.layout.fragment_home, container, false); 

затем использовать

TextView homeFeatured = (TextView) view.findViewById(R.id.featured_home); 

и остальные коды ..

+0

Снова возникает проблема с рендерингом Отсутствующие стили? выбрана правильная тема для вашего макета? Это ошибка, которую я получаю. –

0

Я импортировал Gradle файлы, и она работала.