0

i´m working on a Tutorial right now and i´m creating a Android Drawer.

Java - Files:
- MainActivity - NavigationDrawerFragment - AboutMeFragment - ContactInfoFragment - MyCompanyFragment

XML- Files:
-activity_main -fragment_about_me -fragment_contact_info -fragment_my_company fragment_navigation_drawer - fragment_main

The three Files "AboutMeFragment, ContactInfoFragment, MyCompanyFragment contain alomost the same code.

package blabla.app1;

import android.content.Context;
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;


public class AboutMeFragment extends Fragment {

public static AboutMeFragment newInstance(){

    AboutMeFragment fragment = new AboutMeFragment();
    return fragment;
}


public AboutMeFragment(){}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    View rootView = inflator.inflate(R.layout.fragment_about_me, container, false);
    return rootView;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    ((MainActivity) activity).onSectionAttached(1);

}
}

In all i get the following errormessages: 1. "inflater": Cannot resolve symbol inflator 2. "activity": Cannot resolve symbol activity

Thanks

seb4222
  • 5
  • 4
  • share logcat............... – Muhammad Waleed Sep 22 '15 at 12:58
  • App1\app\src\main\java\blabla\app1\AboutMeFragment.java (same for ContactInfoFragment.java and MyCompanyFragment.java == `Error:(29, 25) error: cannot find symbol variable inflator Error:(37, 25) error: cannot find symbol variable activity`. Additional Error Message: `Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.` – seb4222 Sep 22 '15 at 13:52

1 Answers1

0

The first error is an easy fix, it's just a spelling mistake. Your method shows onCreateView(LayoutInflater inflater... but when you've used it within the method you've spelled it inflator, which doesn't exist, just change it to inflater.

The second is a bit more complicated, your tutorial will probably be using

public void onAttach(Activity activity)

but this is now deprecated and replaced with

public void onAttach(Context context)

This is why activity cannot be resolved, you don't have anything called 'activity'. The quickest way to fix this and still be mostly following along with your tutorial is probably to cast context to activity like this:

    @Override
public void onAttach(Context context) {
    super.onAttach(context);
    if(context instanceof Activity) {
        MainActivity activity = (MainActivity) context;
        activity.onSectionAttached(1);
    }
}
Lewis McGeary
  • 7,692
  • 2
  • 40
  • 46
  • Hey Lewis thanks for your help. I corrected the spelling mistake. I placed your code into my 3 Fragments. But new errors came up. Error:(38, 31) error: cannot find symbol class Activity. -- I have in my MainActivity.Java also a part with activity. ` @Override public void onAttach(Activity activity) { super.onAttach(activity); ((MainActivity) activity).onSectionAttached( getArguments().getInt(ARG_SECTION_NUMBER)); } – seb4222 Sep 22 '15 at 23:03
  • This may happen if you haven't added Activity to your imports at the top of each fragment. It should show `import android.app.Activity;` . – Lewis McGeary Sep 22 '15 at 23:16
  • That was it. You have been a great help Lewis. Thanks. – seb4222 Sep 24 '15 at 16:41
  • Excellent @seb4222! By the way, if you are using Android Studio, there is a setting you can turn on in the Preferences to do those import statements automatically. Search in the preferences for "auto import" and tick the boxes 'Optimize imports on the fly' and 'add unambiguous imports on the fly', Android studio will automatically add or remove the import statements for you. – Lewis McGeary Sep 24 '15 at 18:37