findViewById返回null CustomSource cadslib

问题描述:

我一直在尝试在我正在处理的项目中使用cardslib lirary。我正在尝试使用CustomSource的缩略图。一切都按预期工作。但是,当我在扩展卡的类的setupInnerViewElements方法中调用findViewById以查找R.id.textview_oferta_preco时,它将返回null。findViewById返回null CustomSource cadslib

贝娄跟随我的课程和我的布局。

活动:

public class MenuOpcoes extends AppCompatActivity implements OfertasFragment.OnFragmentInteractionListener, NoticiasFragment.OnFragmentInteractionListener{ 

private Drawer.Result result = null; 
private AccountHeader.Result headerResult = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_menu_opcoes); 

    headerResult = new AccountHeader() 
      .withActivity(this) 
      .withHeaderBackground(R.drawable.header) 
      .addProfiles(
        new ProfileDrawerItem().withName("Oswaldo Roberto Marques").withEmail("[email protected]").withIcon(getResources().getDrawable(R.drawable.profile)) 
      ) 
      .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() { 
       @Override 
       public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) { 
        return false; 
       } 
      }) 
      .build(); 

    result = new Drawer() 
      .withActivity(this) 
      .withTranslucentStatusBar(false) 
      .withActionBarDrawerToggle(true) 
      .withAccountHeader(headerResult) 
      .addDrawerItems(
        new PrimaryDrawerItem().withName(R.string.drawer_item_listas).withIcon(FontAwesome.Icon.faw_list), 
        new PrimaryDrawerItem().withName(R.string.drawer_item_ofertas).withIcon(FontAwesome.Icon.faw_dollar), 
        new PrimaryDrawerItem().withName(R.string.drawer_item_noticias).withIcon(FontAwesome.Icon.faw_newspaper_o), 
        new SectionDrawerItem().withName(R.string.drawer_item_section_header), 
        new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog) 
      ) 
      .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) { 
        if (drawerItem instanceof Nameable) { 
         String opcaoSelecionada = MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()); 
         FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
         if (opcaoSelecionada.equals("Ofertas")) { 
          Toast.makeText(MenuOpcoes.this, MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()), Toast.LENGTH_SHORT).show(); 
          OfertasFragment ofertasFragment = new OfertasFragment(); 
          transaction.replace(R.id.home_layout_container, ofertasFragment); 
          transaction.addToBackStack("ofertas"); 
         } 
         if (opcaoSelecionada.equals(R.string.drawer_item_noticias)) { 
          Toast.makeText(MenuOpcoes.this, MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()), Toast.LENGTH_SHORT).show(); 
          NoticiasFragment noticiasFragment = new NoticiasFragment(); 
          transaction.replace(R.id.home_layout_container, noticiasFragment); 
          transaction.addToBackStack("noticias"); 
         } 
         transaction.commit(); 
        } 
       } 
      }).build(); 

    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_menu_opcoes, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    //int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    //if (id == R.id.action_settings) { 
    // return true; 
    //} 

    //return super.onOptionsItemSelected(item); 

    switch (item.getItemId()) { 
     case android.R.id.home: 
      onBackPressed(); 
      return true; 


     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

@Override 
public void onBackPressed() { 
    //handle the back press :D close the drawer first and if the drawer is closed close the activity 
    if (getFragmentManager().getBackStackEntryCount() > 0) { 
     getFragmentManager().popBackStack(); 
    } else { 
     this.finish(); 
    } 
} 

@Override 
public void onFragmentInteraction(Uri uri) { 

} 

CustomSource:

public class OfertasCardCustomSource extends Card { 


public OfertasCardCustomSource(Context context) { 
    super(context); 
    init(); 
} 

public OfertasCardCustomSource(Context context, int innerLayout) { 
    super(context, innerLayout); 
    init(); 
} 

private void init() { 
    CardHeader header = new CardHeader(getContext()); 
    header.setButtonOverflowVisible(true); 
    header.setTitle("Coca Cola 2,5L"); 
    addCardHeader(header); 

    CardThumbnail thumbnail = new CardThumbnail(getContext()); 
    thumbnail.setDrawableResource(R.drawable.coca_cola_25l); 
    addCardThumbnail(thumbnail); 
} 

@Override 
public void setupInnerViewElements(ViewGroup parent, View view) { 

    TextView title = (TextView) parent.findViewById(R.id.textview_oferta_preco); 
    title.setText("R$ 4,25"); 


    TextView subtitle = (TextView) parent.findViewById(R.id.textview_oferta_mercado); 
    subtitle.setText("Mercado Exemplo"); 


    RatingBar mRatingBar = (RatingBar) parent.findViewById(R.id.carddemo_gplay_main_inner_ratingBar); 


    mRatingBar.setNumStars(5); 
    mRatingBar.setMax(5); 
    mRatingBar.setStepSize(0.5f); 
    mRatingBar.setRating(4.7f); 
} 
} 

片段布局:

<ScrollView 
    android:id="@+id/card_scrollview" 
    style="@style/carddemo_default_container_padding" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 


     <!-- Custom Source Thumbnail--> 
     <TextView 
      style="@style/Theme.Carddemo.TitleText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/secao_ofertas"/> 


     <it.gmariotti.cardslib.library.view.CardViewNative 
      android:id="@+id/carddemo_thumb_customsource" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      style="@style/card_external" 
      card:card_layout_resourceID="@layout/native_card_thumbnail_layout"/> 
     <!-- Empty view--> 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="15dp"/> 

    </LinearLayout> 
</ScrollView> 

CustomSource布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="60dp" 
> 

<RatingBar 
    android:id="@+id/carddemo_gplay_main_inner_ratingBar" 
    style="@style/carddemo_myapps_main_inner_ratingbar" 
    android:numStars="5" 
    android:stepSize="1.0" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:id="@+id/textview_oferta_preco" 
    android:layout_width="wrap_content" 
    android:textSize="12sp" 
    android:textColor="@android:color/holo_green_light" 
    android:layout_alignParentRight="true" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:id="@+id/textview_oferta_mercado" 
    android:textSize="12sp" 
    android:textColor="#BBB" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

我不知道该怎么办。我正在使用Android Studio。谢谢。

+0

在'setContentView()'完成创建视图之前,可能会调用该方法。 – m0skit0

扩展Card时,必须通过传递要膨胀的布局引用来调用super

我在你的OfertasCardCustomSource类中看到你有两个构造函数,其中一个接收变量innerLayout,它被正确地传递给超类,另一个构造函数不设置布局。

你的问题可能是下列之一:

  • 你不设置在该构造的布局:你应该做的事情就像调用super(context, R.layout.the_layout_of_your_card) - 检查this reference一个更详细的例子
  • 你是不是调用构造函数,接收innerLayout任何地方

我建议你要做的是要么删除构造函数,没有收到布局要膨胀或通过这是我在上面留下的链接中完成的默认值。

+0

问题解决了!谢谢! – Oswaldo