2012-04-08 33 views
0

我試圖從應用程序內增加特定文本視圖的大小。我想通過菜單項選擇做到這一點,但我有問題。我試過以下內容:在我的應用程序中增加文本大小的問題

@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的值可以是任何你想要的。

+0

是什麼getTextView()?你可以請你從Log Cat發佈錯誤信息嗎? – 2012-04-08 15:27:26

+0

什麼是htese行TextView tempTxt = getTextView();在你的代碼 – 2012-04-08 15:28:10

+0

這是一個只是返回一個textview的實驗 - 我已經更新了我的代碼 – Katana24 2012-04-08 15:37:22

回答

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); 
    } 

} 
+0

是的隊友,工作正常,但我會建議添加到setTextSize而不是隻有25的現有值。感謝幫助隊友:D – Katana24 2012-04-08 17:00:35

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

編輯:我沒有使用任何處理

+0

爲什麼不使用setOnMenuItemClickListener ?你能提供任何資源來驗證這一點嗎? – Katana24 2012-04-08 16:36:43

+1

我的參考資料:android文檔:http://developer.android.com/reference/android/view/MenuItem.html向下滾動到setOnMenuItemClickListener,你會看到「它更有效和更容易使用onOptionsItemSelected(MenuItem)」 – 2012-04-08 16:40:58

+0

Cool - 但我希望他們會給出一個理由。感謝隊友 – Katana24 2012-04-08 16:57:13

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; 
    } 
相關問題