0
在發佈此之前,我已經檢查了其他相關帖子stackoverflow中,但似乎沒有真正解決我的類似問題。ZXing QR代碼生成器應用程序崩潰與NPE
這是我的錯誤:
06-29 21:02:38.274: E/AndroidRuntime(2261): FATAL EXCEPTION: main
06-29 21:02:38.274: E/AndroidRuntime(2261): Process: org.example.helloandroid, PID: 2261
06-29 21:02:38.274: E/AndroidRuntime(2261): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.helloandroid/org.example.helloandroid.QRGenActivity}: java.lang.NullPointerException
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.os.Handler.dispatchMessage(Handler.java:102)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.os.Looper.loop(Looper.java:136)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-29 21:02:38.274: E/AndroidRuntime(2261): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 21:02:38.274: E/AndroidRuntime(2261): at java.lang.reflect.Method.invoke(Method.java:515)
06-29 21:02:38.274: E/AndroidRuntime(2261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-29 21:02:38.274: E/AndroidRuntime(2261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-29 21:02:38.274: E/AndroidRuntime(2261): at dalvik.system.NativeStart.main(Native Method)
06-29 21:02:38.274: E/AndroidRuntime(2261): Caused by: java.lang.NullPointerException
06-29 21:02:38.274: E/AndroidRuntime(2261): at org.example.helloandroid.QRGenActivity.onCreate(QRGenActivity.java:81)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.Activity.performCreate(Activity.java:5231)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-29 21:02:38.274: E/AndroidRuntime(2261): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-29 21:02:38.274: E/AndroidRuntime(2261): ... 11 more
這是我的QRGenerator代碼應生成QR碼,但它拋出一個NPE。
public class QRGenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String emailaddress = intent.getStringExtra("emailValue");
String password = intent.getStringExtra("password");
ImageView view = (ImageView) findViewById(R.id.qr);
Log.d("EmailValue", emailaddress);
Log.d("password", password);
String qrInputText = emailaddress+"_"+password;
Log.v("QR TAG", qrInputText);
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
int height = point.y;
int smallerDimension = width < height ? width: height;
smallerDimension*=3/4;
com.google.zxing.Writer writer = new QRCodeWriter();
String finalData = Uri.encode(qrInputText,"utf-8");
try {
BitMatrix bm = writer.encode(finalData, BarcodeFormat.QR_CODE, 150, 150);
Bitmap ImageBitmap = Bitmap.createBitmap(150, 150, Config.ARGB_8888);
for(int i=0;i<150;i++){
for(int j=0;j<150;j++){
ImageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);
}
}
if(ImageBitmap!=null){
Log.d("QRCreated", "QR Is created "+R.id.qr);
view.setImageBitmap(ImageBitmap);
}
else{
Toast.makeText(getApplicationContext(), "Some error occurred in generating QRCode", Toast.LENGTH_LONG).show();
}
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Handler handler = new Handler();
setContentView(R.layout.display_qr);
getActionBar().setDisplayHomeAsUpEnabled(true);
}