I want create a class whose funtion is only do a fragmentTransaction but I have an error. My class is the next:
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 <[email protected]>
* @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();
}
}
And I call to this function here:
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 <[email protected]>
*/
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();
}
}
}
They are simple class. But I have an error:
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)
So how can i this in java without errors.
Thank you.
Edit: Add mainActivity class:
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;
}
}
MainActivity
class - specially theonCreate
method. This can help figure out why you are getting aNullPointerException
. – ishmaelMakitlagoToMainFragment()
- could you try and do:getActivity().getSupportFragmentManager()
instead of justgetSupportFragmentManager()
- see if this helps. – ishmaelMakitlanew
). Google a bit, use intents. You need an activity which HOLDS the fragments, and then to swap the fragments in it. – Vucko