导航抽屉不工作

时间:2014-07-05 07:13:05

标签: java android android-fragments navigation-drawer

我的应用程序中有一个导航抽屉,我正在尝试使用它的内容,这是一个简单的计数器。每当我点击计数器时我的应用程序崩溃我的logcat显示视图未找到等等。我的三个文件开始。 java是我的主要java文件,fragtasbeeh是我的计数器片段和xml file.thnx提前:

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

private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;

List<DrawerItem> dataList;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#076672")));
setContentView(R.layout.activity_start);



      // Initializing
 dataList = new ArrayList<DrawerItem>();

 mDrawerTitle = getTitle();
 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 mDrawerList = (ListView) findViewById(R.id.leftdrawer);
 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
 GravityCompat.START);

      // Add Drawer Item to dataList
 dataList.add(new DrawerItem("Search", R.drawable.ic_action_map));

 dataList.add(new DrawerItem("compass",
 R.drawable.ic_action_location_found));

 dataList.add(new DrawerItem("counter", R.drawable.ic_action_good));

 dataList.add(new DrawerItem("tings", R.drawable.ic_action_group));

 dataList.add(new DrawerItem("about us", R.drawable.ic_action_about));

 adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
                  dataList);

 mDrawerList.setAdapter(adapter);

 mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

 getActionBar().setDisplayHomeAsUpEnabled(true);
 getActionBar().setHomeButtonEnabled(true);

 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                 R.drawable.ic_drawer, R.string.drawer_open,
                  R.string.drawer_close) {
 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(-1);
      }


}


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

public void SelectItem(int possition) {

    if(possition==-1)
    {
        setTitle(getResources().getString(R.string.app_name));
    }
    else{
      Fragment fragment = null;
      Bundle args = new Bundle();
      switch (possition) {
      case 0:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                        .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                        .getImgResID());

            break;
               case 1:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                        .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                        .getImgResID());

            break;

      case 2:
            fragment = new FragTasbeeh();            
            break;


      default:
                       break;
      }

      fragment.setArguments(args);
      FragmentManager frgManager = getFragmentManager();
      frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                  .commit();

      mDrawerList.setItemChecked(possition, true);
      setTitle(dataList.get(possition).getItemName());
      mDrawerLayout.closeDrawer(mDrawerList);

}
}

@Override
public void setTitle(CharSequence title) {
      mTitle = title;
      getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      // Sync the toggle state after onRestoreInstanceState has occurred.
      mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      // Pass any configuration change to the drawer toggles
      mDrawerToggle.onConfigurationChanged(newConfig);
}

@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;
      }

      return false;
}
public class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {
SelectItem(position);

}
}



}

Fragtasbeeh.java

public class FragTasbeeh extends Fragment implements OnClickListener{


int count;
Button reset,add;
TextView counter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View v=inflater.inflate(R.layout.tasbeeh, container,true);


    reset=(Button) v.findViewById(R.id.reset);
    add=(Button)v.findViewById(R.id.count);
    counter=(TextView)v.findViewById(R.id.editText1);
    add.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    count=count+1;
    counter.setText(count);
}

});
    reset.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count=0;
            counter.setText(count);

        }
    });



    return v;

}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}



}

tasbeeh.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1"
    android:ems="10"
    android:inputType="number"
    android:maxHeight="60dp"
    android:textColor="#076672" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/reset"
    android:layout_width="117dp"
    android:layout_height="20dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="15dp"
    android:layout_weight="0.35"
    android:paddingLeft="50dp"
    android:text="@string/RESET"
    android:textAlignment="center"
    android:textSize="20dp" />

<Button
    android:id="@+id/count"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="15dp"
    android:layout_weight="0.85"
    android:text="count" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

正如您所见,此示例示例是简单的计数器example ....他们使用导航抽屉在列表视图中创建新项目

答案 1 :(得分:0)

问题出在xml文件中,按钮和textview之间存在空格或对齐问题 我通过制作一个relativelayout来解决它