我是Android开发的初学者,我正在尝试开发一个程序,用户可以将文本中显示的文本转换为TextVIEw(TextVIEw正在显示我从资产导入的代码39条形码字体文本)到按位后的位图“转换为位图!”按钮.我试过搜索谷歌,但我只是设法得到答案,如将字符串转换为位图,没有指导在哪里键入代码,所以我很混淆.
我尝试使用我在谷歌搜索后尝试输入的代码来运行程序,但每次按下转换按钮时它都会崩溃.
真的希望你能帮忙!先感谢您! :d
以下是我目前的代码: –
*关于西蒙的代码编辑
在java:
public class MainActivity extends Activity implements OnClickListener { //Called when activity is first created TextVIEw tv1; ImageVIEw iv; button b; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); TextVIEw tv1 = (TextVIEw) findVIEwByID(R.ID.txtV); tv1.setDrawingCacheEnabled(true); //To change to code 39 barCode Font Typeface barcodeFont = Typeface.createFromAsset(getAssets(), "Fonts/IDautomationHC39M_FREE.otf"); TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.txtV); tv.setTypeface(barcodeFont); } public voID onClick(VIEw v) { tv1.buildDrawingCache(); iv.setimageBitmap(tv1.getDrawingCache()); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
Logcat错误:
10-04 06:33:25.076: E/AndroIDRuntime(1423): FATAL EXCEPTION: main10-04 06:33:25.076: E/AndroIDRuntime(1423): java.lang.IllegalStateException: Could not find a method ConvertText(VIEw) in the activity class com.example.txtvbitmapconverter.MainActivity for onClick handler on vIEw class androID.Widget.button with ID 'btnConvert'10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.vIEw.VIEw.onClick(VIEw.java:3578)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.vIEw.VIEw.performClick(VIEw.java:4084)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:16966)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.os.Handler.handleCallback(Handler.java:615)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.os.Handler.dispatchMessage(Handler.java:92)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.os.Looper.loop(Looper.java:137)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.app.ActivityThread.main(ActivityThread.java:4745)10-04 06:33:25.076: E/AndroIDRuntime(1423): at java.lang.reflect.Method.invokeNative(Native Method)10-04 06:33:25.076: E/AndroIDRuntime(1423): at java.lang.reflect.Method.invoke(Method.java:511)10-04 06:33:25.076: E/AndroIDRuntime(1423): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)10-04 06:33:25.076: E/AndroIDRuntime(1423): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-04 06:33:25.076: E/AndroIDRuntime(1423): at dalvik.system.NativeStart.main(Native Method)10-04 06:33:25.076: E/AndroIDRuntime(1423): Caused by: java.lang.NoSuchMethodException: ConvertText [class androID.vIEw.VIEw]10-04 06:33:25.076: E/AndroIDRuntime(1423): at java.lang.class.getConstructorOrMethod(Class.java:460)10-04 06:33:25.076: E/AndroIDRuntime(1423): at java.lang.class.getmethod(Class.java:915)10-04 06:33:25.076: E/AndroIDRuntime(1423): at androID.vIEw.VIEw.onClick(VIEw.java:3571)10-04 06:33:25.076: E/AndroIDRuntime(1423): ... 11 more
解决方法:
试试这个
添加到你的onCreate()
tv1.setDrawingCacheEnabled(true);
然后在你的onClick()
tv1.buildDrawingCache();iv.setimageBitmap(tv1.getDrawingCache());
http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean)
http://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean)
[编辑]
您现在遇到的问题是您在tv1存在之前尝试启用绘图缓存.
你应该做这个:
TextVIEw tv1 = (TextVIEw) findVIEwByID(R.ID.txtV); tv1.setDrawingCacheEnabled(true);
总结 以上是内存溢出为你收集整理的如何在Android中将TextView的文本转换为位图?全部内容,希望文章能够帮你解决如何在Android中将TextView的文本转换为位图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)