-6

为什么eclipse不让我编译这个。我想让自己点击一个按钮,然后Edittext数据转到Textview. 然后从那里我希望textView通过按另一个将其转换为条形码Button,所以我总共有 2Buttons和 2textViews和 1editTexts

请告诉如何做到这一点或我哪里错了

public class cc extends Activity {

EditText inputc;
TextView outputc;
TextView tv1;
ImageView iv;
Button b;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cc);

inputc = (EditText) findViewById(R.id.ccinput);
outputc = (TextView) findViewById(R.id.outputc);
Button send = (Button) findViewById(R.id.button);
final TextView tv1 = (TextView) findViewById(R.id.outputc);  
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.outputc);         
tv.setTypeface(barcodefont);

send.setOnClickListener (new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        tv1.buildDrawingCache(); 
        iv.setImageBitmap(tv1.getDrawingCache()); 
        outputc.setText((inputc.getText()));
    }
});

}
}

附加信息。我已经编译了,但现在我得到了错误,这里是 logcat:

10-16 15:41:11.095: E/AndroidRuntime(5822): FATAL EXCEPTION: main
10-16 15:41:11.095: E/AndroidRuntime(5822): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.barcodedisplayer/com.example.barcodedisplayer.cc}: java.lang.RuntimeException: native typeface cannot be made
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.os.Looper.loop(Looper.java:137)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.ActivityThread.main(ActivityThread.java:5303)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at java.lang.reflect.Method.invoke(Method.java:525)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at dalvik.system.NativeStart.main(Native Method)
10-16 15:41:11.095: E/AndroidRuntime(5822): Caused by: java.lang.RuntimeException: native typeface cannot be made
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.graphics.Typeface.<init>(Typeface.java:175)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.graphics.Typeface.createFromAsset(Typeface.java:149)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at com.example.barcodedisplayer.cc.onCreate(cc.java:32)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.Activity.performCreate(Activity.java:5207)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-16 15:41:11.095: E/AndroidRuntime(5822):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
10-16 15:41:11.095: E/AndroidRuntime(5822):     ... 11 more
4

3 回答 3

2

您的 logcat 中的错误指向这一行:

Typeface barcodefont = Typeface.createFromAsset(getAssets(), "fonts/IDAutomationHC39M_FREE.otf");

确保fonts/IDAutomationHC39M_FREE.otf存在,它没有损坏,并且名称大小写完全匹配。

或者,如果上述方法不起作用,请尝试此处的建议:Android 中的自定义字体

于 2013-10-16T14:32:43.170 回答
1

您的 ImageViewiv未初始化。你会得到NullPointerException. 初始化 iv

 iv = (ImageView) findViewById(R.id.yourimageviewid);

这是错误之一。正如@NickT 指出的那样,这将是一个运行时错误。

你有 textview outputc 和 tv1 都用相同的 id 初始化R.id.outputc

  outputc = (TextView) findViewById(R.id.outputc); 
  final TextView tv1 = (TextView) findViewById(R.id.outputc);  

你有

 TextView tv = (TextView) findViewById(outputc); 
 // remove this line  or use R.id.yourtextviewid
 // outputc is a textview 

确保您IDAutomationHC39M_FREE.otf在 /assets/fonts 下。

于 2013-10-16T14:15:58.117 回答
1

这里有一些问题:

你已经声明TextView tv1;了,所以你不必再做一次final TextView tv1 = (TextView) findViewById(R.id.outputc);

您还提到:

所以我总共有 2 个按钮和 2 个 textViews 和 1 个 editTexts

公共类 cc 扩展 Activity {

EditText inputc;
TextView outputc;
TextView tv1;
ImageView iv;
Button b;
Button send;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cc);

    inputc = (EditText) findViewById(R.id.ccinput);
    outputc = (TextView) findViewById(R.id.outputc);
    send = (Button) findViewById(R.id.button);
    tv1 = (TextView) findViewById(R.id.outputc);
    tv1.setDrawingCacheEnabled(true);

    //To change to code 39 barCode font
    Typeface barcodefont = Typeface.createFromAsset(getAssets(), 
                "fonts/IDAutomationHC39M_FREE.otf");


    outputc.setTypeface(barcodefont);

    send.setOnClickListener (new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv1.buildDrawingCache();
            iv.setImageBitmap(tv1.getDrawingCache());
            outputc.setText((inputc.getText()));
        }
    });
}
}
于 2013-10-16T14:29:58.057 回答