-2

I have an activity and a fragment. The fragment's java file is currently empty. Below is the code for my activity

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class CrimeActivity extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crime);

}
}

When I try running my app, the error message is

Error:(15, 9) error: cannot find symbol method setContentView(int)

is I have tried searching for an answer but to no avail. I have cleaned my project and restarted Android Studio thrice. There are also no errors in the XML files.

user2628566
  • 11
  • 1
  • 4

3 Answers3

1

You are extending Fragment not Activity therefore you do not call setContentView as it does not exist for Fragment.

Please see previous Question for further information.

If you want to continue using fragments look at overriding the onCreateView() method.

Community
  • 1
  • 1
perkss
  • 1,037
  • 1
  • 11
  • 37
1

1-

public class CrimeActivity extends Activity{
  @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);

} 
}

2-

public class CrimeActivity extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     return inflater.inflate(R.layout.activity_crime, container, false);
 }

}
0

Select:

File---->invalidate caches/restart ---->invalidate and restart

By this android studio restarts and that error is gone..

Hope it helps