2013-06-27 90 views
1

所以我遇到了一些适配器问题。我不确定什么是错的。我收到的错误是

06-2619:44:59.228:E/AndroidRuntime(7275):android.content.res.Resources$NotFoundException: Resource ID #0x0 

现在我要在这里发布我的所有代码。有很多,所以我会指出我相信问题在哪里。我只是不知道如何解决它。

MainActivity.java

public class MainActivity extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 

    private CharSequence mDrawerTitle; 
    private CharSequence mTitle; 
    private String[] mPlanetTitles; 

    Context mContext; 


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

     mTitle = mDrawerTitle = getTitle(); 
     mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     // set a custom shadow that overlays the main content when the drawer opens 
     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);(PROBLEM LIES HERE) 

     // We pass our newly generated list to the adapter 
     ArrayList<String> tempTiles = new ArrayList<String>(); 
     for(int i = 0; i < mPlanetTitles.length; i++){ 
      tempTiles.add(mPlanetTitles[i]); (PROBLEM LIES HERE) 

     } 
     SideMenuAdapter adapter = new SideMenuAdapter(this, R.layout.drawer_list_item, tempTiles); 

     mDrawerList.setAdapter(adapter); 

     // set up the drawer's list view with items and click listener 
//  mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
//    R.layout.drawer_list_item, mPlanetTitles)); 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     // enable ActionBar app icon to behave as action to toggle nav drawer 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     // ActionBarDrawerToggle ties together the the proper interactions 
     // between the sliding drawer and the action bar app icon 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description for accessibility */ 
       R.string.drawer_close /* "close drawer" description for accessibility */ 
       ) { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      selectItem(0); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    /* Called whenever we call invalidateOptionsMenu() */ 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // If the nav drawer is open, hide action items related to the content view 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
     menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); 
     return super.onPrepareOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // The action bar home/up action should open or close the drawer. 
     // ActionBarDrawerToggle will take care of this. 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     // Handle action buttons 
     switch(item.getItemId()) { 
     case R.id.action_websearch: 
      // create intent to perform web search for this planet 
      Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 
      intent.putExtra(SearchManager.QUERY, getActionBar().getTitle()); 
      // catch event that there's no activity to handle intent 
      if (intent.resolveActivity(getPackageManager()) != null) { 
       startActivity(intent); 
      } else { 
       Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); 
      } 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

SideMenuAdapter.java

package com.example.android.navigationdrawerexample; 

import java.util.ArrayList; 
import java.util.List; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 



public class SideMenuAdapter extends ArrayAdapter<String> { 
    private Context context; 
    private int rowViewResourceId; 
    //private List<SideMenuListItem> sideMenuItemsList = new ArrayList<SideMenuListItem>(); 

    //private ImageView SideMenuItemIcon; 
    private TextView sideMenuItemName; 
    private List<String> objects; 
    private int textViewResourceId; 


    public SideMenuAdapter(Context context, int textViewResourceId, List<String> objects){ 
     super(context, textViewResourceId, objects); 
     this.textViewResourceId = textViewResourceId; 
     this.context = context; 
     this.objects= objects; 

    } 

    public int getCount(){ 
     return this.objects.size(); 
    } 

    public String getItem(int index) { 
     return this.objects.get(index); 
    } 

    public View getView(int position, View convertView, ViewGroup parent){ 

     View row = convertView; (PROBLEM LIES HERE) 
     if (row == null) { 
      LayoutInflater inflater = (LayoutInflater) this.getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(rowViewResourceId, parent, 
        false); 
     } 

     String sideMenuListItem = getItem(position); 

     //thinking about making a boolean to say if it's a title then do this. 
     if(position == 0){ 
      //set the text to the xml format 
      sideMenuItemName = (TextView) row.findViewById(R.id.list_header_title); 
      sideMenuItemName.setText(sideMenuListItem); 

      //need to make the text gone so it doesn't take up space 
      //this lets us put the title where we want it 
      TextView setT = (TextView) row.findViewById(R.id.text1); 
      setT.setVisibility(View.GONE); 

      //need to make row icon gone so it doesn't take up space 
      //this lets us put the icon where we want it 
      //ImageView setTV = (ImageView) row.findViewById(R.id.row_icon); 
      //setTV.setVisibility(View.GONE); 

      //setting the color for the divider 
      View divider= (View) row.findViewById(R.id.customdivider); 
      divider.setBackgroundColor(0xFFFF0000); 

     } 
     else{ 
      //lets find our items that we want the list to be populated 
      sideMenuItemName = (TextView) row.findViewById(R.id.list_header_title); 


      //need to make the title gone so it doesn't take up space 
      //this lets us put the title where we want it 
      TextView setTV = (TextView) row.findViewById(R.id.list_header_title); 
      setTV.setVisibility(View.GONE); 

      //set the text 
      sideMenuItemName.setText(sideMenuListItem); 
// 
//   int imageResource = context.getResources().getIdentifier(
//     slidingMenuListItem.IconResourceId, null, 
//     context.getPackageName()); 
// 
//   //set the image 
//   slidingMenuItemIcon.setImageResource(imageResource); 

      //setting the color for the divider 
      View divider= (View) row.findViewById(R.id.customdivider); 
      divider.setBackgroundColor(0xFFFFFF00); 
     } 


     return row; 
    } 



} 

XML文件

activity_main.xml中

drawer_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?android:attr/activatedBackgroundIndicator" 
     android:gravity="center_vertical" 
     android:minHeight="?android:attr/listPreferredItemHeightSmall" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:textAppearance="?android:attr/textAppearanceListItemSmall" 
     android:textColor="#fff" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/list_header_title" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="10dp" /> 
    </LinearLayout> 

    <View 
     android:id="@+id/customdivider" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" /> 

</LinearLayout> 

fragment_planet.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:gravity="center" 
    android:padding="32dp" /> 

请帮帮忙!

+0

因此,logcat中没有行号。你确定你用过的所有drawable都存在吗? –

回答

1

你已经在你的适配器

private int rowViewResourceId; 

private int textViewResourceId; 

定义这些变量在你的构造函数,你没有指派rowViewResourceIdtextViewResourceId

public SideMenuAdapter(Context context, int textViewResourceId, List<String> objects) 
{ 
     super(context, textViewResourceId, objects); 
     this.textViewResourceId = textViewResourceId; 
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
     this.context = context; 
     this.objects= objects; 

} 

在您getView()身边,你正在使用rowViewResourceId尚未分配,可以在声明中分配一个值。

public View getView(int position, View convertView, ViewGroup parent){ 

     View row = convertView; (PROBLEM LIES HERE) 
     if (row == null) { 
      LayoutInflater inflater = (LayoutInflater) this.getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(rowViewResourceId, parent, 
        false); 
     } 
    // ...... 
} 

这就是为什么你会得到资源错误。

+0

你已经解决了!非常感谢。 –

相关问题