-1

I'm trying to modify my app which works on fragments. I'm trying to add on one fragment, let's call it tab_1, one view from other application. This is a weather app, which downloads data after user click on a button, therefore I'm using listiner which causes the problem

E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.chirag.slidingtabsusingviewpager, PID: 4586
                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                     at com.example.chirag.slidingtabsusingviewpager.Tab1.onCreate(Tab1.java:100)
                                                     at android.support.v4.app.Fragment.performCreate(Fragment.java:2226)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)

Here's the code of tab_1.class

public class Tab1 extends Fragment implements  View.OnClickListener  {


    private OnFragmentInteractionListener mListener;

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
    }

    public Tab1() {
        // Required empty public constructor
    }

    public static Tab1 newInstance(String param1, String param2) {
        Tab1 fragment = new Tab1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        getActivity().setContentView(R.layout.activity_main);
        sharedPreferences = getActivity().getSharedPreferences(PREFERENCES_NAME, MainActivity.MODE_PRIVATE);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isOnline()) {
                    textViewTemperature.setText(restoreData("TEMP"));
                    Toast.makeText(getActivity().getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
                } else {
                    Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                weather = parser.getWeatherForLocation(editText.getText() + "");
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        editText.setText(weather.getCity() + ",  " + weather.getCountry());
                                        textViewTemperature.setText("Temperature: " + weather.getTemperature());
                                        saveData(textViewTemperature.getText() + "", "TEMP");
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.start();
                }
            }
        });
        sharedPreferences = getActivity().getSharedPreferences("file_name", getActivity().MODE_PRIVATE);
        buttonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
                System.exit(0);
            }
        });
        buttonAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity().getApplicationContext(), AboutActivity.class));
            }
        });
    }
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab1, container, false);
    }

    @Override
    public void onClick(View v) {

    }

Is there any way to get around this problem? App is constantly crashing.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
PiiiTeeeR
  • 45
  • 3

1 Answers1

0

Seems like you are not binding your view references object via default findViewById(int id) Activity's method. Thats why you getting NPE's.

Also if you just didn't include view reference binding block in your code, there is a possibility that you trying to bind view with un-existing id or view from a different layout.

So try double check your layout markup and binding view references code, it might help you.

dniHze
  • 2,162
  • 16
  • 22