views.setTextViewTextSize导致应用程序崩溃?

时间:2015-03-06 07:08:20

标签: android android-appwidget

我正在制作一个主屏幕小部件以及设置页面。用户可以从设置页面中选择小部件字体大小。我能够成功地将字体大小值发送到updateAppWidget方法,并在字符串变量“fontSize”中成功捕获。

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) 
{
        CharSequence widgetSettings = myWidgetConfigureActivity.loadSettings(context, appWidgetId, PREF_PREFIX_KEY_SETTINGS);

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);

        //read settings
        String fontSize = "";

        String[] splitTextSettings = widgetSettings.toString().split("%");
        if (splitTextSettings.length > 1) {
            fontSize = splitTextSettings[1].trim();            
        }

        views.setTextViewText(R.id.txtHeaderText, fontSize);


       // ###################  this works. ####################
       views.setTextViewTextSize(R.id.txtQuoteText, TypedValue.COMPLEX_UNIT_SP, Float.parseFloat("20"));

       // ###################  this does not work ###############
       views.setTextViewTextSize(R.id.txtQuoteText, TypedValue.COMPLEX_UNIT_SP, Float.parseFloat(fontSize));

        // More code here
        ....
        ....
        ....

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

问题是当我在setTextViewTextSize方法中传递这个fontSize变量时,它不起作用,我看到“小部件已停止工作”。但是,如果我在同一方法中对该值进行硬编码,则可以正常工作,但由于该值是硬编码的,因此它始终显示相同的文本大小。

我在这里做错了什么?

0 个答案:

没有答案