在片段中单击按钮时的相对布局中未添加视图

时间:2014-09-04 06:32:20

标签: android dynamic view fragment onclicklistener

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

the avd view

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>

片段类是

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)

5 个答案:

答案 0 :(得分:0)

您的观点是

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

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

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

你可以发布menu_fragment.xml吗?

答案 1 :(得分:0)

 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 :(得分:0)

尝试修改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>

答案 3 :(得分:0)

尝试此代码它将起作用,我已经在我的系统中测试了它

@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;
    }

答案 4 :(得分:0)

在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);

}