gpt4 book ai didi

java - 如何在其他类中执行 fragmentTransaction?

转载 作者:行者123 更新时间:2023-11-30 10:43:48 25 4
gpt4 key购买 nike

我想创建一个类,它的功能只是做一个 fragmentTransaction,但我有一个错误。下一个是我的课:

package net.elinformaticoenganchado.sergio.crossfighters.common;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import net.elinformaticoenganchado.sergio.crossfighters.R;
import net.elinformaticoenganchado.sergio.crossfighters.statics.Main;

/**
* @author Sergio Herrero Cruz <sergioherre96@gmail.com>
* @package net.elinformaticoenganchado.sergio.crossfighters.common
*/
public class FragmentsGoTo extends FragmentActivity{


public void goToMainFragment() {
Fragment fragment = new Main();
FragmentTransaction ftConfig = getSupportFragmentManager().beginTransaction();
ftConfig.replace(R.id.main_frame, fragment);
ftConfig.commit();
}
}

我在这里调用这个函数:

package net.elinformaticoenganchado.sergio.crossfighters.configuration;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import net.elinformaticoenganchado.sergio.crossfighters.R;
import net.elinformaticoenganchado.sergio.crossfighters.common.FragmentsGoTo;
import net.elinformaticoenganchado.sergio.crossfighters.entity.Configuration;

/**
* @author Sergio Herrero Cruz <sergioherre96@gmail.com>
*/
public class ConfigurationEdit extends Fragment implements View.OnClickListener{

EditText name;

/**
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed
* from a previous saved state as given here.
* @return Return the View for the fragment's UI, or null.
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.configuration_edit, container, false);

name = (EditText) view.findViewById(R.id.configuration_edit_name);
Button save = (Button) view.findViewById(R.id.configuration_edit_button);

save.setOnClickListener(this);

return view;
}

/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.configuration_edit_button) {
Configuration config = new Configuration(name.getText().toString());
config.save();
FragmentsGoTo fragmentsGoTo = new FragmentsGoTo();
fragmentsGoTo.goToMainFragment();
}
}

}

它们是简单的类。但是我有一个错误:

Process: net.elinformaticoenganchado.sergio.crossfighters, PID: 5116 java.lang.RuntimeException: Unable to start activity ComponentInfo{net.elinformaticoenganchado.sergio.crossfighters/net.elinformaticoenganchado.sergio.crossfighters.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference at net.elinformaticoenganchado.sergio.crossfighters.common.FragmentsGoTo.goToMainFragment(FragmentsGoTo.java:19) at net.elinformaticoenganchado.sergio.crossfighters.MainActivity.onCreate(MainActivity.java:42) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

那么我怎样才能在 java 中不出错。

谢谢。

编辑:添加 mainActivity 类:

package net.elinformaticoenganchado.sergio.crossfighters;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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 net.elinformaticoenganchado.sergio.crossfighters.common.FragmentsGoTo;
import net.elinformaticoenganchado.sergio.crossfighters.configuration.ConfigurationEdit;
import net.elinformaticoenganchado.sergio.crossfighters.entity.Configuration;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

FragmentsGoTo fragmentsGoTo = new FragmentsGoTo();
//@Todo change for start
//Configuration.deleteAll(Configuration.class);

if (Configuration.count(Configuration.class) == 0) {
Fragment fragment = new ConfigurationEdit();
FragmentTransaction ftConfig = getSupportFragmentManager().beginTransaction();
ftConfig.replace(R.id.main_frame, fragment);
ftConfig.commit();
} else {
fragmentsGoTo.goToMainFragment();
}

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.addDrawerListener(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();

if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

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

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

} 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;
}
}

最佳答案

由于我的评论有助于修复:

This is not the way to instantiate the activity (calling new). Google a bit, use intents. You need an activity which HOLDS the fragments, and then to swap the fragments in it.

关于java - 如何在其他类中执行 fragmentTransaction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com