java - 适配器有问题。安卓.content.res.Resources$NotFoundException : Resource ID #0x0

标签 java android listadapter

所以我在使用适配器时遇到了很多麻烦。我不确定哪里出了问题。我收到的错误是

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

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

主 Activity .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" />

求助!

最佳答案

你已经在你的 Adapter 上定义了这些变量

private int rowViewResourceId;

private int textViewResourceId;

在您的构造函数中,您没有分配 rowViewResourceId,而是分配 textViewResourceId

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);
        }
   // ......
}

这就是您收到资源错误的原因。

关于java - 适配器有问题。安卓.content.res.Resources$NotFoundException : Resource ID #0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17332991/

相关文章:

java - android studio 找不到符号报错

java - 如何在 AsyncTask 中使用 ListAdapter?

android - 如何使用 FirebaseListAdapter 填充 ListView?

java - 加载大文件时tomcat重启超时

Java将静态引用引用为非静态方法

java - 是否可以扩展类的实例

带有增量构建的 ListAdapter 的 Android ListView

java - 在框架内重新绘制 JPanel

android - 在 Android/SQLite 中获取未使用的 rowid 的有效方法?

android - 在Android插件任务上调用doFirst