-2

FirstFragment extends Fragment and I know setContentView and findViewById needs to extend from appcompactactivity. But I don't know to use interface to extends both of them. Is there any solution to use setContentView or findViewById (This code is FirstFragment java file and I added FloatingActionButton) Here is my Code

import android.app.Fragment;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class FirstFragment extends android.support.v4.app.Fragment{
FloatingActionButton b1;

public FirstFragment() {
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (FloatingActionButton) findViewById(R.id.fab);
    b1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent intent1 = new Intent(getApplicationContext(),ShowMe_fab.class);
            startActivity(intent1);
        }
    });


}


}
  • 1
    its a fragment you extend not an activity. inflate a view in onCreateView and return the same. check the docs – Raghunandan May 19 '18 at 08:45

1 Answers1

0

Remove upper import import android.app.Fragment; and use following code

import android.support.v4.app.Fragment
public class FirstFragment extends Fragment{

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.example_fragment, container, false);

TextView textview = view.findViewById(R.id.Yourtextid);
    // Inflate the layout for this fragment
    return view;
}