android - 抽屉导航不工作并已停止

标签 android navigation-drawer

我尝试了来自 http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/ 的简单抽屉导航示例site,,,, 但不幸的是模拟器停止了 为什么代码不起作用,我是否必须添加任何支持库...?帮助 plz...提前致谢

下面附上了

在我的 Mainactivity.java 中显示错误

ArrayAdapter adapter = new ArrayAdapter(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

错误: 1.类型安全:构造函数ArrayAdapter(Context, int, Object[])属于原始类型ArrayAdapter。对泛型类型 ArrayAdapter 的引用应该被参数化

我的 Activity main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFF"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>

我的fragmentone.xml,two.xml,three.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="one" />
</LinearLayout>

主 Activity .java

package com.example.navigations;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {
    final String[] data ={"one","two","three"};
    final String[] fragments ={
            "com.example.navigationdrawer.FragmentOne",
            "com.example.navigationdrawer.FragmentTwo",
            "com.example.navigationdrawer.FragmentThree"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         ArrayAdapter adapter = new ArrayAdapter(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

         final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
         final ListView navList = (ListView) findViewById(R.id.drawer);
         navList.setAdapter(adapter);
         navList.setOnItemClickListener(new OnItemClickListener(){
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                         drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                                 @Override
                                 public void onDrawerClosed(View drawerView){
                                         super.onDrawerClosed(drawerView);
                                         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                                         tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
                                         tx.commit();
                                 }
                         });
                         drawer.closeDrawer(navList);
                 }
         });
         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
         tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
         tx.commit();
    }

}

我的 Fragmentone.java,

package com.example.navigations;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {

    public static Fragment newInstance(Context context) {
        FragmentOne f = new FragmentOne();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
        return root;
    }

}

日志:

02-26 03:08:31.120: E/AndroidRuntime(1137): Process: com.example.navigations, PID: 1137 
02-26 03:08:31.120: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigations/com.example.navigations.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public 

最佳答案

References to generic type ArrayAdapter should be parameterized

表示在创建 ArrayAdapter 实例时需要指定参数类型:

ArrayAdapter<String> adapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_list_item_1, data);

关于android - 抽屉导航不工作并已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22035653/

相关文章:

android - 具有向后兼容性 android 的抽屉导航

android - TextView 顶部填充问题?

java - android.content.res.Resources$NotFoundException : String resource ID #0x0

android - 牛轧糖状态栏上的抽屉导航?

android - fragment 事务加载空 View 但 fragment 在旋转设备后显示

android - Navigation-drawer group header - 如何设置?

android - 抽屉导航 : How do I set the selected item at startup?

android - Exoplayer View 无法找到 player.prepare() 函数

java - 抽屉导航 : how to make the item direct you to a website. Android Studio

android - 带有所有特殊符号的正则表达式