我是Android开发的新手,正在尝试制作一个应用,但是一旦我打开我的应用,它就会显示上述错误 我在其他问题上尝试了前面提到的方法 这是我的主要活动
public class FreeCal extends AppCompatActivity {
public ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
mViewPager.setCurrentItem(1);
}
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position) {
case 0:
fragment = new MenuFragment();
break;
case 1:
fragment = new CalculatorFragment();
break;
case 2:
fragment = new MoreFragment();
break;
default:
fragment = new CalculatorFragment();
}
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
**我认为这是我的片段,由于该片段而导致错误。我不知道该怎么办。由于此CalculatorFragment的onCreate方法而发生错误。我认为该错误与容器**
有关public class CalculatorFragment extends Fragment implements View.OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_calculator,container, false);
return view;
**Here is my onCreateMethod for the fragment**
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
final String string = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()).getString("theme", "0");
switch ( string ) {
case "0": {
getActivity().setContentView(R.layout.fragment_calculator);
break;
}
case "1": {
getActivity().setContentView(R.layout.fragment_calculator_orange);
break;
}
}
this.z = getActivity().getSharedPreferences("calc_history", 0);
this.A = getActivity().getSharedPreferences("calc_mode", 0);
this.B = this.A.getBoolean("mode", false);
if (this.B) {
this.J = this.getString(R.string.screen_scientific);
} else {
this.J = this.getString(R.string.screen_standard);
}
this.l = getActivity().findViewById(R.id.txt_hint);
this.m = getActivity().findViewById(R.id.txt_out);
if (bundle != null) {
this.r = bundle.getDouble("ans");
this.s = bundle.getDouble("mreg");
this.v = bundle.getString("current_token");
this.n = bundle.getChar("lastkey");
this.t = new LinkedList <String>(Arrays.asList(bundle.getStringArray("input_tokens")));
this.m.setText(this.c(this.b(true)));
}
final Display defaultDisplay = getActivity().getWindowManager().getDefaultDisplay();
final Point point = new Point();
defaultDisplay.getSize(point);
final int y = point.y;
this.o = y / 9;
if (this.B && this.getResources().getConfiguration().orientation == 1) {
this.o = y / 12;
这是Logcat。我已经尝试了先前问题中提供的方法
java.lang.IllegalArgumentException: No view found for id 0x7f09007b (com.kingslayer.freecal_allinonecalculator:id/container) for fragment CalculatorFragment{e145b35 #0 id=0x7f09007b android:switcher:2131296379:1}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
at
答案 0 :(得分:2)
检查您的setContentView(R.layout.activity_main);
和班级名称FreeCal
。
错误说找不到ID的视图。这意味着活动找不到布局文件。确保布局文件可用。