0

i just created a new project with new blank activity but the moment i implemented a button/Imagebutton an error happens...

here is the code:

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

    }

}

i am still learning how to use the fragment template that the updated ADT provides for new blank activities.. if there is anything i need to do, please say it.. thank you very much!

EDIT: 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:

ERROR

Christian Burgos
  • 1,572
  • 9
  • 26
  • 48
  • 5
    Sigh... post the log (or at least the stack trace), clearly identify the line in your code that causes the exception, and please be sure to read [What is a NullPointerException?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) first. – Jason C Mar 23 '14 at 01:39
  • 1
    post your activity_main layout – Libin Mar 23 '14 at 02:19
  • added them on edit... – Christian Burgos Mar 23 '14 at 02:36

2 Answers2

1

your button is added in fragment_main not in activity_main. you can find button view only after attaching the fragment..

Change Activity onCreate below..

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

and 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;
    }
Libin
  • 16,967
  • 7
  • 61
  • 83
0

i solved it with this code:

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;
        }
    }
Christian Burgos
  • 1,572
  • 9
  • 26
  • 48