Java 导航抽屉不工作
我的应用程序中有一个导航抽屉,我正在尝试使用它的内容,这是一个简单的计数器。每当我点击计数器,我的应用程序就会崩溃,我的logcat会显示视图未找到等。下面是我的三个文件start。java是我的主java文件,fragtasbeeh是我的计数器片段,xml文件。thnx提前:Java 导航抽屉不工作,java,android,android-fragments,navigation-drawer,Java,Android,Android Fragments,Navigation Drawer,我的应用程序中有一个导航抽屉,我正在尝试使用它的内容,这是一个简单的计数器。每当我点击计数器,我的应用程序就会崩溃,我的logcat会显示视图未找到等。下面是我的三个文件start。java是我的主java文件,fragtasbeeh是我的计数器片段,xml文件。thnx提前: public class Start extends Activity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; privat
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);
}
}
}
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文件,按钮和文本视图之间存在空格或对齐问题
我通过创建一个relativelayout解决了这个问题。这不是我要找的。你能告诉我我的代码有什么问题吗?甚至你可以在列表07-05 03:25:49.949:E/AndroidRuntime(1053):java.lang.IllegalStateException:指定的子级已经有父级了。必须首先对子级的父级调用removeView()。
<?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>