Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 适配器有问题。android.content.res.Resources$NotFoundException:资源ID#0x0_Java_Android_Listadapter - Fatal编程技术网

Java 适配器有问题。android.content.res.Resources$NotFoundException:资源ID#0x0

Java 适配器有问题。android.content.res.Resources$NotFoundException:资源ID#0x0,java,android,listadapter,Java,Android,Listadapter,所以我在适配器方面遇到了很多麻烦。我不确定出了什么问题。我收到的错误是 06-2619:44:59.228:E/AndroidRuntime(7275):android.content.res.Resources$NotFoundException: Resource ID #0x0 现在我要把我所有的代码都贴在这里。有很多,所以我要指出我认为问题所在。我只是不知道怎么修 MainActivity.java public class MainActivity extends Activ

所以我在适配器方面遇到了很多麻烦。我不确定出了什么问题。我收到的错误是

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);
        }
    }
公共类MainActivity扩展活动{
私人抽屉布局mDrawerLayout;
私有列表视图mDrawerList;
私有操作bardrawertoggle mDrawerToggle;
私有字符序列mDrawerTitle;
私有字符序列mTitle;
私有字符串[]mplanettiles;
语境;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle=mDrawerTitle=getTitle();
mPlanetTitles=getResources().getStringArray(R.array.planets\u数组);
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ListView)findViewById(R.id.left\u抽屉);
//设置抽屉打开时覆盖主要内容的自定义阴影
mDrawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);(问题就在这里)
//我们将新生成的列表传递给适配器
ArrayList=new ArrayList();
for(int i=0;i
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;
    }



}
package com.example.android.navigationdrawerexample;
导入java.util.ArrayList;
导入java.util.List;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类SideMenuAdapter扩展了ArrayAdapter{
私人语境;
私有int rowViewResourceId;
//private List sideMenuItemsList=new ArrayList();
//私有ImageView SideMenuItemion;
私有文本视图sideMenuItemName;
私有列表对象;
私有int textViewResourceId;
public SideMenuAdapter(上下文上下文、int textViewResourceId、列表对象){
超级(上下文、textViewResourceId、对象);
this.textViewResourceId=textViewResourceId;
this.context=上下文;
这个。对象=对象;
}
public int getCount(){
返回此.objects.size();
}
公共字符串getItem(int索引)
<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>
<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" />
private int rowViewResourceId;

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