Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a problem with my Android project. I am working with fragments. One of the classes is the
MainActivity
class which extends
FragmentActivity
. And two classes which extend fragment. I want to call a
MainActivity
method from a fragment, but every time it gives me an
NullPointerException
. I have tried everything, but the problem still exists.
The
MainActivity
looks like this:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mySectionsPagerAdapter;
ViewPager myViewPager;
ActionBar myactionBar;
Schuldet_mir fr_schuldetMir;
Ich_schulde fr_ichSchulde;
public DataBaseHandler myDBHandler;
public String testString = null;
// ------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myactionBar = getActionBar();
myactionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setSectionsPagerAdapter();
initActionbar();
initFragments();
// ------------------------------------------------------------------------
@Override
protected void onResume() {
super.onResume();
openDB();
populateListViewFromDB();
// ------------------------------------------------------------------------
private void setSectionsPagerAdapter() {
mySectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
myViewPager = (ViewPager) findViewById(R.id.pager);
myViewPager.setAdapter(mySectionsPagerAdapter);
myViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
myactionBar.setSelectedNavigationItem(position);
// ------------------------------------------------------------------------
private void initActionbar() {
for (int i = 0; i < mySectionsPagerAdapter.getCount(); i++) {
myactionBar.addTab(myactionBar.newTab()
.setText(mySectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
// ------------------------------------------------------------------------
private void initFragments() {
fr_schuldetMir = (Schuldet_mir)mySectionsPagerAdapter.getItem(0);
fr_ichSchulde = (Ich_schulde)mySectionsPagerAdapter.getItem(1);
// ------------------------------------------------------------------------
private void openDB() {
myDBHandler = new DataBaseHandler(this);
myDBHandler.open();
private void closeDB() {
myDBHandler.close();
public String test() {
testString = "Test Test TEst!";
return testString;
// ------------------------------------------------------------------------
private void populateListViewFromDB() {
fr_schuldetMir.populateListViewFromDB();
// ------------------------------------------------------------------------
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
The fragment, which calls the method of the MainActivity
:
public class Schuldet_mir extends Fragment {
public static String ARG_SECTION_NUMBER = "section_number";
ListView listView;
TextView verliehenesGeld;
float _betrag = 0;
float gesamt = 0;
int iconID;
EditText eintrag_name, eintrag_betrag, eintrag_grund;
String name = "";
String grund = "";
String datum = "";
MainActivity mainActivity = (MainActivity)getActivity();
GetDate date;
public Cursor cursor;
public Schuldet_mir(int position) {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_schuldet_mir, container, false);
listView = (ListView)v.findViewById(R.id.listView1);
verliehenesGeld = (TextView)v.findViewById(R.id.textView1);
verliehenesGeld.setText(null);
return v;
// --------------------------------------------------------------------
public void btnClick_SchulderEintragen(View v) { //Button Klick to Add Schuldner
openDialog();
// --------------------------------------------------------------------
private void openDialog() { //Dialog erstellen
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog1);
dialog.setTitle("Schulden Hinzufügen");
dialog.setCancelable(false);
eintrag_name = (EditText)dialog.findViewById(R.id.editText_name);
eintrag_betrag = (EditText)dialog.findViewById(R.id.editText_betrag);
eintrag_grund = (EditText)dialog.findViewById(R.id.editText_grund);
Button hinzufügen = (Button)dialog.findViewById(R.id.button2);
Button abbrechen = (Button)dialog.findViewById(R.id.button1);
hinzufügen.setOnClickListener(new OnClickListener() { //Buttonklick Hinzufügen
@Override
public void onClick(View v) {
name = eintrag_name.getText().toString();
_betrag = Float.valueOf(eintrag_betrag.getText().toString());
grund = eintrag_grund.getText().toString();
datum = date.returnDate();
iconID = R.drawable.user_image3;
mainActivity.myDBHandler.insertRow(name, _betrag, grund, datum);
populateListViewFromDB();
dialog.dismiss();
// --------------------------------------------------------------------
abbrechen.setOnClickListener(new OnClickListener() { //Buttonklick Abbrechen
@Override
public void onClick(View v) {
dialog.dismiss();
dialog.show();
// --------------------------------------------------------------------
@SuppressWarnings("deprecation")
public void populateListViewFromDB() { //ListView mit Werten aus der db befüllen
// cursor = mainActivity.myDBHandler.getAllRows();
if(mainActivity == null) {
Log.e("***TEST***", "MAIN ist NULL");
mainActivity.startManagingCursor(cursor = mainActivity.myDBHandler.getAllRows());
String[] dbSpaltenNamen = new String[] {DataBaseHandler.KEY_NAME, //die spalteneinträge wie
DataBaseHandler.KEY_BETRAG, //angeordnet in die jeweiligen
// DataBaseHandler.KEY_GRUND, //id's des custom listzeile zuordnen
DataBaseHandler.KEY_DATUM};
int[] zuListViewIDs = new int[] {R.id.txt_name, //hier die id's. name zu name
R.id.txt_betrag, //betrag zu betrag...
R.id.datum};
SimpleCursorAdapter myCursorAdapter
= new SimpleCursorAdapter
(getActivity(), R.layout.list_zeile, cursor, dbSpaltenNamen, zuListViewIDs);
listView.setAdapter(myCursorAdapter);
private float gesamtSchulden(EditText betrag) {
gesamt += Float.valueOf(betrag.getText().toString());
return gesamt;
// --------------------------------------------------------------------
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mainActivity = (MainActivity) activity;
catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " Fehler: Keine MainActivity-Instanz!");
The SectionsPagerAdapter
:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Schuldet_mir f1;
private Ich_schulde f2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
@Override
public Fragment getItem(int position) {
//damit nicht immer wieder bei einer Anzeige ein neues fragment instanziiert wird,
//wurde diese Methode angepasst. D.h. es werden die vorher erzeugten framgente verwendet
if(position == 0) {
if (f1 == null) {
f1 = new Schuldet_mir(position);
return f1;
else {
if (f2 == null) {
f2 = new Ich_schulde(position);
return f2;
@Override
public int getCount() {
// Show 2 total pages.
return 2;
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "... schuldet mir";
case 1:
return "Ich schulde ...";
return null;
The NullPointerException
:
12-29 11:30:30.172: D/ActivityThread(26852): setTargetHeapUtilization:0.25
12-29 11:30:30.172: D/ActivityThread(26852): setTargetHeapIdealFree:8388608
12-29 11:30:30.172: D/ActivityThread(26852): setTargetHeapConcurrentStart:2097152
12-29 11:30:30.192: V/ActivityThread(26852): Class path: /data/app/debtmanager.mainfolder-2.apk, JNI path: /data/data/debtmanager.mainfolder/lib
12-29 11:30:30.262: E/***TEST***(26852): MAIN ist NULL
12-29 11:30:30.272: D/AndroidRuntime(26852): Shutting down VM
12-29 11:30:30.272: W/dalvikvm(26852): threadid=1: thread exiting with uncaught exception (group=0x410fb2a0)
12-29 11:30:30.272: E/AndroidRuntime(26852): FATAL EXCEPTION: main
12-29 11:30:30.272: E/AndroidRuntime(26852): java.lang.RuntimeException: Unable to resume activity {debtmanager.mainfolder/debtmanager.mainfolder.MainActivity}: java.lang.NullPointerException
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2608)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2636)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2103)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread.access$600(ActivityThread.java:138)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.os.Looper.loop(Looper.java:137)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread.main(ActivityThread.java:4872)
12-29 11:30:30.272: E/AndroidRuntime(26852): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 11:30:30.272: E/AndroidRuntime(26852): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 11:30:30.272: E/AndroidRuntime(26852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-29 11:30:30.272: E/AndroidRuntime(26852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-29 11:30:30.272: E/AndroidRuntime(26852): at dalvik.system.NativeStart.main(Native Method)
12-29 11:30:30.272: E/AndroidRuntime(26852): Caused by: java.lang.NullPointerException
12-29 11:30:30.272: E/AndroidRuntime(26852): at fragments.Schuldet_mir.populateListViewFromDB(Schuldet_mir.java:114)
12-29 11:30:30.272: E/AndroidRuntime(26852): at debtmanager.mainfolder.MainActivity.populateListViewFromDB(MainActivity.java:105)
12-29 11:30:30.272: E/AndroidRuntime(26852): at debtmanager.mainfolder.MainActivity.onResume(MainActivity.java:49)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1187)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.Activity.performResume(Activity.java:5265)
12-29 11:30:30.272: E/AndroidRuntime(26852): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2598)
12-29 11:30:30.272: E/AndroidRuntime(26852): ... 12 more
In the line 114 is following the code.
mainActivity.startManagingCursor(cursor = mainActivity.myDBHandler.getAllRows());
I don't know why MainActivity
is null
. Hopefully someone can give me a suggestion to solve this problem.
As others have pointed out, you are trying to reference the Activity
before the Fragment
is attached to the Activity
.
In saying that, however, you shouldn't try to call an Activity's
own method directly from a Fragment
in the way you are trying to do it. It can be done but it is bad practice for designing with Fragments
.
A Fragment
is meant to be modular and reusable - because of this a Fragment
shouldn't need to know what type of Activity
is hosting it or what methods might be available to call.
Instead you should define a callback interface in the Fragment
itself and have the Activity
implement that interface and the associated methods.
See the Event Callbacks guide for Fragments
.
Doing it this way means the Fragment
always 'knows' what methods have been defined by the interface and how to use them. As long as all Activities
that use the Fragment
implement the interface things should work.
The problem is caused by this line:
MainActivity mainActivity = (MainActivity)getActivity(); // in Schuldet_mir
This is called in the fragment's constructor. Fragment.getActivity()
has to be called after the Fragment
has been constructed. You should call getActivity()
in all the places that you use mainActivity
field. Also you should move the populateListViewFromDB()
from the activitie's onResume()
method to the fragment's onResume()
method.
You have another mistake. Your fragment has to have an empty constructor. You should have a look at the answers to this question.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.