对不起,我的英语水平很低,我会简短地告诉别人了解我。 我有一个有viewpager的活动,这有几个膨胀布局。在布局上我有一个文本视图。 我想通过字符串(“something”)添加到textview textView.setText; 但是当我写setText热潮时!我的申请停止了。 有人有解决方案吗?
TextView tVNombreMen1;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_mostrar_menu);
// Creando textview y pasandoles variables
tVNombreMen1 = (TextView) findViewById(R.id.tVNombreMenu1);
tVNombreMen1.setText("aa");
MY CLASS:
public class Local_tipos_de_menuActivity extends ActionBarActivity {
private String[] opcionesMenu;
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
private CharSequence tituloSeccion;
private CharSequence tituloApp;
private ViewPager viewPager;
private LinearLayout page1;
private LinearLayout page2;
private LinearLayout page3;
private LinearLayout page4;
// variables titulo y descripcion menu
String tituloMenu1 = ("Menu de degustación:");
String descripcionMenu1 = ("Menu compuesto por los platos mas vendidos del
local.Debes elegir una bebida 2 entrantes, 2 segundos y un postre o cafe.");
double precioMenu1 = 10.90;
String tituloMenu2 = ("Menu selecto:");
String descripcionMenu2 = ("Menu compuesto por los platos preferidos del cheff.Debes
elegir una bebida 2 entrantes, 2 segundos y un postre o cafe.");
double precioMenu2 = 16.90;
TextView tVNombreMen1;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_mostrar_menu);
// Creando textview y pasandoles variables
tVNombreMen1 = (TextView) findViewById(R.id.tVNombreMenu1);
tVNombreMen1.setText("aa");
// TextView tVDescripcionMenu1 = (TextView)
findViewById(R.id.tVDescripcionMenu1);
// tVDescripcionMenu1.setText(descripcionMenu1);
// TextView tVPrecioMenu1 = (TextView)
findViewById(R.id.tVPrecioMenu1);
// tVPrecioMenu1.setText("€");
// TextView tVNombreMenu2 = (TextView) findViewById(R.id.tVNombreMenu2);
// tVNombreMenu2.setText(tituloMenu2);
// TextView tVDescripcionMenu2 = (TextView)
findViewById(R.id.tVDescripcionMenu2);
// tVDescripcionMenu2.setText(descripcionMenu2);
//DrawerLayout(menu lateral)
opcionesMenu = new String[] {"Mi perfil", "Opción 2", "Opción 3","4"};
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(
getSupportActionBar().getThemedContext(),
android.R.layout.simple_list_item_1, opcionesMenu));
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//android.support.v4.app.Fragment fragment = null;
switch (position) {
case 0:
//fragment = new Fragment1();
Intent a = new
Intent(Local_tipos_de_menuActivity.this, Local_tipos_de_menuActivity.class);
startActivity(a);
//abrir activity con una animacion personalizada
overridePendingTransition(R.anim.right_in,
R.anim.right_out);
break;
case 1:
//fragment = new Fragment2();
break;
case 2:
//fragment = new Fragment3();
break;
case 3:
break;
}
android.support.v4.app.FragmentManager fragmentManager =
getSupportFragmentManager();
fragmentManager.beginTransaction()
// .replace(R.id.content_frame, fragment)
.commit();
drawerList.setItemChecked(position, true);
tituloSeccion = opcionesMenu[position];
getSupportActionBar().setTitle(tituloSeccion);
drawerLayout.closeDrawer(drawerList);
}
});
tituloSeccion = getTitle();
tituloApp = getTitle();
drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.drawable.ic_navigation_drawer,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(tituloSeccion);
ActivityCompat.invalidateOptionsMenu(Local_tipos_de_menuActivity.this);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(tituloApp);
ActivityCompat.invalidateOptionsMenu(Local_tipos_de_menuActivity.this);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//viewpagerindicator viewpager
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MainPageAdapter());
TitlePageIndicator titleIndicator =
(TitlePageIndicator)findViewById(R.id.indicator);
//TabPageIndicator titleIndicator =
(TabPageIndicator)findViewById(R.id.indicator);
titleIndicator.setViewPager(viewPager);
titleIndicator.setOnPageChangeListener(new
OnPageChangeListener() {
@Override
public void onPageSelected(int position)
{
}
@Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels)
{
}
@Override
public void onPageScrollStateChanged(int state)
{
}
});
}
//ActionBar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId())
{
case R.id.action_settings:
Toast.makeText(this, "Settings",
Toast.LENGTH_SHORT).show();;
break;
case R.id.action_menu2:
startActivity(new Intent(this, Menu2Activity.class));
//abrir activity con una animacion personalizada
overridePendingTransition(R.anim.left_in, R.anim.left_out);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean menuAbierto = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_atras).setVisible(false);
if(menuAbierto)
menu.findItem(R.id.action_menu2).setVisible(false);
else
menu.findItem(R.id.action_menu2).setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
//Viewpager
class MainPageAdapter extends PagerAdapter
{
@Override
public CharSequence getPageTitle(int position)
{
String title = null;
switch (position)
{
case 0:
title = "Menu 1";
break;
case 1:
title = "Menu 2";
break;
case 2:
title = "Menu 3";
break;
default:
title = "Menu 4";
break;
}
return title;
}
@Override
public int getCount()
{
return 4;
}
@Override
public Object instantiateItem(ViewGroup collection, int position)
{
View page = null;
switch (position)
{
case 0:
if (page1 == null)
{
page1 = (LinearLayout)
LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_one_viewpager, null);
}
page = page1;
break;
case 1:
if (page2 == null)
{
page2 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_two_viewpager, null);
}
page = page2;
break;
case 2:
if (page3 == null)
{
page3 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_three_viewpager, null);
}
page = page3;
break;
default:
if (page4 == null)
{
page4 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_four_viewpager, null);
}
page = page4;
break;
}
((ViewPager) collection).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}
@Override
public void destroyItem(View collection, int position, Object view)
{
((ViewPager) collection).removeView((View) view);
}
}
我希望有人可以帮助我,因为我会发疯。我改变了textview setText的值,但是在viewpager中我是texview,我发现它是不可能的。虽然在这个世界上没有什么是不可能的,但是谢谢你。
答案 0 :(得分:0)
而不是在活动的onCreate中设置文本,而是在适配器的instantiateItem
中执行。您将获得空指针异常,因为您尝试在onCreate
中找到的视图尚未添加。因此,请尝试关注并告诉我您是否面临更多问题。
@Override
public Object instantiateItem(ViewGroup collection, int position)
{
View page = null;
switch (position)
{
case 0:
if (page1 == null)
{
page1 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_one_viewpager, null);
//Try below code
tVNombreMen1 = (TextView) page1.findViewById(R.id.tVNombreMenu1);
tVNombreMen1.setText("aa");
}
page = page1;
break;
case 1:
if (page2 == null)
{
page2 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_two_viewpager, null);
}
page = page2;
break;
case 2:
if (page3 == null)
{
page3 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_three_viewpager, null);
}
page = page3;
break;
default:
if (page4 == null)
{
page4 = (LinearLayout) LayoutInflater.from(Local_tipos_de_menuActivity.this).inflate(R.layout.page_four_viewpager, null);
}
page = page4;
break;
}
((ViewPager) collection).addView(page, 0);
return page;
}