我有两个类,它将触发另一个
的静态方法这是第一个类(Trigger)的第一个方法
public void PopulateCards(){
tab3.refresh3(datea,timea);
}
这是第二类方法
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);
}
}
这个Tab3是用于滑动标签的片段,请帮助我这么困惑。我在第一个和第二个选项卡中使用了同样的方法,但是在这里它不能创建这些视图。
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)
logcat说我没有初始化动态创建的linearlayout请帮忙
答案 0 :(得分:0)
在创建LinearLayout之前检查contexted是否为null。你说你有两个类,所以也许你在创建片段之前尝试调用该方法。此外,如果将活动保存为类/片段中的全局变量,则由于您的类中存在对这些活动的引用,因此当不再需要它们时,它们的资源可能不会被释放,这可能会导致内存问题。
我通常在项目的Application文件中保存对当前运行的活动的引用(此文件在创建任何其他Activity之前创建,可用于在整个应用程序中共享数据和实用程序方法)。我们的想法是在Application文件中保存一个Activity类型的变量,并且每次调用onCreate()或onResume()方法时,使用调用onCreate / onResume的Activity覆盖此变量。然后,无论何时希望调用需要引用Context或Activity的静态方法,都可以从Application类传入变量,确保活动在使用时保持活动状态。这样,我在整个应用程序的任何给定时间只存储了一个Activity。每当应用程序在堆栈中的最后一个Activity被销毁时(退出应用程序并且所有活动都应该销毁)时,请记住释放此变量。