台灣最大程式設計社群網站
線上人數
1117
 
會員總數:242929
討論主題:187836
歡迎您免費加入會員
討論區列表 >> Android APP 開發 >> [新手] 關於fragment getAdapter #NullPointerException
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
[新手] 關於fragment getAdapter #NullPointerException
價值 : 50 QP  點閱數:73 回應數:2

樓主

Una
門外漢
0 1
12 1
發送站內信

爬文查了很多與getAdapter相關問題,但似乎都是針對Activity
想在這裡請教fragment的部分。
主要:debug沒問題,開啟fragment指令一下則app閃退。

[logcat]
08-02 03:19:57.318 18316-18316/? I/4_secondprojec: Not late-enabling -Xcheck:jni (already on)
08-02 03:19:57.420 18316-18316/? W/4_secondprojec: Unexpected CPU variant for X86 using defaults: x86
08-02 03:19:57.691 18316-18316/bme.ccycu.a20180724_secondproject W/4_secondprojec: JIT profile information will not be recorded: profile file does not exits.
08-02 03:19:57.692 18316-18316/bme.ccycu.a20180724_secondproject I/chatty: uid=10085(bme.ccycu.a20180724_secondproject) identical 10 lines
08-02 03:19:57.692 18316-18316/bme.ccycu.a20180724_secondproject W/4_secondprojec: JIT profile information will not be recorded: profile file does not exits.
08-02 03:19:57.713 18316-18316/bme.ccycu.a20180724_secondproject I/InstantRun: starting instant run server: is main process
08-02 03:19:57.877 18316-18316/bme.ccycu.a20180724_secondproject W/4_secondprojec: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
08-02 03:19:58.068 18316-18316/bme.ccycu.a20180724_secondproject D/OpenGLRenderer: HWUI GL Pipeline
08-02 03:19:58.151 18316-18338/bme.ccycu.a20180724_secondproject I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
08-02 03:19:58.152 18316-18338/bme.ccycu.a20180724_secondproject I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
08-02 03:19:58.152 18316-18338/bme.ccycu.a20180724_secondproject I/OpenGLRenderer: Initialized EGL, version 1.4
08-02 03:19:58.152 18316-18338/bme.ccycu.a20180724_secondproject D/OpenGLRenderer: Swap behavior 1
08-02 03:19:58.152 18316-18338/bme.ccycu.a20180724_secondproject W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-02 03:19:58.152 18316-18338/bme.ccycu.a20180724_secondproject D/OpenGLRenderer: Swap behavior 0
08-02 03:19:58.168 18316-18338/bme.ccycu.a20180724_secondproject D/EGL_emulation: eglCreateContext: 0xe6505480: maj 2 min 0 rcv 2
08-02 03:19:58.189 18316-18338/bme.ccycu.a20180724_secondproject D/EGL_emulation: eglMakeCurrent: 0xe6505480: ver 2 0 (tinfo 0xe65036e0)
08-02 03:19:58.269 18316-18338/bme.ccycu.a20180724_secondproject D/EGL_emulation: eglMakeCurrent: 0xe6505480: ver 2 0 (tinfo 0xe65036e0)
08-02 03:20:01.112 18316-18316/bme.ccycu.a20180724_secondproject W/4_secondprojec: Accessing hidden method Landroid/view/View;->getTransitionAlpha()F (light greylist, reflection)
08-02 03:20:01.146 18316-18316/bme.ccycu.a20180724_secondproject D/AndroidRuntime: Shutting down VM
08-02 03:20:01.148 18316-18316/bme.ccycu.a20180724_secondproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: bme.ccycu.a20180724_secondproject, PID: 18316
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at bme.ccycu.a20180724_secondproject.fragment_alarm.onCreate(fragment_alarm.java:48)
at android.app.Fragment.performCreate(Fragment.java:2503)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1256)
at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2426)
at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2205)
at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2161)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2062)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:738)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
08-02 03:20:01.172 18316-18316/bme.ccycu.a20180724_secondproject I/Process: Sending signal. PID: 18316 SIG: 9


[fragment_alarm.XML]
class fragment_alarm extends Fragment {
private ListView listview_alarm;
private TextView textview_alarm_time, textview_alarm_title, textview_alarm_repeat;
private Switch switch_alarm_io;
private SimpleAdapter adapter;

private String[] item_time = {"06:30", "10:00"};
private String[] item_title = {"飯前", "早餐後"};
private String[] item_repeat = {"每天", "周末"};

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

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < item_time.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("time", item_time[i]);
map.put("title", item_title[i]);
map.put("repeat", item_repeat[i]);
list.add(map);
}

listview_alarm = (ListView)getActivity().findViewById(R.id.listview_alarm);
adapter = new SimpleAdapter(getActivity(), list, R.layout.alarm_listitem, new String[]{"time","title","repeat"}, new int[]{R.id.textview_alarm_time, R.id.textview_alarm_title, R.id.textview_alarm_repeat});
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_alarm, container, false);
textview_alarm_time = (TextView)view.findViewById(R.id.textview_alarm_time);
textview_alarm_title = (TextView)view.findViewById(R.id.textview_alarm_title);
textview_alarm_repeat = (TextView)view.findViewById(R.id.textview_alarm_repeat);

return view;
}
}





####第一次發文,排版請見諒####



搜尋相關Tags的文章: [ android ] , [ java ] , [ fragment ] , [ adapter ] , [ listview ] , [ getadapter ] ,
本篇文章發表於2018-08-02 11:32
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
作者回應

Una
檢舉此回應
抱歉 我打錯了,不是getadapter,是setadaper
本篇文章回覆於2018-08-02 11:35
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
2樓
最有價值解答

Silent
檢舉此回應
你的程式碼 沒有setAdapter
listview_alarm.setAdapter(adapter);



本篇文章回覆於2018-08-04 19:17
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.