在此发布之前,我已经在stackoverlfow上查看了其他相关帖子,但似乎没有什么能像我一样解决类似的问题。
这是我得到的错误:
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
这是我应该生成QR码的QRGenerator代码,但它会抛出一个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);
}
答案 0 :(得分:0)
你正在推出
ImageView view = (ImageView) findViewById(R.id.qr);
前
setContentView(R.layout.display_qr);
这不起作用,因为findViewById()
在视图层次结构中搜索具有指定ID的视图,并且您还没有在该点添加任何视图。因此,它将返回null。这可能是导致事故的原因。
您应该在setContentView()
之后立即移动super.onCreate()
来电。