【问题标题】:The call to the onViewCreated() method of superclass doesn't gets executed in onViewCreated() method of fragment对超类的 onViewCreated() 方法的调用不会在片段的 onViewCreated() 方法中执行
【发布时间】:2017-01-05 19:57:41
【问题描述】:

我目前面临关于片段的问题。 众所周知,在fragment的onViewCreated()方法中,第一条语句就是调用超类的onViewCreated()方法:

super.onViewCreated(view, savedInstanceState);

但是,上面的语句没有执行,因此,该语句下面的代码根本没有执行,我的应用程序不断崩溃。我不知道如何解决这个问题。 任何形式的帮助都将不胜感激。

提前致谢!

public class FragmentAddWatchedTVShow extends ListFragment {

private Activity context;

public void setContext(Activity context) {
    this.context = context;
}

@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.frag_add_watched_tvshow, container, false);
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    Log.d(AIntroductionScreen.DEBUGTAG, "Entered in onViewCreated");
//The above log statement is executed but after that nothing gets executed

    super.onViewCreated(view, savedInstanceState);/***THIS STATEMENT IS NOT EXECUTED***/

        /***None of the code below this gets executed***/

    String[] optionsString = getResources().getStringArray(R.array.watched_tvshow_options);

    List<AddOptionsItem> optionsList = new ArrayList<AddOptionsItem>();

    for (int i = 0; i < optionsString.length; i++){
        optionsList.add(new AddOptionsItem(optionsString[i]));          
    }

    AdapterAddWatchedTVShow adapter = new AdapterAddWatchedTVShow(context, optionsList);

    setListAdapter(adapter);


}

Logcat 输出:

08-29 23:11:22.670 658-658/? E/Sensors: new acc setDelay handle(0),ns(66667000) err! go to hwmsen
08-29 23:11:22.786 658-658/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG   History>
08-29 23:11:22.788 658-658/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG   History>
08-29 23:11:22.791 658-658/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG   History>
08-29 23:11:22.791 658-658/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG   History>
08-29 23:11:22.832 137-711/? E/AudioMTKHardware: setCommonParameters() still have param.size() = 1, remain param = "screen_state=on"
08-29 23:11:22.876 927-1018/? E/SQLiteLog: (13) statement aborts at 14: [INSERT INTO activity(start_time,duration,session_id,realtime,refer,name) VALUES (?,?,?,?,?,?)] 
08-29 23:11:22.903 927-1018/? E/SQLiteDatabase: Error inserting start_time=1472494282752 duration=0 session_id=28b85451-33d5-4962-8778-cebb5dbd5b61 realtime=174770322 refer=com.ibingo.launcher2.Launcher name=com.ibingo.launcher2.Launcher
                                                android.database.sqlite.SQLiteFullException: database or disk is full (code 13)
                                                    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                                                    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:786)
                                                    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
                                                    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
                                                    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1494)
                                                    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1364)
                                                    at com.tendcloud.tenddata.aw.a(Unknown Source)
                                                    at com.tendcloud.tenddata.s.a(Unknown Source)
                                                    at com.tendcloud.tenddata.s.b(Unknown Source)
                                                    at com.tendcloud.tenddata.s.a(Unknown Source)
                                                    at com.tendcloud.tenddata.p.handleMessage(Unknown Source)
                                                    at android.os.Handler.dispatchMessage(Handler.java:110)
                                                    at android.os.Looper.loop(Looper.java:193)
                                                    at android.os.HandlerThread.run(HandlerThread.java:61)
08-29 23:11:22.941 11785-11785/? E/NetworkScheduler.SR: Invalid parameter app
08-29 23:11:22.941 11785-11785/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
08-29 23:11:22.986 658-658/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG   History>
08-29 23:11:22.986 658-658/? E/RemoteViews: ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG   History>
08-29 23:11:23.502 927-1018/? E/CellLocation: create GsmCellLocation
08-29 23:11:33.766 137-7226/? E/AudioMTKHardware: setCommonParameters() still have param.size() = 1, remain param = "screen_state=off"
08-29 23:11:33.870 13321-13464/? E/SQLiteLog: (1) no such table: behavior
08-29 23:11:47.680 658-1100/? E/Sensors: new acc setDelay handle(0),ns(20000000) err! go to hwmsen
08-29 23:11:53.440 927-1018/? E/CellLocation: create GsmCellLocation
08-29 23:12:00.069 13321-13651/? E/SQLiteLog: (1) no such table: behavior
08-29 23:12:17.276 658-673/? E/Sensors: new acc setDelay handle(0),ns(20000000) err! go to hwmsen
01-01 00:00:00.000 0-0/? E/Internal: device '0123456789ABCDEF' not found

【问题讨论】:

  • 你是对的吗?它可能不会被执行。它是 ListFragment 而不是 Fragment。 ListFragment 可能会覆盖该方法。你应该重写第一个超类的方法。在您的情况下是 ListFragment。
  • 如果发生崩溃,您应该在问题中包含 logcat 中的异常堆栈跟踪。还有,你怎么知道代码没有被执行?
  • 请张贴logCat!
  • @laalto 我知道是因为我在该语句下方的代码中放置了多个 Log 语句,但 Logcat 输出中没有显示它们。
  • @lionscribe 好的,请稍等。

标签: java android android-fragments fragment android-listfragment


【解决方案1】:

我终于找到了解决问题的方法!

根据ListFragment信息here

ListFragment 具有由单个列表视图组成的默认布局。但是,如果您愿意,您可以通过从 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回您自己的视图层次结构来自定义片段布局。为此,您的视图层次结构必须包含一个 ID 为“@android:id/list”的 ListView 对象(如果它在代码中,则为列表)

在我的视图层次结构中,我有一个 ListView 对象,但如上所述,我没有为该对象提供 id:@android:id/list,而是为该对象分配了一个全新的 id。正因为如此,一个运行时异常被抛出,应用程序每次都崩溃。 无论如何,感谢所有立即回复并试图帮助我的人:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多