Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java setAdapter导致应用程序崩溃_Java_Android - Fatal编程技术网

Java setAdapter导致应用程序崩溃

Java setAdapter导致应用程序崩溃,java,android,Java,Android,当我调用fragment类中代码底部的setAdapter方法时,它导致我的应用程序在启动前崩溃。然而,当我把它注释掉时,这个应用程序运行得很好(显然没有适配器) package com.example.android.navigationdrawerexample; 导入android.app.Activity; 导入android.app.Fragment; 导入android.app.FragmentManager; 导入android.content.res.Configuration;

当我调用fragment类中代码底部的setAdapter方法时,它导致我的应用程序在启动前崩溃。然而,当我把它注释掉时,这个应用程序运行得很好(显然没有适配器)

package com.example.android.navigationdrawerexample;
导入android.app.Activity;
导入android.app.Fragment;
导入android.app.FragmentManager;
导入android.content.res.Configuration;
导入android.os.Bundle;
导入android.support.v4.app.ActionBarDrawerToggle;
导入android.support.v4.view.GravityCompat;
导入android.support.v4.widget.DrawerLayout;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
私人抽屉布局mDrawerLayout;
私有列表视图mDrawerList;
私有操作bardrawertoggle mDrawerToggle;
私有字符序列mDrawerTitle;
私有字符序列mTitle;
私有字符串[]mNavigationTitles;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle=mDrawerTitle=getTitle();
mNavigationTitles=getResources().getStringArray(R.array.Navigation_数组);
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ListView)findViewById(R.id.left\u抽屉);
//设置抽屉打开时覆盖主要内容的自定义阴影
mDrawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
//使用项目设置抽屉的列表视图,然后单击listener
mDrawerList.setAdapter(新阵列适配器,
R.layout.drawer_list_item,mNavigationTitles);
setOnItemClickListener(新的DrawerItemClickListener());
//启用ActionBar应用程序图标作为切换导航抽屉的操作
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//ActionBarDrawerToggle将适当的交互连接在一起
//在滑动抽屉和操作栏应用程序图标之间
mDrawerToggle=新操作bardrawertoggle(
此,/*主机活动*/
mDrawerLayout,/*抽屉布局对象*/
R.drawable.ic_抽屉,/*导航抽屉图像替换“Up”插入符号*/
R.string.drawer\u open,/*“open drawer”描述,用于辅助功能*/
R.string.drawer\u close/*“close drawer”可访问性说明*/
) {
公共无效onDrawerClosed(视图){
getActionBar().setTitle(mTitle);
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
打开图纸上的公共空白(视图抽屉视图){
getActionBar().setTitle(mDrawerTitle);
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(savedInstanceState==null){
选择项目(0);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。主菜单,菜单);
返回super.onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//操作栏home/up操作应打开或关闭抽屉。
//ActionBarDrawerToggle会处理好的。
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
//手柄动作按钮
开关(item.getItemId()){
违约:
返回super.onOptionsItemSelected(项目);
}
}
/*在导航抽屉中单击ListView的listner*/
私有类DrawerItemClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
选择项目(位置);
}
}
私有void selectItem(内部位置){
//通过替换片段更新主要内容
Fragment Fragment=新的NavigationFragment();
Bundle args=新Bundle();
args.putInt(NavigationFragment.ARG_NAVIGATION_位置,位置);
fragment.setArguments(args);
FragmentManager FragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment.commit();
//更新所选项目和标题,然后关闭抽屉
mDrawerList.setItemChecked(位置,true);
setTitle(mNavigationTitles[职位]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@凌驾
公共无效设置标题(字符序列标题){
mTitle=标题;
getActionBar().setTitle(mTitle);
}
/**
*使用ActionBarDrawerToggle时,必须在
*onPostCreate()和onConfigurationChanged()。。。
*/
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
//在onRestoreInstanceState发生后同步切换状态。
mDrawerToggle.syncState();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//将任何配置更改传递给抽屉toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
*“内容框架”中出现的片段显示了一颗行星
*/
公共静态类NavigationFragment扩展了片段{
公共静态最终字符串ARG\u NAVIGATION\u POSITION=“NAVIGATION\u POSITION”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u导航,容器,错误);
int i=getArguments().getInt(参数导航位置);
字符串导航=getResources().getStringArray(R.array.na
package com.example.android.navigationdrawerexample;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;

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

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mNavigationTitles;


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

    mTitle = mDrawerTitle = getTitle();
    mNavigationTitles = getResources().getStringArray(R.array.navigaton_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mNavigationTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        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(0);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@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;
    }
    // Handle action buttons
    switch(item.getItemId()) {

    default:
        return super.onOptionsItemSelected(item);
    }
}

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = new NavigationFragment();
    Bundle args = new Bundle();
    args.putInt(NavigationFragment.ARG_NAVIGATION_POSITION, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mNavigationTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}
/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@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 toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
 * Fragment that appears in the "content_frame", shows a planet
 */
public static class NavigationFragment extends Fragment {
    public static final String ARG_NAVIGATION_POSITION = "navgation_position";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
        int i = getArguments().getInt(ARG_NAVIGATION_POSITION);
        String navigation = getResources().getStringArray(R.array.navigaton_array)[i];

       AutoCompleteTextView countriesAutoComplete = (AutoCompleteTextView) getActivity().findViewById(R.id.countries_autoComplete);
       String[] countriesListFullName = getResources().getStringArray(R.array.countryNames);
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(countriesAutoComplete.getContext(), android.R.layout.simple_dropdown_item_1line, countriesListFullName);
       countriesAutoComplete.setAdapter(adapter);
       countriesAutoComplete.setThreshold(1);
       //int imageId = getResources().getIdentifier()(navigation.toLowerCase(Locale.getDefault()),
       //                "drawable", getActivity().getPackageName());
       // ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
        getActivity().setTitle(navigation);
        return rootView;
    }
}
AutoCompleteTextView countriesAutoComplete = (AutoCompleteTextView) getActivity().findViewById(R.id.countries_autoComplete);
AutoCompleteTextView countriesAutoComplete = (AutoCompleteTextView) rootView.findViewById(R.id.countries_autoComplete); // use rootView.findViewById