简体   繁体   中英

navigation drawer not working

i have a navigation drawer in my app and i am trying to use its content which is a simple counter.whenever i click on the counter my app crashes my logcat shows view not found etc etc.here are my three files start.java is my main java file,fragtasbeeh is my counter fragment and xml file.thnx in advance:

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM