錯誤訊息:
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
他是說我的setLayoutManager是空的 想了想
我的猜測是 "我的Re1沒有被宣告" 也就是我的initView沒有被執行!?
先說如果不是用從其他頁呼叫 是使用我的onLazyLoad看到畫面執行 這部分是正常的
呼叫:
Fragment1 fragment = new Fragment1();
fragment.change();
Fragment1
public class Fragment1 extends BaseLazyFragment {
private String url = "http://211.22.XXX.XXX/xampp/";
private RecyclerView Re1;
private int page = 1,run=0;
private String user_id,phone,cookie;
private TextView text1,text2,text3;
private Context context;
private boolean isFirstLoad = false;
private RecyclerView.LayoutManager mLayoutManager;
@Override
public void onLazyLoad() {
initData();
isFirstLoad =true;
}
有成功執行這個change
public void change(){
Log.e("change",toString());
initData();
run =1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
initView(view);
context = view.getContext();
if(isFirstLoad){
initData();
}
return view;
}
private void initView(View view){
Re1 = view.findViewById(R.id.Re1_tab1);
GlobalVariable Account = (GlobalVariable)getActivity().getApplicationContext();
user_id = Account.getUser_id();
phone = Account.getPhone();
cookie = Account.getCookie();
}
private void initData(){
//除錯用
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
Handler myHandler = new Handler();//時間處理器
myHandler.postDelayed(runTimerStop,400);//使Runnable 添加到Handler中,在指定的時間到時運行runTimerStop。
if(cookie != null) {
myHandler.removeCallbacks(runTimerStop);//removeCallbacks方法是删除指定的Runnable對象,使線程對象停止運行。
}
}
private Runnable runTimerStop = new Runnable() {
@Override
public void run() {
select(user_id);
// dialog.dismiss();
}
};
private void select(String id){
try {
String r = DBphp.DBstr(id,cookie,url);
JSONArray jsonArray = new JSONArray(r);
ArrayList<Rel_list> items = new ArrayList<Rel_list>();
迴圈這邊正常
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonData = jsonArray.getJSONObject(i);
items.add(new Rel_list(jsonData.getString("id"),jsonData.getString("product_name"),jsonData.getString("specification")
,jsonData.getString("selling_price"),jsonData.getString("operations_center"),jsonData.getString("inventory"),jsonData.getString("pictuture_id"),jsonData.getString("ANS")));
}
RelAdapter SA = new RelAdapter(getContext(),items);
** 有問題的地方!!!**
Re1.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
Re1.setAdapter(SA);
SA.setOnItemClickListener(new RelAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int i) {
// byte buff[] = new byte[125*250];
ImageView im0 = view.findViewById(R.id.img_id);
TextView te0 = view.findViewById(R.id.t1);
TextView te1 = view.findViewById(R.id.t2);
TextView te2 = view.findViewById(R.id.t3);
TextView te3 = view.findViewById(R.id.t4);
TextView te4 = view.findViewById(R.id.t5);
TextView te5 = view.findViewById(R.id.t6);
TextView te6 = view.findViewById(R.id.t7);
TextView te7 = view.findViewById(R.id.t14);
// love = view.findViewById(R.id.btn_love);
// Bitmap bitmap = setimage(im0); //调用setimage方法,得到返回值bitmap
Intent intent= new Intent();//轉跳另一頁,並顯示帳密及信箱
intent.setClass(getContext(), shop_thing.class);
Bundle bundle = new Bundle();
bundle.putString("user_id",user_id);
bundle.putString("user_phone",phone);
bundle.putString("cookie",cookie);
bundle.putString("page", String.valueOf(page));
bundle.putString("shop_id",te0.getText().toString());
bundle.putString("shop_product_name",te1.getText().toString());
bundle.putString("shop_specification",te2.getText().toString());
bundle.putString("shop_selling_price",te3.getText().toString());
bundle.putString("shop_operations_center",te4.getText().toString());
bundle.putString("shop_inventory",te5.getText().toString());
bundle.putString("picture_id",te6.getText().toString());
bundle.putString("love",te7.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
});
}catch (Exception e){
Log.e("error",e.toString());
}
}
}