新空白活动的空指针异常错误

时间:2014-03-23 01:37:49

标签: java android fragment

我刚创建了一个新的空白活动项目,但是当我实现一个按钮/ Imagebutton时,会发生错误......

这是代码:

public class MainActivity extends ActionBarActivity implements OnClickListener {

    ImageButton html;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        html = (ImageButton) findViewById(R.id.bHTML);

        html.setOnClickListener(this);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 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_main, container,
                    false);
            return rootView;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.bHTML:
            Intent i = new Intent(this, HtmlListView.class);
            startActivity(i);
            break;
        }

    }

}

我还在学习如何使用更新的ADT为新的空白活动提供的片段模板..如果有什么我需要做的,请说出来..非常感谢!

编辑:fragment_main:

<LinearLayout 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:orientation="vertical"
    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.gtxradeon.learnhtml.MainActivity$PlaceholderFragment" >

    <ImageButton
        android:id="@+id/bHTML"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/html" />

</LinearLayout>

activity_main:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gtxradeon.learnhtml.MainActivity"
    tools:ignore="MergeRootFrame" />

错误:

ERROR

2 个答案:

答案 0 :(得分:1)

您的按钮已添加到fragment_main而不是activity_main中。只有在附加片段后才能找到按钮视图..

更改活动onCreate以下..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

和PlaceholderFragment onCreateView as ...

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        html = (ImageButton) rootView.findViewById(R.id.bHTML);

        html.setOnClickListener(MainActivity.this);
        return rootView;
    }

答案 1 :(得分:0)

我用这段代码解决了它:

public class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

            ImageButton html = (ImageButton) rootView.findViewById(R.id.bHTML);

            final View.OnClickListener Click = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {

                    case R.id.bHTML:
                        Intent i = new Intent(getActivity(), HtmlListView.class);
                        startActivity(i);
                        break;
                    }
                }
            };
            html.setOnClickListener(Click);
            return rootView;
        }
    }