2013-03-15 26 views
0

我得到我的应用程序来阅读一个应用程序,然后告诉我什么代码说,但现在我试图让它将字符串编码回条形码并显示为图像屏幕上。但是,它始终强制在应用程序启动之前关闭应用程序。这里是我的代码,请帮助:我的条形码应用程序强制关闭,只要它运行

import java.util.EnumMap; 
import java.util.Map; 

import android.os.Bundle; 
import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.drawable.BitmapDrawable; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.google.zxing.BarcodeFormat; 
import com.google.zxing.EncodeHintType; 
import com.google.zxing.MultiFormatWriter; 
import com.google.zxing.WriterException; 
import com.google.zxing.common.BitMatrix; 

public class Main extends Activity { 

IntentIntegrator integrator = new IntentIntegrator(this); 

private static final int WHITE = 0xFFFFFFFF; 
private static final int BLACK = 0xFF000000; 
LinearLayout myLayout = (LinearLayout)findViewById(R.id.myLayout); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //LinearLayout myLayout = (LinearLayout)findViewById(R.id.myLayout); 

    Button btn =(Button)findViewById(R.id.button1); 
    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      integrator.initiateScan(); 
     } 
    }); 
} 
public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
     if (scanResult != null) { 
      Toast.makeText(Main.this, 
        "works", 
        Toast.LENGTH_SHORT).show(); 
      String contents = intent.getStringExtra("SCAN_RESULT"); 
      String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 
      Toast.makeText(Main.this, 
        contents, 
        Toast.LENGTH_SHORT).show(); 
      Bitmap bitmap = null; 
      ImageView iv = new ImageView(this); 
      try { 

       bitmap = encodeAsBitmap(contents, BarcodeFormat.CODE_128, 600, 300); 
       iv.setImageBitmap(bitmap); 

      } catch (WriterException e) { 
       e.printStackTrace(); 
      } 
      myLayout.addView(iv); 
      TextView tv = new TextView(this); 
      tv.setGravity(Gravity.CENTER_HORIZONTAL); 
      tv.setText(contents); 

      myLayout.addView(tv); 
     } 


    } 
Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException { 
    String contentsToEncode = contents; 
    if (contentsToEncode == null) { 
     return null; 
    } 
    Map<EncodeHintType, Object> hints = null; 

     hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
     hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 

    MultiFormatWriter writer = new MultiFormatWriter(); 
    BitMatrix result; 
    try { 
     result = writer.encode(contentsToEncode, format, img_width, img_height, hints); 
    } catch (IllegalArgumentException iae) { 
     // Unsupported format 
     return null; 
    } 
    int width = result.getWidth(); 
    int height = result.getHeight(); 
    int[] pixels = new int[width * height]; 
    for (int y = 0; y < height; y++) { 
     int offset = y * width; 
     for (int x = 0; x < width; x++) { 
     pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; 
     } 
    } 
    Bitmap bitmap = Bitmap.createBitmap(width, height, 
      Bitmap.Config.ARGB_8888); 
     bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 
     return bitmap; 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

IntentResult和IntentIntegrator都来自ZXing。

登录猫:

03-15 13:05:04.303: E/AndroidRuntime(4363): FATAL EXCEPTION: main 
+1

你可以发布更多的logcat吗? – ianhanniballake 2013-03-15 17:12:50

+0

贝兹你正试图找到布局之前设置setContentView为活动。我不是为什么你在onCreate中评论布局线? – 2013-03-15 17:12:54

+0

然后它说集成商没有被使用,因为它唯一使用的时间在onCreate以外,在onActivityResult – Corey 2013-03-15 17:27:14

回答

0

尝试移动这样的:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.myLayout); 

下:

setContentView(R.layout.main); 
+0

那么在onActivityResult中使用myLayout时不会产生问题? – Corey 2013-03-15 17:24:44

+0

不,它不应该,onActivityResult会得到什么错误? – 2013-03-15 17:31:21

+0

我得到myLayout无法解析... – Corey 2013-03-15 17:33:45

0

在祝酒词,而不是Main.this你应该尝试getApplicationContext()。还记得ActivityContext的一种。

+0

多数民众赞成那不是问题,因为我尝试使条形码图像之前工作。 – Corey 2013-03-15 17:36:57