无法访问片段中的按钮

时间:2015-06-27 19:25:17

标签: android android-fragments

我是android编程的新手。只是想尝试使用片段。我使用以下代码:

public class SimpleFragActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_fragment);
        if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }


    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_simple,
                                             container, false);
            Button btnClickMeBlue = (Button)rootView.findViewById(R.id.btnsimplefrag);
            return rootView;
        }
    }
}

XML代码:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://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.framentexample.SimpleFragActivity$PlaceholderFragment" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:scale="centercrop"
        android:src="@drawable/royal_enfield_blue" />

    <Button
        android:id="@+id/btnsimplefrag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="42dp"
        android:text="Click Me Blue" />

</RelativeLayout>

但它会抛出错误

  

btnsimplefrag无法解析

有人可以帮帮我吗?我尝试了很多东西,无法解决问题。

0 个答案:

没有答案