我正在尝试从应用内增加特定textview的大小。我想通过菜单项选择来做,但我遇到了问题。我尝试过以下方法:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Create an options menu for the activity
super.onCreateOptionsMenu( menu );
incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
incrseTxtMenu.setIcon( R.drawable.ic_menu_plus );
incrseTxtMenu.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
// handler.sendMessage( handler.obtainMessage() );
TextView tempTxt = getTextView();
tempTxt.setTextSize( 25 );
return true;
}
});
return true;
}
但这是抛出一个空指针异常。我也试过使用intro.setTextSize()但它会抛出相同的错误。如何从此菜单项中访问textview?
**更新
//Method used to fetch the textview
public TextView getTextView()
{
return intro;
}
来自log cat的错误:
AndroidRuntime FATAL EXCEPTION: main
java.lang.NullPointerException
at android.omni.Artist_activity$1.handleMessage( Artist_activity.java:32 )
另外顺便说一句 - 我正在尝试使用处理程序来更新GUI - 我认为这是正确的吗?
**更新2 XML CODE
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/tab_one_top_level"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation = "vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id = "@+id/faq_Intro"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/faq_Intro"
android:typeface = "monospace"
android:textStyle = "bold"
android:paddingBottom = "12dp"
/>
<TextView
android:id = "@+id/faq_Intro_Info"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/faq_Intro_Info"
android:textSize = "10dp"
android:typeface = "monospace"
android:textStyle = "bold"
/>
</LinearLayout>
</ScrollView>
有什么想法吗?
我的代码解决方案
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Create an options menu for the activity
super.onCreateOptionsMenu( menu );
incrseTxtMenu = menu.add( 0,1,0,"Increase Text Size" );
incrseTxtMenu.setIcon( R.drawable.ic_menu_plus );
decrseTxtMenu = menu.add( 0,2,0,"Decrease Text Size" );
decrseTxtMenu.setIcon( R.drawable.ic_menu_negate );
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Increase size menu item
if( item.getItemId() == 1 )
{
intro.setTextSize( myIntroSize += 5 );
introInfo.setTextSize( myIntroInfoSize += 5 );
}
// Derease size menu item
else if( item.getItemId() == 2 )
{
intro.setTextSize( myIntroSize -= 5 );
introInfo.setTextSize( myIntroInfoSize -= 5 );
}
return true;
}
onCreate()方法只是像以前一样初始化textview。哦,myIntroSize和myIntroInfoSize的值可以是你想要的任何值。
答案 0 :(得分:3)
请尝试以下代码并告诉我它是否有效。我不知道出了什么问题,只是基于几次预感 -
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Create an options menu for the activity
super.onCreateOptionsMenu( menu );
incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
incrseTxtMenu.setIcon( R.drawable.ic_menu_plus );
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 4){
TextView tempTxt = (TextView)findViewById( R.id.faq_intro);
tempTxt.setTextSize( 25 );
}
}
答案 1 :(得分:2)
这段代码与我合作正常,试一试。我猜你在如何获得TextView本身的问题。 “你应该使用findViewbyId(int id)
”
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Create an options menu for the activity
super.onCreateOptionsMenu( menu );
MenuItem incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
incrseTxtMenu.setIcon( android.R.drawable.btn_plus );
incrseTxtMenu.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
TextView tempTxt = (TextView) findViewById(R.id.text);
tempTxt.setTextSize( 25 );
return true;
}
});
return true;
}
使用此功能处理菜单选项也更好:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
不使用setOnMenuItemClickListener
编辑:我没有使用任何处理程序
答案 2 :(得分:1)
我相信你的问题是当你调用“getTextView();”
时TextView tempTxt = getTextView();
tempTxt.setTextSize( 25 );
此外,当您设置文字的大小时,您应该使用它来确保它们是像素:
tempTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);
在这里阅读: TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for different screens Android TextView setTextSize incorrectly increases text size
最好是使用“findViewById”创建的对象具有XML中的ID:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Create an options menu for the activity
super.onCreateOptionsMenu( menu );
MenuItem incrseTxtMenu = menu.add( 0,4,0,"Increase Text Size" );
incrseTxtMenu.setIcon( android.R.drawable.btn_plus );
incrseTxtMenu.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
TextView text = (TextView) findViewById(R.id.faq_Intro); //faq_Intro from XML
tempTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25); //corrected
return true;
}
});
return true;
}
而不是使用“OnMenuItemCLickListener”,您可以轻松地将“onOptionsItemSelected”与案例开关一起使用:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.increasesize:
//some code
break;
case R.id.exit:
finish();
break;
default:
return true;
}