我正在尝试从应用程序中增加特定文本视图的大小。我想通过菜单项选择来做到这一点,但遇到了问题。我尝试了以下方法:
@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() 但它会引发同样的错误。如何从此菜单项中访问文本视图?
**更新
//Method used to fetch the textview
public TextView getTextView()
{
return intro;
}
来自日志猫的错误:
AndroidRuntime FATAL EXCEPTION: main
java.lang.NullPointerException
at android.omni.Artist_activity$1.handleMessage( Artist_activity.java:32 )
另外顺便说一句-我正在尝试使用处理程序来更新GUI-假设这是必要的,我是否正确?
**更新 2 XML 代码
<?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() 方法像以前一样简单地初始化文本视图。哦,myIntroSize 和 myIntroInfoSize 的值可以是你想要的任何值。