自定义导航抽屉错误

时间:2014-06-17 00:03:05

标签: android navigation-drawer

我在MyAdapter中没有看到任何错误......但是每次我运行它都会强行关闭..这就是Logcat ..

  

06-17 07:30:15.250:E / AndroidRuntime(9282):at com.example.MyAdapter.getView(MainActivity.java:164)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.AbsListView.obtainView(AbsListView.java:1484)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.ListView.makeAndAddView(ListView.java:1786)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.ListView.fillDown(ListView.java:705)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.ListView.fillFromTop(ListView.java:762)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.ListView.layoutChildren(ListView.java:1639)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.AbsListView.onLayout(AbsListView.java:1314)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.View.layout(View.java:7224)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.View.layout(View.java:7224)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.FrameLayout.onLayout(FrameLayout.java:369)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.View.layout(View.java:7224)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1290)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1166)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.LinearLayout.onLayout(LinearLayout.java:1083)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.View.layout(View.java:7224)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.FrameLayout.onLayout(FrameLayout.java:369)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.View.layout(View.java:7224)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.widget.FrameLayout.onLayout(FrameLayout.java:369)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.View.layout(View.java:7224)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.ViewRoot.performTraversals(ViewRoot.java:1181)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.view.ViewRoot.handleMessage(ViewRoot.java:1913)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.os.Handler.dispatchMessage(Handler.java:130)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.os.Looper.loop(SourceFile:351)   06-17 07:30:15.250:E / AndroidRuntime(9282):在android.app.ActivityThread.main(ActivityThread.java:3821)   06-17 07:30:15.250:E / AndroidRuntime(9282):at java.lang.reflect.Method.invokeNative(Native Method)   06-17 07:30:15.250:E / AndroidRuntime(9282):at java.lang.reflect.Method.invoke(Method.java:538)   06-17 07:30:15.250:E / AndroidRuntime(9282):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:969)   06-17 07:30:15.250:E / AndroidRuntime(9282):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)   06-17 07:30:15.250:E / AndroidRuntime(9282):at dalvik.system.NativeStart.main(Native Method)

这是我在MyActivity中的代码..

public class MainActivity extends ActionBarActivity implements
    OnItemClickListener {

private DrawerLayout drawerLayout;
private ListView listView;
private ActionBarDrawerToggle drawerListener;
private MyAdapter myAdapter;

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

    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

    listView = (ListView) findViewById(R.id.drawerList);
    myAdapter = new MyAdapter(this);
    listView.setAdapter(myAdapter);
    listView.setOnItemClickListener(this);
    drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, " Drawer Closed",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, " Drawer Opened",
                    Toast.LENGTH_SHORT).show();
        }

    };
    drawerLayout.setDrawerListener(drawerListener);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    drawerListener.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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();
    if (drawerListener.onOptionsItemSelected(item)) {
        return true;
    }

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    drawerListener.onConfigurationChanged(newConfig);

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub

    selectItem(position);
}

public void selectItem(int position) {
    // TODO Auto-generated method stub
    listView.setItemChecked(position, true);

}

public void setTitle(String title) {
    getSupportActionBar().setTitle(title);
}

   }

 class MyAdapter extends BaseAdapter {
View row = null;
String[] menu;
private Context context;
int[] images = { R.drawable.ic_home, R.drawable.ic_map, R.drawable.ic_book,
        R.drawable.ic_fav, R.drawable.ic_settings };

public MyAdapter(Context context){
    menu = context.getResources().getStringArray(R.array.menu);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return menu.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return menu[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView == null){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.custom_row, parent, false);

    }else{
        row = convertView;
    }

    TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
    ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);
    titleTextView.setText(menu[position]);
    titleImageView.setImageResource(images[position]);


    return row;
}



   }

1 个答案:

答案 0 :(得分:0)

使用此

row = inflater.inflate(R.layout.custom_row, null);

而不是

row = inflater.inflate(R.layout.custom_row, parent, false);