1

I am new to Android development.
To create a tabbed Activity, I created three Java classes: 1) About; 2) Search; 3) List;

In my layout for Search I want to have a search bar, so I created an EditText (by drag and drop) and also a Button (by drag and drop).
In order to make my tabs work, in my Search.java I have the following code:

public class Search extends Fragment{


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

        View rootView = inflater.inflate(R.layout.search, container, false);

        return rootView;
    }

    }

Now in my Search.java class I also want to start working on code for my search bar, but in order to do that I think I need a constructor that starts like this like in a main activity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

But when I write code for it, it gives me these errors:

1) on protected: 'onCreate(Bundle) in 'com.example.alex.project.Search' crashes with 'onCreate(Bundle)' in 'android.support.v4.app.Fragment; attempting to assign weaker access privileges ('protected'); was 'public'
2)on setContentView: cannot resolve method 'setContentView(int)'

What can I do or what do I need to change to start writing Java code for search bar in 'onCreate(Bundle)' method?
Or do I need to do it somewhere else?

I would really appreciate any help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Alex B.
  • 47
  • 7

3 Answers3

1

You have the method signature wrong. You can't weaken the access privilege of an overridden method. Use

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

You can access your EditText in OnViewCreated:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // Parameter view is the inflated view you passed in onCreateView

    // Replace with your id
    EditText myEditText = (EditText) view.findViewById(R.id.edit1);
}
Community
  • 1
  • 1
Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40
  • Thanks for the reply. I have tried that, then when I put code inside it, for example, when I try using stuff like `sTxt = (EditText) findViewById(R.id.sTxt);` (sTxt being ID of editText area from search.xml(by drag and drop)), on findViewById() it gives me this error : cannot resolve method 'findViewById(int)' – Alex B. Apr 08 '17 at 19:56
  • `setContentView`, `findViewById`, etc are methods of `Activity`. Your class is a `Fragment`. – Veneet Reddy Apr 08 '17 at 19:58
  • Do I need to create `Activity` class for Search then? – Alex B. Apr 08 '17 at 20:02
  • How is the method signature wrong?? `onCreateView` is fine for Fragments. You can also use `rootView.findViewById` there – OneCricketeer Apr 08 '17 at 20:19
  • Sorry, I didn't mention, I also need a button in there because it is part of a search bar I want to create. Now it tells me that button cannot be resolved. Also, I want user to not be able to press the button(search) if there is nothing in the search bar. So I also want to use `sTxt.addTextChangedListener(new TextWatcher() {` and it tells me that TextWatcher cannot be resolved, also the same when I try to `myBtn.setEnabled` – Alex B. Apr 08 '17 at 20:22
0

Regarding second issue, looks like you haven't defined search layout. You need to have a layout search.xml in your res/layout folder: read more here: What is R.layout.search?

If you have it - than you should call setContentView method in your MainActivity class that extends *Activity, since Fragment class doen't have such method.

Community
  • 1
  • 1
Ivan Pronin
  • 1,768
  • 16
  • 14
  • Thanks for the reply. Sorry, if I wasn't clear, but I do have `search.xml`, if that was what you meant. – Alex B. Apr 08 '17 at 19:59
  • Is the 2) issue still appears after fixing the 1) one? – Ivan Pronin Apr 08 '17 at 20:02
  • Haven't managed to fix anything yet. If you mean `search.xml` I always had it. If I try changing `protected` to `public` onCreate(bundle), it doesn't give me the 1 error anyone, but 2 still remains. – Alex B. Apr 08 '17 at 20:07
  • I tried adding code(regarding search bar(edit text, buttons etc)) to my main activity, but then when I run it, emulator is not able to open the app, it says that it keep stopping and then that it cannot be opened. – Alex B. Apr 08 '17 at 20:25
  • You need to extend some `Activity` class with you `Search` , however. Refer to this tutorial: https://developer.android.com/guide/topics/search/search-dialog.html – Ivan Pronin Apr 08 '17 at 20:31
  • I can't really extend my Search.java as it is already extended by Fragment. – Alex B. Apr 08 '17 at 20:47
0

I think I need a constructor that starts like this

That's not a constructor. Everything you asked about are methods.

on protected: 'onCreate(Bundle) in 'com.example.alex.project.Search' clashes with 'onCreate(Bundle)' in 'android.support.v4.app.Fragment

Not sure about that, but all methods you mentioned should generally be public, not protected.

You should also un-comment @Override.

What can I do/what do I need to change to start writing java code for search bar

You need to do something like this with what you have.

View rootView = ...

SearchBar search = (SearchBar) rootView.findViewById(R.id.search);
// edit searchbar

return rootView;

onCreate for an Activity is like onCreateView, and setContentView is essentially what return rootView does for a Fragment.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks for your reply. Write that where? Sorry, I don't really understand. Where does SearchBar come from? I have editText field and a button in my search.xml that I thought I should turn into a search bar, and I want to write code for it, for example, that button should be disabled until user puts text into text field part of my search bar. – Alex B. Apr 08 '17 at 20:46