我有主類和更多的2片段類,但其中之一是從FragmentActivity擴展,當我從一個類不是從FragmentActivity擴展,而是從片段擴展, 有用。我怎麼能調用從appCompacAtivity擴展的主類中的FragmentActivity擴展的片段?從主類調用FragmentActivity(appCompatActivity)
這裏是主要的活動我的代碼(Principal_eng.Java):
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
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.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class Principal_eng extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.principal_eng);
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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fr = getFragmentManager();
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.ceramicas) {
fr.beginTransaction()
.replace(R.id.content_main, new Frag1())//Here i get an error
.commit();
} else if (id == R.id.tijolos) {
fr.beginTransaction()
.replace(R.id.content_main, new Frag2())
.commit();
} else if (id == R.id.ohms) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
下面是一個片段(Frag1.Java)
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag1 extends FragmentActivity {
public View onCreateView(LayoutInflater inf, ViewGroup cont, Bundle savedInst) {
View v = inf.inflate(R.layout.frag1, cont, false);
return v;
}
}
代碼從片段代碼兩個(Frag2.Java)
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag2 extends Fragment{
public View onCreateView(LayoutInflater inf,ViewGroup cont, Bundle savedInst){
View v = inf.inflate(R.layout.frag2,cont,false);
return v;
}
}
FragmentActivity不是片段。是一項活動。 – Blackbelt
但我想知道如何從主類調用FragmentActivity,這可能嗎? – Fantoche