为什么我的应用程序在嵌套在另一个类中时应该修改文本?

时间:2017-08-04 18:12:31

标签: java android android-fragments import

Android下面的代码应该修改声明" Hello World!"说"今天是8月4日"今天,说" ERROR"一年中的任何其他日子。但是,我得到的只是" Hello World!"奇怪的是,我在完全新的程序上以完全相同的方式运行代码,但没有在类FirstFragment中嵌套类CheckDate。我试图通过删除FirstFragment类来解决这个问题,但是这导致了我的MainActivity类中的错误,我用它为应用程序创建了侧边栏应用程序。有什么我可以做的来保持侧边栏,并让应用程序修改文本" Hello World!"它应该说什么?

FirstFragment

package com.example.gcsd.gcsdapp;

import android.app.Fragment;
import android.icu.util.Calendar;
import android.icu.util.GregorianCalendar;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.icu.util.GregorianCalendar;
import android.icu.util.Calendar;

public class FirstFragment extends Fragment {

View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.home, container, false);
    return myView;

}

public class CheckDate extends AppCompatActivity {

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

        TextView textView = (TextView) findViewById(R.id.textView);

        GregorianCalendar cal = new GregorianCalendar();

        System.out.println(cal.get(Calendar.DATE)); //prints 4
        System.out.println(cal.get(Calendar.MONTH) + 1); //this starts at 0, not 1. Therefore, add 1
        System.out.println(cal.get(Calendar.YEAR)); //2017

        if ((cal.get(Calendar.DAY_OF_MONTH) + 0 == 4) && (cal.get(Calendar.MONTH) + 1 == 8)) { //Is it the fourth of the month of 2017?
            System.out.println("TODAY IS AUGUST FOURTH");
            textView.setText("TODAY IS AUGUST FOURTH");
        } else {
            System.out.println("ERROR");
            textView.setText("ERROR");
        }

    }
}
}

First Fragment $ CheckDate.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gcsd.gcsdapp.FirstFragment$CheckDate">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:text="Hello World!"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

MainActivity

package com.example.gcsd.gcsdapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.FragmentManager;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    android.app.FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.nav_home) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new FirstFragment())
                .commit();
    } else if (id == R.id.nav_events) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new SecondFragment())
                .commit();
    } else if (id == R.id.nav_schooltool) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new ThirdFragment())
                .commit();
    } else if (id == R.id.nav_lessonreminders) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new FourthFragment())
                .commit();
    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

0 个答案:

没有答案