我在尝试修复空指针错误时遇到了一些麻烦 我试图在我的应用程序中从片段实现Zxing扫描仪,但是我收到以下错误。
错误
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: FATAL EXCEPTION: main
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: Process: app.rory.menu, PID: 4123
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.startCamera()' on a null object reference
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at app.rory.pocket_chef.Fragments.scan_Foods_Fragment.onCreateView(scan_Foods_Fragment.java:35)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.Fragment.performCreateView(Fragment.java:2220)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:793)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-20 19:17:39.249 4123-4123/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-20 19:17:39.274 782-2968/? W/ActivityManager: Force finishing activity app.rory.menu/app.rory.pocket_chef.MyActivity
我遵循了这个简单的教程(http://d-codepages.com/barcode-scanner-android-example/),但遇到了空指针错误 如果我从片段中发布,我应该采取不同的方法吗?
从我尝试启动扫描程序的位置
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import javax.xml.transform.Result;
import app.rory.slidemenu.R;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
/**
* Created by Rory on 10/22/2014.
*/
public class scan_Foods_Fragment extends Fragment implements ZXingScannerView.ResultHandler{
private ZXingScannerView scannerView;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootview = inflater.inflate(R.layout.scan_foods_layout, container, false);
//return rootview;
scannerView = (ZXingScannerView) container.findViewById(R.id.scanner_view);
scannerView.startCamera(); //error on this line
scannerView.setResultHandler(this);
return rootview;
}
@Override
public void handleResult(com.google.zxing.Result result) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(result.getBarcodeFormat().toString());
alert.setMessage(result.getText());
alert.show();
}
}
片段的XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scanner_view"
/>
<!--<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/scanFoods"
android:id="@+id/textView" />-->
</RelativeLayout>
答案 0 :(得分:3)
这里有一个错误:
scannerView = (ZXingScannerView) container.findViewById(R.id.scanner_view);
一定是
scannerView = (ZXingScannerView) rootView.findViewById(R.id.scanner_view);