有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java My应用程序在嵌套到另一个类中时不会修改文本?

下面的Android代码应该修改“Hello World!”在今天说“今天是八月七日”,在一年中的其他任何一天说“错误”。然而,我得到的只是“你好,世界!”奇怪的是,我在一个全新的程序上以完全相同的方式运行代码,但没有将类CheckDate嵌套在类FirstFragment中。我试图通过删除FirstFragment类来解决这个问题,但这在我的MainActivity类中导致了一个错误,我用它为应用程序创建了一个侧栏应用程序。我能做些什么来保留侧栏,并让应用程序修改文本“Hello World!”它应该说什么

第一个片段

package com.example.gcsd.gcsdapp;

import 安卓.app.Fragment;
import 安卓.icu.util.Calendar;
import 安卓.icu.util.GregorianCalendar;
import 安卓.os.Bundle;
import 安卓.support.annotation.Nullable;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.support.v4.app.FragmentManager;
import 安卓.widget.TextView;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.widget.TextView;
import 安卓.icu.util.GregorianCalendar;
import 安卓.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");
    }

}
}
}

第一个片段$CheckDate。xml

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

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

</RelativeLayout>

主要活动

package com.example.gcsd.gcsdapp;

import 安卓.os.Bundle;
import 安卓.support.design.widget.FloatingActionButton;
import 安卓.support.design.widget.Snackbar;
import 安卓.support.v4.app.FragmentManager;
import 安卓.view.View;
import 安卓.support.design.widget.NavigationView;
import 安卓.support.v4.view.GravityCompat;
import 安卓.support.v4.widget.DrawerLayout;
import 安卓.support.v7.app.ActionBarDrawerToggle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.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();
安卓.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) 个答案