Skip to content Skip to sidebar Skip to footer

Application Is Stopped In Android Emulator

I'm completely new to java, so i have started making an app to find the TOTAL by presing add and subtract buttons. I have defined buttons and the text view in FRAGMENT_MAIN.XML, an

Solution 1:

The problem is that you are mixing up Fragments and Activities, and as such, handling events in the Activity, even though these Views are displayed in the Fragment. Basically you are meant to have a FrameLayout in the Activity, and that's it - the logic is handled in the Fragment.

An example is the following:

publicclassContainerActivityextendsActionBarActivity
{
    @OverridepublicvoidonCreate(Bundle saveInstanceState)
    {
        super.onCreate(saveInstanceState);
        this.setContentView(R.layout.activity_container);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (saveInstanceState == null)
        {               
             getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_container_container, newExampleFragment())
                .addToBackStack(null)
             .commit();
        }
        getSupportFragmentManager().addOnBackStackChangedListener(newOnBackStackChangedListener()
        {
            publicvoidonBackStackChanged()
            {
                intbackCount= getSupportFragmentManager().getBackStackEntryCount();
                if (backCount == 0)
                {
                    finish();
                }
            }
        });
    }
}

activity_container.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><FrameLayoutandroid:id="@+id/activity_container_container"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>

ExampleFragment:

publicclassExampleFragmentextendsFragmentimplementsView.OnClickListener
{
    private Button btnOne;
    private Button btnTwo;
    private Button btnThree;

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        ViewrootView= inflater.inflate(R.layout.fragment_example, container, false);

        btnOne = (Button) rootView.findViewById(R.id.example_button_one);
        btnTwo = (Button) rootView.findViewById(R.id.example_button_two);
        btnThree = (Button) rootView.findViewById(R.id.example_button_three);

        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        return rootView;
    }

    @OverridepublicvoidonClick(View v)
    {
        if (btnOne == v)
        {
            Toast.makeText(getActivity(), "One.", Toast.LENGTH_LONG).show();
        }
        elseif (btnTwo == v)
        {
            Toast.makeText(getActivity(), "Two.", Toast.LENGTH_LONG).show();
        }
        elseif (btnThree == v)
        {
            Toast.makeText(getActivity(), "Three.", Toast.LENGTH_LONG).show();
        }
    }
}

fragment_example.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/example_button_one"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="30dp"android:text="@string/hello"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"/><Buttonandroid:id="@+id/example_button_two"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/example_button_one"android:layout_alignRight="@+id/example_button_one"android:layout_below="@+id/example_button_one"android:layout_marginTop="30dp"android:text="@string/hello" /><Buttonandroid:id="@+id/example_button_three"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/example_button_two"android:layout_alignRight="@+id/example_button_two"android:layout_below="@+id/example_button_two"android:layout_marginTop="30dp"android:text="@string/hello" /></RelativeLayout>

Android-Manifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="19" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.ContainerActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

And that should be a valid example, it shows how you can use an Activity to display a Fragment, and handle events in that Fragment. It currently doesn't show how the Fragment communicates with the Activity to change to another Fragment, which is by the way done by an interface, which you would make a variable to and store in the onAttach() callback method.

But I did include that as well in the "mildly refined" edition on NullPointerException accessing views in onCreate()

Post a Comment for "Application Is Stopped In Android Emulator"