【问题标题】:View is not getting added in the relative layout on button click in the fragment在片段中单击按钮时,视图未添加到相对布局中
【发布时间】:2014-10-28 19:12:13
【问题描述】:

我正在开发一个应用程序,我必须在单击按钮时添加动态视图。 为此,我在我的主 xml 中使用了一个片段和一个相对布局。在片段 xml 中,我使用了添加按钮,并在此按钮上单击我想在相对布局中显示视图,即按钮。 这是我的代码。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/titles"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        class="com.example.sampledynamicview.MenuFragment" />

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

fragment 类是

Button btnAddButton;
RelativeLayout rl1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu_fragment, null);
    btnAddButton = (Button) view.findViewById(R.id.btnAdd);
    rl1 = (RelativeLayout) view.findViewById(R.id.relative_layout);
    btnAddButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Button btn = new Button(getActivity());
            btn.setText("Button");
            rl1.addView(btn);
        }
    });

    return view;
}

menu_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFD3D3D3"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

logcat 中的错误是

09-04 11:42:14.866: E/AndroidRuntime(552): FATAL EXCEPTION: main
09-04 11:42:14.866: E/AndroidRuntime(552): java.lang.NullPointerException
09-04 11:42:14.866: E/AndroidRuntime(552):  at com.example.sampledynamicview.MenuFragment$1.onClick(MenuFragment.java:38)
09-04 11:42:14.866: E/AndroidRuntime(552):  at android.view.View.performClick(View.java:2408)
09-04 11:42:14.866: E/AndroidRuntime(552):  at android.view.View$PerformClick.run(View.java:8816)
09-04 11:42:14.866: E/AndroidRuntime(552):  at android.os.Handler.handleCallback(Handler.java:587)
09-04 11:42:14.866: E/AndroidRuntime(552):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 11:42:14.866: E/AndroidRuntime(552):  at android.os.Looper.loop(Looper.java:123)
09-04 11:42:14.866: E/AndroidRuntime(552):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 11:42:14.866: E/AndroidRuntime(552):  at java.lang.reflect.Method.invokeNative(Native Method)
09-04 11:42:14.866: E/AndroidRuntime(552):  at java.lang.reflect.Method.invoke(Method.java:521)
09-04 11:42:14.866: E/AndroidRuntime(552):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 11:42:14.866: E/AndroidRuntime(552):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 11:42:14.866: E/AndroidRuntime(552):  at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 这里的相对布局是 Activity 布局的一部分,而不是 Fragment 布局的一部分。所以当你说rl1 = (RelativeLayout) view.findViewById(R.id.relative_layout);时,会返回null...
  • 好的,我现在应该怎么做才能让它运行??
  • 它不工作错误是一样的

标签: android dynamic view fragment onclicklistener


【解决方案1】:
 LayoutInflater inflater =  (LayoutInflater)getContext().
                              getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.test, parent, false);
 btnAddButton = (Button) view.findViewById(R.id.btnAdd);
    rl1 = (RelativeLayout) view.findViewById(R.id.relative_layout);

【讨论】:

  • 请详细说明一下好吗?
【解决方案2】:

你的观点是

查看视图 = inflater.inflate(R.layout.menu_fragment, null);

不是你的 relative_layout 所在的 activity_main.xml。

你的 menu_fragment 是否有 btnAdd 和 relative_layout 的组件,如果没有它会崩溃

你能发布 menu_fragment.xml 吗?

【讨论】:

  • 我要添加片段 xml
  • 所以显然你的 menu_fragment.xml 中没有 id :relative_layout,尝试添加一个。不要混淆,您现在在 menu_fragment 作为视图。不是activity_main
  • 你看到我发的图片了吗??
  • @hanish 你的意思是你的 menu_fragment.xml。是的。我看到了
  • 我想在相对布局中添加按钮,当我点击片段中的按钮时
【解决方案3】:

尝试修改你的 menu_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFD3D3D3"
android:orientation="vertical" >

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />


<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

【讨论】:

  • 它将在片段中添加按钮,这不是我的要求,我想在右侧显示它,这是我在主要活动中的真实布局
  • hm..ok 在这种情况下,当您单击片段中的添加按钮时,您需要在活动中添加按钮而不是片段类使用回调。在您的活动类中触发回调函数,在此处添加按钮。
  • 我试过了,但是点击没有任何反应
  • 是的,我调用了 AddButton(v);在您建议的任何地方应用代码后,在片段类中添加按钮的单击侦听器中
  • 您是否实现了回调,onAttach(Activity activity) 类似于我发布的答案。使用 Log 确保您能够首先从片段中调用 AddButtonInActivityClass。
【解决方案4】:

尝试一下它的代码,它会工作,我已经在我的系统中测试过它

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    LayoutInflater inflater =  (LayoutInflater)getContext().
                                  getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View view = inflater.inflate(R.layout.menu_fragment, parent, false)
        btnAddButton = (Button) view.findViewById(R.id.btnAdd);
        rl1 = (RelativeLayout) view.findViewById(R.id.relative_layout);
        btnAddButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Button btn = new Button(getActivity());
                btn.setText("Button");
                rl1.addView(btn);
            }
        });

        return view;
    }

【讨论】:

    【解决方案5】:

    在你的 menu_fragment 中

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="AddButton"
        android:text="Button" />
    

    在你的片段类中

    private Callbacks mCallbacks;
    public interface Callbacks {
            //Callback for when button clicked.
            public void AddButtonInActivityClass();
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);       
    
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnFragmentChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }
    
    public AddButton(View v){
    ((Callbacks) getActivity()).AddButtonInActivityClass();
    }
    

    然后在你的活动类中

    @Override
    public void AddButtonInActivityClass() {
    //Here is where you start to add your button
    
    //only at the activity which inflate activity_main can call relative_layout
    rl1 = (RelativeLayout) view.findViewById(R.id.relative_layout);
    
    
      Button btn = new Button(this);
      btn.setText("Button");
      rl1.addView(btn);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多