-1

I'm trying to start a new Tab1City activity, as I click on a button from Tab1Discover, which is a fragment. I've try several combinaisons if parameters, looking on stackoverflow, but it keeps compiling and making the app crash at launch, with a :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

at Tab1Discover.onCreateView(Tab1Discover.java:32)

public class Tab1Discover extends Fragment {

@InjectView(R.id.buttonLille)
Button _loginButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    _loginButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // Start the Signup activity
            Intent intent = new Intent(getActivity(), Tab1City.class);
            startActivity(intent);
        }
    });


    return inflater.inflate(R.layout.tab_1_disc1_main, container, false);
}
}
Adrien Chapelet
  • 386
  • 4
  • 24

3 Answers3

2

Edit:

I've noticed (thanks to the comment) that you're using RoboGuice. After a quick search, I've come up that the reference will only be available in onViewCreated, so move your code there:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  return inflater.inflate(R.layout.tab_1_disc1_main, container, false);
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  _loginButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      // Start the Signup activity
      Intent intent = new Intent(getActivity(), Tab1City.class);
      startActivity(intent);
    }
  });
}
Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116
  • His `@InjectView (R.id.idhere) CLASS varname;` is perfectly valid. Shouldn't he still inflate the xml before using the variable if he uses `@InjectView`? If not, then it could simply be that `R.id.buttonLille` isn't existent. – showp1984 Oct 12 '15 at 20:14
  • @showp1984 thanks for the insight. I've updated the answer accordingly. – Simas Oct 12 '15 at 20:18
  • Android Studio says to me that types are incompatibles, as _loginButton is a button, and it found that R.id.buttonLille is a view – Adrien Chapelet Oct 12 '15 at 20:19
  • @AdrienChapelet you will need RoboGuice if you are going to use `@InjectView`. See: https://github.com/roboguice/roboguice/ – showp1984 Oct 12 '15 at 20:21
  • 1
    @AdrienChapelet if you're talking about my old answer, you need to cast it using `... = (Button) ...` – Simas Oct 12 '15 at 20:21
0

From your usage of '@InjectView', i'm guessing you're using ButterKnife 6.0.x.

You're missing the part of ButterKnife where you have to tell it WHEN to inject the views.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_1_disc1_main, container, false);

    ButterKnife.inject(this, rootView);

    _loginButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // Start the Signup activity
            Intent intent = new Intent(getActivity(), Tab1City.class);
            startActivity(intent);
        }
    });

    return rootView;
}

With this code, you're telling ButterKnife where to find the view so it can set up the bindings. From here on, you can simply add all the @InjectView you want, and it'll take care of the bindings automatically.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
0

Thanks a lot, that's now working :

public class Tab1Discover extends Fragment {

@InjectView(R.id.buttonLille)
Button _lilleButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_1_disc1_main, container, false);

    ButterKnife.inject(this, rootView);

    _lilleButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // Start the Signup activity
            Intent intent = new Intent(getActivity(), Tab1City.class);
            startActivity(intent);
        }
    });

    return rootView;
}
Adrien Chapelet
  • 386
  • 4
  • 24