FirstFragment扩展了Fragment,我知道setContentView和findViewById需要从appcompactactivity扩展。 但我不知道使用接口来扩展它们。有没有使用setContentView或findViewById的解决方案 (此代码是FirstFragment java文件,我添加了FloatingActionButton) 这是我的代码
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);
}
});
}
}
答案 0 :(得分:0)
删除upper import import android.app.Fragment;并使用以下代码
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;
}