【问题标题】:How to Repeat fragment layout ? in Android如何重复片段布局?在安卓中
【发布时间】:2014-08-22 23:37:29
【问题描述】:

现在我想将这个布局“R.layout.post”添加到这个布局“R.layout.fragment_main”中,并重复多次并将其作为视图返回。

所以,我不知道如何将那个 LinearLayout “mainLayout” 放在 View 中以将其返回

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        LinearLayout mainLayout = (LinearLayout) rootView.findViewById(R.layout.fragment_main); 
        int k = 3;
        for (int i = 0 ; i < k ;i++)
        {
            rootView = inflater.inflate(R.layout.post, container, false);
            mainLayout.addView(rootView);
        }

        return rootView;
    }

fragment_main.xml

<RelativeLayout xmlns:android="schemas.android.com/apk/res/android";     
  xmlns:tools="schemas.android.com/tools"; android:layout_width="match_parent"    
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="com.example.wikimob.MainActivity$PlaceholderFragment" 
  android:id="@+id/frgment"> 
 </RelativeLayout>

post.xml

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical" > 
  <LinearLayout
   android:id="@+id/postLayout"    
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:orientation="vertical"> 
  </LinearLayout>
</LinearLayout>

LogCat

07-02 13:21:05.537: I/art(17001): Profiler disabled.  To enable setprop    dalvik.vm.profiler 1
07-02 13:21:05.642: I/am_on_resume_called(17001): [0,com.example.wikimob.Splash]
07-02 13:21:05.647: D/CanvasContext(17001): Render dirty regions requested: true
07-02 13:21:05.650: I/am_on_resume_called(17001): [0,com.example.wikimob.Splash]
07-02 13:21:05.677: W/Adreno-GSL(17001): <ioctl_kgsl_device_getproperty:663>: mmap failed: errno 22 Invalid argument
07-02 13:21:05.677: I/Adreno-EGL(17001): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I10246dbd022c719c705be805d5642cc8fdfbd2a2Date: 03/07/14
07-02 13:21:05.679: I/CanvasContext(17001): Initialized EGL, version 1.4
07-02 13:21:05.700: D/OpenGLRenderer(17001): Enabling debug mode 0
07-02 13:21:07.855: I/am_on_paused_called(17001): [0,com.example.wikimob.Splash]
07-02 13:21:07.891: I/am_on_resume_called(17001): [0,com.example.wikimob.Splash]
07-02 13:21:08.643: I/am_on_paused_called(17001): [0,com.example.wikimob.Splash]
07-02 13:21:08.821: D/AndroidRuntime(17001): Shutting down VM
07-02 13:21:08.823: E/AndroidRuntime(17001): FATAL EXCEPTION: main
07-02 13:21:08.823: E/AndroidRuntime(17001): Process: com.example.wikimob, PID: 17001
07-02 13:21:08.823: E/AndroidRuntime(17001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wikimob/com.example.wikimob.MainActivity}:     java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at     android.app.ActivityThread.access$800(ActivityThread.java:143)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.os.Looper.loop(Looper.java:135)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.ActivityThread.main(ActivityThread.java:5070)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at java.lang.reflect.Method.invoke(Native Method)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at java.lang.reflect.Method.invoke(Method.java:372)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
07-02 13:21:08.823: E/AndroidRuntime(17001): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
07-02 13:21:08.823: E/AndroidRuntime(17001):    at com.example.wikimob.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:158)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.Fragment.performCreateView(Fragment.java:1704)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1448)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.Activity.performStart(Activity.java:5735)
07-02 13:21:08.823: E/AndroidRuntime(17001):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
07-02 13:21:08.823: E/AndroidRuntime(17001):    ... 10 more
07-02 13:21:14.540: I/art(17001): Heap transition to ProcessStateJankImperceptible took 49.541553ms saved at least 607KB
07-02 13:23:08.545: I/Process(17001): Sending signal. PID: 17001 SIG: 9
07-02 13:31:40.233: I/am_on_resume_called(18304): [0,com.example.wikimob.Splash]
07-02 13:31:40.239: D/CanvasContext(18304): Render dirty regions requested: true
07-02 13:31:40.267: W/Adreno-GSL(18304): <ioctl_kgsl_device_getproperty:663>: mmap failed: errno 22 Invalid argument
07-02 13:31:40.267: I/Adreno-EGL(18304): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I10246dbd022c719c705be805d5642cc8fdfbd2a2Date: 03/07/14
07-02 13:31:40.269: I/CanvasContext(18304): Initialized EGL, version 1.4
07-02 13:31:40.288: D/OpenGLRenderer(18304): Enabling debug mode 0
07-02 13:31:43.239: I/am_on_paused_called(18304): [0,com.example.wikimob.Splash]
07-02 13:31:43.424: D/AndroidRuntime(18304): Shutting down VM
07-02 13:31:43.425: E/AndroidRuntime(18304): FATAL EXCEPTION: main
07-02 13:31:43.425: E/AndroidRuntime(18304): Process: com.example.wikimob, PID: 18304
07-02 13:31:43.425: E/AndroidRuntime(18304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wikimob/com.example.wikimob.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.ActivityThread.access$800(ActivityThread.java:143)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.os.Looper.loop(Looper.java:135)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.ActivityThread.main(ActivityThread.java:5070)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at java.lang.reflect.Method.invoke(Native Method)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at java.lang.reflect.Method.invoke(Method.java:372)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
07-02 13:31:43.425: E/AndroidRuntime(18304): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
07-02 13:31:43.425: E/AndroidRuntime(18304):    at com.example.wikimob.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:158)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.Fragment.performCreateView(Fragment.java:1704)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1448)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.Activity.performStart(Activity.java:5735)
07-02 13:31:43.425: E/AndroidRuntime(18304):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
07-02 13:31:43.425: E/AndroidRuntime(18304):    ... 10 more
07-02 13:31:49.069: I/art(18304): Heap transition to ProcessStateJankImperceptible took 40.922905ms saved at least 573KB
07-02 13:32:51.355: I/Process(18304): Sending signal. PID: 18304 SIG: 9
07-02 13:34:08.638: I/art(18842): Profiler disabled.  To enable setprop dalvik.vm.profiler 1
07-02 13:34:08.760: I/am_on_resume_called(18842): [0,com.example.wikimob.Splash]
07-02 13:34:08.768: D/CanvasContext(18842): Render dirty regions requested: true
07-02 13:34:08.796: W/Adreno-GSL(18842): <ioctl_kgsl_device_getproperty:663>: mmap failed: errno 22 Invalid argument
07-02 13:34:08.796: I/Adreno-EGL(18842): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I10246dbd022c719c705be805d5642cc8fdfbd2a2Date: 03/07/14
07-02 13:34:08.797: I/CanvasContext(18842): Initialized EGL, version 1.4
07-02 13:34:08.813: D/OpenGLRenderer(18842): Enabling debug mode 0
07-02 13:34:11.764: I/am_on_paused_called(18842): [0,com.example.wikimob.Splash]
07-02 13:34:11.949: D/AndroidRuntime(18842): Shutting down VM
07-02 13:34:11.950: E/AndroidRuntime(18842): FATAL EXCEPTION: main
07-02 13:34:11.950: E/AndroidRuntime(18842): Process: com.example.wikimob, PID: 18842
07-02 13:34:11.950: E/AndroidRuntime(18842): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wikimob/com.example.wikimob.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.ActivityThread.access$800(ActivityThread.java:143)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.os.Looper.loop(Looper.java:135)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.ActivityThread.main(ActivityThread.java:5070)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at java.lang.reflect.Method.invoke(Native Method)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at java.lang.reflect.Method.invoke(Method.java:372)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
07-02 13:34:11.950: E/AndroidRuntime(18842): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
07-02 13:34:11.950: E/AndroidRuntime(18842):    at com.example.wikimob.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:158)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.Fragment.performCreateView(Fragment.java:1704)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1448)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.Activity.performStart(Activity.java:5735)
07-02 13:34:11.950: E/AndroidRuntime(18842):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
07-02 13:34:11.950: E/AndroidRuntime(18842):    ... 10 more
07-02 13:34:17.592: I/art(18842): Heap transition to ProcessStateJankImperceptible took     11.996296ms saved at least 615KB

【问题讨论】:

  • 运行这个会发生什么?有没有崩溃?
  • 总是崩溃,并且活动永远不会启动
  • 同时发布您的 XML 代码

标签: java android android-layout layout android-fragments


【解决方案1】:

我认为问题是LinearLayout

LinearLayout mainLayout = (LinearLayout)rootView.findViewById(R.layout.fragment_main); 

您发现 rootView.findViewById(R.layout.fragment_main) 它的布局 ID 不是 linearLayout 它应该像 R.id.linearlayout(无论您的布局 ID)

编辑 1

这样做

RelativeLayout mainLayout  = (RelativeLayout)rootView.findViewById(R.id.fragment);

【讨论】:

  • 我想在 mainLayout 中获得该布局,但是当我写它时没有参考 rootView 像这样 'LinearLayout mainLayout = (LinearLayout) findViewById( R.layout.fragment_main);',出现错误Cannot make a static reference to the non-static method findViewById(int) from type Activity
  • 你的布局不是 VIEW 放你的 XML
  • 好的,我发现它的相对布局不是线性的,但我的问题仍然是关于如何将 mainLayout 作为视图返回
  • 这不是完整的 logcat
  • 如何在这里发布所有内容?!
猜你喜欢
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多