-3

I am new to the android working environment, was following some tutorial regarding fragments, there appears to be no error when i build and run the project but once the app is installed on the emulator i get this message in the dialog saying the app has stopped working

I have spent an entire day looking for a solution now any help will be appreciated here is the source code below: MainActivity.java:

package com.example.fragmentsanotherexample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

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

    FragmentManager fm=getSupportFragmentManager();
    android.support.v4.app.Fragment fragment=fm.findFragmentById(R.id.fragment_content);

    if(fragment==null){

        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.fragment_content, new BasicFragment());
        ft.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;
}

 }

BasicFragment.java:

package com.example.fragmentsanotherexample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;



public class BasicFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view=inflater.inflate(R.layout.basic_fragment,container,false);

    Button button=(Button) view.findViewById(R.id.fragment_button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Activity activity=getActivity();

            if(activity != null){

                Toast.makeText(activity, R.string.hello_world, Toast.LENGTH_LONG).show();
            }

        }
    });
    return view;
}


}

Here is the basic_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button android:id="@+id/fragment_button"
    android:text="@string/btn_fragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />



</LinearLayout>

Here is activiy_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FragmentLayout
    android:id="@+id/fragment_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />




</LinearLayout>

Here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fragmentsanotherexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.fragmentsanotherexample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

As far as the logcat is concerned it's empty, any sort of help would be really appreciated

Hassan
  • 111
  • 1
  • 14
  • 2
    its a crash how can it be empty – Raghunandan Jul 04 '13 at 08:45
  • 2
    Logcat shouldn't be empty. Try adjusting filters for the correct pid, or try running on phone. – Timmetje Jul 04 '13 at 08:46
  • go to ddms and select your device and application you will get the log – farrukh Jul 04 '13 at 08:47
  • After inflating your fragment, add it to your main view before assigning button listener. – Aleks G Jul 04 '13 at 08:49
  • this is what i get on the console – Hassan Jul 04 '13 at 08:51
  • console is different from logcat. – Raghunandan Jul 04 '13 at 08:51
  • [2013-07-04 13:42:35 - FragmentsAnotherExample] Success! [2013-07-04 13:42:35 - FragmentsAnotherExample] Starting activity com.example.fragmentsanotherexample.MainActivity on device emulator-5554 [2013-07-04 13:42:36 - FragmentsAnotherExample] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.fragmentsanotherexample/.MainActivity } – Hassan Jul 04 '13 at 08:51
  • @user1485075 if using eclipse goto windows open perspective . click ddms. there you can see the logcat. post the details of crash – Raghunandan Jul 04 '13 at 08:53
  • If your emulator is detached than try with rest adb option of device perspective . – kaushal trivedi Jul 04 '13 at 08:55

1 Answers1

1
   <FragmentLayout
    android:id="@+id/fragment_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

AFAIK FragmentLayout doest not exist. Do you want a FrameLayout?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • The project wouldn't compile if this were the case – Aleks G Jul 04 '13 at 08:50
  • @AleksG are you sure? – Blackbelt Jul 04 '13 at 08:52
  • The tutorial i am following say that if u want to add fragment at runtime u ull have to change the fragment to fragmentlayout:http://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application – Hassan Jul 04 '13 at 08:53
  • I can't see how the project would compile/build if there were errors in XML - that's all. I haven't encountered FragmentLayout myself either. – Aleks G Jul 04 '13 at 08:53
  • I've observed using Your code 'java.lang.RuntimeException: Unable to start activity ... Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.FragmentLayout" '. I believe You should observe the same. – sandrstar Jul 04 '13 at 08:55
  • I think the errors are due of an xml that can not be parsed. If the "translation" from xml to java generates a class that does not exists the problem I think the problem will be at runtime. What do you think? – Blackbelt Jul 04 '13 at 08:56
  • @blackbelt nice catch. staring at the code for a long time. could not spot the mistake – Raghunandan Jul 04 '13 at 08:56
  • can it be a problem with the min version of API being used?? – Hassan Jul 05 '13 at 07:16
  • As far as you use the support package you are good. – Blackbelt Jul 05 '13 at 07:17
  • iv tried but still have'nt pointed out the error, can it be an issue with the APIs i am using and for this example my minSDK version in the maifest is 8, is it a problem???? – Hassan Jul 05 '13 at 09:52
  • you should tell me what error do you get at least – Blackbelt Jul 05 '13 at 09:52