我有一个包含一个活动和三个片段的Android项目。所有片段上都有相同的视图项。它基于TabbedActivity示例项目。这将创建相同片段视图的多个实例。
所有编辑框都具有相同的ID: 机器人:ID =" @ + ID / editText2"
片段上有相同的按钮和相同的编辑框,按下按钮会启动网络活动,尝试更新同一片段的编辑框。
final EditText resultBox = (EditText) findViewById(R.id.editText2);
System.out.println("reponse: " + decodedStr);
try
{
runOnUiThread(new Runnable()
{
@Override
public void run() {
resultBox.setText(decodedStr);
}
});
}
但只有一个片段得到更新,有时甚至不是活动片段。当我执行findViewById(R.id.editText2);
时,这里到底发生了什么提前致谢!
编辑:添加了更多代码,结果是getText函数也获取了错误的数据:
public class MainActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
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);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_PAGE_TITLE = "page_title";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(String pageTitle) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putString(ARG_PAGE_TITLE, pageTitle);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
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().getString(ARG_PAGE_TITLE)));
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
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).
String pageTitle;
switch (position)
{
case 0:
pageTitle = "1";
break;
case 1:
pageTitle = "2";
break;
case 2:
pageTitle = "3";
break;
default:
pageTitle = "4";
break;
}
return PlaceholderFragment.newInstance(pageTitle);
}
@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;
}
}
/** Called when the user touches the button */
public void randomFunc(View view) {
try {
// Get the current Fragment in View using the ViewPager's currentItem
PlaceholderFragment fragment = (PlaceholderFragment) mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem());
// Get the current Fragment's View
View fragview = fragment.getView(); // Same view that onCreateView returns
System.out.println("fragView " + fragview); // NULL
EditText et = (EditText) fragview.findViewById(R.id.editText); // CRASH!!
...
}
catch (Exception e)
{
System.out.println("func failed" + e.toString());
}
}
}
和xml:
机器人:的onClick =" randomFunc"
答案 0 :(得分:0)
我假设您使用ViewPager,以便更新正确的片段:
Cannot start debugger. Gem 'ruby-debug-ide' isn't installed or its executable script 'rdebug-ide' doesn't exist.
祝你好运,快乐的编码!