0

I have 2 classes one that will trigger a static method from the other

This is the first method from the first class (Trigger)

public void PopulateCards(){
    tab3.refresh3(datea,timea);
}

This is the second class the methods

public class Tab3 extends Fragment {


public static LinearLayout LL3, LLText2;
public static LinearLayout parent_linear;
public static Context contexted;
public static View v2;
public static ImageView history_icon;
public static TextView res_date, res_time;

String ida, user_ida, codea, datea, timea, dateca, timeca, statusa, statusa2, crsid, cpid;

int rows;

public static ArrayList<LinearLayout> linear_listeners = new ArrayList();
public static SharedPreferences prefs;
public static SharedPreferences.Editor editor;

static JSONObject json = null;

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

    contexted=this.getActivity();



    prefs = this.getActivity().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = prefs.edit();

    if (prefs.getString("flagnet", "Yes").equals("No")) {

        if (prefs.getInt("flagAres", 0) != 0) {
            String resJSON = prefs.getString("ResJSON", "");
            parent_linear = (LinearLayout) v2.findViewById(R.id.parent_linear3);


            try {
                json = new JSONObject(resJSON);


                String r = json.getString("rows");
                rows = Integer.parseInt(r);

                for (int x = 0; x < rows; x++) {
                    JSONObject json_user = json.getJSONObject("reservation" + x);


                    ida = json_user.getString("res_id").toString();
                    codea = json_user.getString("res_code").toString();
                    timea = json_user.getString("res_time").toString();
                    datea = json_user.getString("res_date").toString();
                    timeca = json_user.getString("res_timecreated").toString();
                    dateca = json_user.getString("res_datecreated").toString();
                    statusa = json_user.getString("res_status").toString();
                    statusa2 = json_user.getString("res_status2").toString();
                    user_ida = json_user.getString("user_id").toString();
                    crsid = json_user.getString("crs_id").toString();
                    cpid = json_user.getString("cp_id").toString();


                    //tab2.clear_views();
                    refresh3(datea, timea);

                }
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            } catch (Exception e) {
                e.printStackTrace();

            }

        } else {


            parent_linear = (LinearLayout) v2.findViewById(R.id.parent_linear3);

            refresh3("No Record", "");
        }
        //parent_linear = (LinearLayout) v2.findViewById(R.id.parent_linear3);

    }
    parent_linear = (LinearLayout) v2.findViewById(R.id.parent_linear3);

    return v2;
}



public static void create_layout2(String datea, String timea) {


    LL3 = new LinearLayout(contexted);



    LL3.setBackgroundResource(R.drawable.navbtns);
    LL3.setClickable(true);
    LL3.setPadding(20, 20, 20, 20);
    LinearLayout.LayoutParams LLParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LLParams2.setMargins(0, 15, 0, 0);
    LL3.setOrientation(LinearLayout.HORIZONTAL);
    LL3.setLayoutParams(LLParams2);
    LL3.setWeightSum(10);
    LL3.setGravity(Gravity.CENTER_VERTICAL);

    history_icon = new ImageView(contexted);
    history_icon.setImageResource(R.drawable.history_icon);
    LayoutParams car_pic_params = new LayoutParams(0, LayoutParams.FILL_PARENT, 2);
    history_icon.setLayoutParams(car_pic_params);
    history_icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);


    LLText2 = new LinearLayout(contexted);
    LLText2.setOrientation(LinearLayout.VERTICAL);
    LayoutParams LLTextParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 8);
    LLTextParams.setMargins(0, 0, 0, 0);
    LLText2.setLayoutParams(LLTextParams);


    res_date = new TextView(contexted);
    res_time = new TextView(contexted);

    LayoutParams TVParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


    res_date.setLayoutParams(TVParams);
    res_time.setLayoutParams(TVParams);

    res_date.setTextSize(20);
    res_time.setTextSize(14);

    res_date.setTextColor(Color.parseColor("#218ec6"));
    res_time.setTextColor(Color.parseColor("#84191919"));

    LLText2.addView(res_date);
    LLText2.addView(res_time);

    //res_date.setText("01-JAN-14");
    res_date.setText(datea);
    res_time.setText(timea);


    LL3.addView(LLText2);
    LL3.addView(history_icon);
    parent_linear.addView(LL3);


}



public static void clear_views2() {
    parent_linear = (LinearLayout) v2.findViewById(R.id.parent_linear3);
    parent_linear.removeAllViews();
}

public static void refresh3(String datea, String timea) {


    create_layout2(datea, timea);

}
}

This Tab3 is a fragment used for a Sliding Tab please help i'm so confused. I used this same kind of method from the first and second tab but here it cannot create these views.

09-21 11:30:43.069    2851-2851/com.pick E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
        at android.view.View.<init>(View.java:3454)
        at android.view.ViewGroup.<init>(ViewGroup.java:464)
        at android.widget.LinearLayout.<init>(LinearLayout.java:168)
        at com.pick.Tab3.create_layout2(Tab3.java:123)
        at com.pick.Tab3.refresh3(Tab3.java:191)
        at com.pick.ProfileFragment.processFinish(ProfileFragment.java:323)
        at com.pick.functions.Connect.onPostExecute(Connect.java:108)
        at com.pick.functions.Connect.onPostExecute(Connect.java:30)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
        at dalvik.system.NativeStart.main(Native Method)

The logcat says that I am not initializing the dynamically created linearlayout please help

1 Answers1

0

Check whether contexted is null just before creating the LinearLayout. You say you have two classes, so maybe you're trying to call the method before the fragment has even been created. In addition, if you save Activities as global variables in classes / fragments, then since there is a reference to these Activities from your classes, their resources may not be released when they're no longer needed, which may cause memory issues.

I usually save a reference to the currently running activity in the Application file of my project (this file is created before any other Activity is created and can be used to share data and utility methods throughout the application). The idea is to have a variable of type Activity saved in your Application file and each time an onCreate() or onResume() method is called, override this variable with the Activity whose onCreate / onResume is called. Then whenever you wish to call a static method in which you need a reference to a Context or Activity, you can pass in the variable from the Application class ensuring the activity is alive at the time it's used. This way, I have only a single Activity stored at any given time throughout the entire application. Remember to release this variable whenever the last Activity of your application in the stack is destroyed (when you exit the application and all Activities should be destroyed).

logcat
  • 283
  • 2
  • 12