안녕하세요, 조각 기본 레이아웃에 콘텐츠를 만들기위한 과제가 주어졌습니다. 그러나 탭 작업 표시 줄로 스 와이프 활동을 작성하면 각 탭 내에서 조각 레이아웃을 편집 할 수 없습니다.프래그먼트 작업의 뷰와 상호 작용할 수 없습니다.
MainActivity.java : 여기에 다음과 같은 코드입니다 방법은 내가 다음 BlankFragment.java을 만들어
는 (액션 탭 기본 슬쩍 활동을 만든) 조각 레이아웃
package com.example.clement.testingfragments;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// action bar
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//manage the fractions
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new BlankFragment());
fragmentTransaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
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;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
return rootView;
}else {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
를 참조하는 데 사용 : (단편 기본 레이아웃의 기본 작성)
public class BlankFragment extends Fragment {
TextView mtv;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("hello","hello");
View viewRoot = inflater.inflate(R.layout.fragment_blank, container, false);
mtv = (TextView)viewRoot.findViewById(R.id.testy);
mtv.setText("hello");
return viewRoot;
}
}
이 가진 문제 내가 fragment_blank.xml의 내부 텍스트 뷰를 변경할 수없는 것으로 생각된다. 나는 인터넷을 통해 모든 연구를했고 주어진 제안은 getActivity(), onViewCreated(), Onstart()를 사용하여 void 메쏘드를 만드는 것이었다. 이 모든 것들은 효과가없는 것 같습니다.
메서드가 작동하는 조건에서 null을 반환하는 if else 루프가 포함되어 있지만 여전히 null을 반환하지 않고 실행되며이 문제가 발생하는 이유에 대한 단서가 없습니다.
내 fragment_blank.xml :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.clement.testingfragments.BlankFragment">
<TextView
android:id="@+id/testy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
더 자세한 정보가 필요한 경우 알려 마십시오. 진심으로 당신의 제안과 조언을 구합니다. 미리 감사드립니다!
내가 쓴 미안 빌어 먹을 오 질문에 대한 잘못된 XML 파일을 아래로. 그것은 편집되었습니다. 다시 읽으십시오. –
xml을 게시하고 질문에서 불필요한 코드를 제거하십시오. –
괜찮습니까? 여기 –