我試圖從應用程序內增加特定文本視圖的大小。我想通過菜單項選擇做到這一點,但我有問題。我試過以下內容:在我的應用程序中增加文本大小的問題
@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()方法簡單地稱爲前初始化TextView的。哦,myIntroSize和myIntroInfoSize的值可以是任何你想要的。
是什麼getTextView()?你可以請你從Log Cat發佈錯誤信息嗎? – 2012-04-08 15:27:26
什麼是htese行TextView tempTxt = getTextView();在你的代碼 – 2012-04-08 15:28:10
這是一個只是返回一個textview的實驗 - 我已經更新了我的代碼 – Katana24 2012-04-08 15:37:22