1

The title of this question is vague because I don't really know what is happening. I hava list view inside a class called SummerJobFragment.java this list view has a onItemClicked() that is suppose to open and other fragment called SummerJobDetailsFragment.java. Below I've posted the code and the logcat screenshot.

SummerJobFragment.java

public class SummerJobsFragmnet extends Fragment {

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

// TODO: Rename and change types and number of parameters
public static Fragment getInstance() {
    Fragment fragment = new SummerJobsFragmnet();
    return fragment;
}

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

}
public void showMessage (String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final MainActivity activity = (MainActivity) getActivity();
    String [] places = activity.getAllPositionsNamesPhone().toArray(
            new String[activity.getAllPositionsNamesPhone().size()]);

    final ListView list = (ListView) getView().findViewById(R.id.joblistView);
    int  prgmImages=R.mipmap.ic_launcher;
    list.setAdapter(new CustomListAdapter(activity,places,prgmImages));
    // OnClick listner for the individual cells of the listView
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            SummerJobDetailsFragment.mMyAppsBundle.putInt("value", position);
            SummerJobDetailsFragment fragment = new SummerJobDetailsFragment();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.layout.displayjobs_detailed, fragment).commit();


        }
    });
}

SummerJobDetailsFragment.java

public class SummerJobDetailsFragment extends Fragment {

    DataBaseHelper summerJobDB;
    public static Bundle mMyAppsBundle = new Bundle();
    public int position = SummerJobDetailsFragment.mMyAppsBundle.getInt("value");


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

    public static Fragment getInstance() {
        Fragment fragment = new SummerJobsFragmnet();
        return fragment;
    }

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

    }
    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.displayjobs_detailed,container,false);
        super.onViewCreated(view, savedInstanceState);
        SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
        DataBaseHelper summerJobDB;
        summerJobDB = new DataBaseHelper(getActivity());

        Cursor res = summerJobDB.getAllData(position+1);
        EditText jobPlace = (EditText)view.findViewById(R.id.jobTitle);
        jobPlace.setText(res.getString(1));
        /*
        EditText jobPlace = (EditText)summer.getView().findViewById(R.id.jobTitle);
        jobPlace.setText(res.getString(1));
        EditText jobPosition = (EditText)summer.getView().findViewById(R.id.jobPlace);
        jobPlace.setText(res.getString(2));
        EditText starTime = (EditText)summer.getView().findViewById(R.id.jobStartingTime);
        jobPlace.setText(res.getString(3));
        EditText address = (EditText)summer.getView().findViewById(R.id.jobAddress);
        jobPlace.setText(res.getString(5));
        EditText phone = (EditText)summer.getView().findViewById(R.id.jobPhoneNum);
        jobPlace.setText(res.getString(6));
        EditText hours = (EditText)summer.getView().findViewById(R.id.jobHours);
        jobPlace.setText(res.getString(4));
        */
        return 

    }

}

and here is the displayjobs_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/LinearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/jobplace"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobPlace"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Job Title"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobTitle"
        android:layout_gravity="right" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Job Address:"
        android:id="@+id/jobAddress" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobAddress" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Phone number:"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobPhoneNum"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Hours"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobHours" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Starting Time"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobStartingTime" />

</LinearLayout>

and here is the logcat: Second logcat error

enter image description here

dadadodo
  • 349
  • 2
  • 15
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Saurabh Vardani May 03 '16 at 05:32

3 Answers3

0

Initialize your view items in onCreateView of the Fragment not in onCreate

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);
    // Initialize Here
    return view;
}
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
0

First of all you are doing the layout operations in onCreate, you should do it in onCreateView() like this :

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

View rootView = inflater.inflate(R.layout.fragment_songs, container, false);


EditText jobPlace = (EditText)rootView.findViewById(R.id.jobTitle);
        jobPlace.setText(res.getString(1));

...
...
}

Also remove this statement

SummerJobDetailsFragment summer = new SummerJobDetailsFragment();

it doesn't make any sense, you call findViewById on the rootView.

No, this is how you do it,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/displayjobs_detailed"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/LinearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/jobplace"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobPlace"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Job Title"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobTitle"
        android:layout_gravity="right" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Job Address:"
        android:id="@+id/jobAddress" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobAddress" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Phone number:"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobPhoneNum"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Hours"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobHours" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Starting Time"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/jobStartingTime" />

</LinearLayout>
</FrameLayout>

Then in your fragment use this to commit

fragmentManager.beginTransaction().replace(R.id.displayjobs_detailed, fragment).commit();  // Note R.id instead of R.layout
varunkr
  • 5,364
  • 11
  • 50
  • 99
0

You are calling wrong. Difference between onCreateView and onViewCreated in Fragment

 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

Instead of, call

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);
    // Initialize Here
    return view;
}
Community
  • 1
  • 1
mubeen
  • 813
  • 2
  • 18
  • 39
  • I tried what you said i still get the same error. The second error logcat that I posted – dadadodo May 03 '16 at 06:16
  • Because there is no view as displayjobs_detailed, since you have layout named displayjobs_detail – mubeen May 03 '16 at 06:24
  • I changed that still the same! – dadadodo May 03 '16 at 06:29
  • If you tried all as @varunkr answer. I think your question is changed a lot and the code above is little out dated. The reason no view is found is simple of course that there is no view. If you had check every things is good. I think try clean and rebuild project. – mubeen May 03 '16 at 06:39
  • I updated the code with the new SummerJobDetailsFragment – dadadodo May 03 '16 at 06:45
  • What is your layout xml file name? Is your layout xml file name same as R.layout.displayjobs_detailed? – mubeen May 03 '16 at 07:12