Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.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
Android 循环遍历片段的第三次迭代中的一行出错_Android_Android Fragments_Navigation Drawer_Countdowntimer - Fatal编程技术网

Android 循环遍历片段的第三次迭代中的一行出错

Android 循环遍历片段的第三次迭代中的一行出错,android,android-fragments,navigation-drawer,countdowntimer,Android,Android Fragments,Navigation Drawer,Countdowntimer,我制作了一个android应用程序,我首先登录,然后显示一个带有导航抽屉的主要活动。导航抽屉具有主页和配置文件。主页显示一个倒计时计时器,而概要文件从数据库检索信息并显示它。 main.class第一次来的时候,它会回家并显示倒计时。然后我转到个人资料。配置文件页面显示正确。 现在我很难理解。然后我回到主页(显示正确),然后是配置文件,然后是主页(现在有一个错误) 我不知道为什么会有错误。请帮忙。我是android编程新手 java是显示倒计时的一个。 错误是一致的 TextView mText

我制作了一个android应用程序,我首先登录,然后显示一个带有导航抽屉的主要活动。导航抽屉具有主页和配置文件。主页显示一个倒计时计时器,而概要文件从数据库检索信息并显示它。 main.class第一次来的时候,它会回家并显示倒计时。然后我转到个人资料。配置文件页面显示正确。 现在我很难理解。然后我回到主页(显示正确),然后是配置文件,然后是主页(现在有一个错误)

我不知道为什么会有错误。请帮忙。我是android编程新手

java是显示倒计时的一个。 错误是一致的

TextView mTextField = (TextView)getActivity().findViewbById(R.id.welcome);
在第三次迭代中使用MenuFragment.java

这是主课

public class Main extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private TextView mTextField;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mMenuTitles;

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

    mTitle = mDrawerTitle = getTitle();
    mMenuTitles = getResources().getStringArray(R.array.menu_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, mMenuTitles));
    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);
}

/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(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()) {
    case R.id.action_websearch:
        // create intent to perform web search for this planet
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
        // catch event that there's no activity to handle intent
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
        }
        return true;
    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

    if (position == 1)
    {
        Fragment fragment = new FragmentProfile();
        Bundle args = new Bundle();

        args.putInt(FragmentProfile.ARG_MENU_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, "current_fragment").commit();
    }
    else
    {

    Fragment fragment = new MenuFragment();
    Bundle args = new Bundle();
    args.putInt(MenuFragment.ARG_MENU_NUMBER, 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



    //timer.cancel();

    }
    mDrawerList.setItemChecked(position, true);
    setTitle(mMenuTitles[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
 */





}
Logcat

E/AndroidRuntime(4456): FATAL EXCEPTION: main
E/AndroidRuntime(4456): Process: com.example.test, PID: 4456
E/AndroidRuntime(4456): java.lang.NullPointerException
E/AndroidRuntime(4456): at      com.example.test.MenuFragment$CounterClass.onTick(MenuFragment.java:108)
E/AndroidRuntime(4456): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
E/AndroidRuntime(4456): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(4456): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(4456): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(4456): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(4456): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(4456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(4456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(4456): at dalvik.system.NativeStart.main(Native Method)

我相信你在这里有比赛条件

该片段可能已与其父活动分离。完成后,您需要取消计时器。可能在onDestroyView

创建一个私有成员变量来保存计时器。在onCreateView方法中初始化它时使用它。然后,在重写的onDestroyView方法中使用它,如下所示:

private CounterClass timer;

@Override
public void onDestroyView()
{
    super.onDestroyView();

    if (timer != null)
    {
        timer.cancel();
        timer = null;
    }
}
此外,您可能希望通过检查getActivity()==null并在这种情况下提前返回来防止片段未附加到onTick方法中的活动

有关片段生命周期的更多信息,请查看此处:


谢谢你的回答。我今天就试着去做。还有一个问题。在MenuFragment.java中使用多个断点以调试模式运行时,代码似乎工作正常。你知道为什么会发生这种事吗?你能解释一下吗?或者如果可能的话,你能提供一些参考链接来研究这种行为吗?我想你所要做的就是导航到这个片段,然后离开它来获得崩溃。是的,我正在这样做。我将从家到配置文件,再到家,几次都没有在调试模式下崩溃。它只是工作。但是,当我删除断点,然后在调试模式下运行并反复切换片段时,我会崩溃。我认为这与按下“run”(运行)按钮以通过断点时产生的时间延迟有关,该断点可防止崩溃。
private CounterClass timer;

@Override
public void onDestroyView()
{
    super.onDestroyView();

    if (timer != null)
    {
        timer.cancel();
        timer = null;
    }
}