空对象引用(android Lint)上的android.view.View.findViewById(int)

时间:2018-11-07 15:56:30

标签: android android-fragments android-view

我有问题。我在Android Studio中运行Lint,它修复了我的应用程序的某些部分,但此应用程序无法启动。它写道:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cewe.martin.cewetracking/com.cewe.martin.cewetracking.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)

我找不到问题出在哪里。我无法撤消,因为有一些更改的文件,所以我试图删除零件,这对我来说并不常见,但仍然无法启动。这是MainActivity的onCreate方法:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null) {
            Log.d("START","DO IFU");
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ScanFragment()).commit();
            Log.d("START","ZA GETSUPP");
            navigationView.setCheckedItem(R.id.nav_scan);
        }
    }

您可以看到Log.d。第一个显示“ DO IFU”,第二个显示“ no”,所以我认为它可能在ScanFragment()中。代码在这里:

public class ScanFragment extends Fragment {

    private final EditText rucneKod = (EditText) getView().findViewById(R.id.numberInput);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_scan, container, false);
        Button skenovaciTlacitko = view.findViewById(R.id.button2);
        skenovaciTlacitko.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zapnoutSkener();
            }
        });

        Button rucne = view.findViewById(R.id.button3);

        View.OnFocusChangeListener ofcListener = new MyFocusChangeListener();
        rucneKod.setOnFocusChangeListener(ofcListener);

        final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        rucne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rucneKod.getText().length() != 0) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    odeslat(rucneKod.getText().toString());
                } else {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    Snackbar snackbar = Snackbar.make(view, R.string.pozadovanKod, Snackbar.LENGTH_LONG);
                    View sbView = snackbar.getView();
                    TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
                    textView.setTextColor(Color.YELLOW);
                    snackbar.show();
                }
            }
        });
        return view;
    }

有人可以看到有问题的代码吗?谢谢

3 个答案:

答案 0 :(得分:1)

private final EditText rucneKod = (EditText) 
getView().findViewById(R.id.numberInput); 

正在引起NPE。将其放在OnCreateView内,并用getView()替换view

答案 1 :(得分:1)

  

getView()返回由onCreateView(LayoutInflater返回的视图,   ViewGroup,捆绑销售))

这样做:private final EditText rucneKod = (EditText) getView().findViewById(R.id.numberInput);

它在onCreateView函数之前调用getView,这将是空的,这是正常的情况,

尝试在onCreateView函数内部移动实例化

希望这会有所帮助

答案 2 :(得分:0)

我认为问题出在这里

private final EditText rucneKod = (EditText) getView().findViewById(R.id.numberInput);

您应该在扩大片段的布局之后立即在onCreateView内调用findViewById。 getView()为null时,构造片段,并在调用onCreateView之前初始化存储的属性。