0

I'm getting a runtime exception from my buttonfragment.class. I doing a fragmented button bar, each button with it's own layout and activity. I've been changing things around and can't figure out where in the fragment class is the error is. Thanks

MainActivity.java

public class MainActivity extends Activity {

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

    Button btn1 =(Button)findViewById(R.id.btn1);
    Button btn2 =(Button)findViewById(R.id.btn2);
    Button btn3 =(Button)findViewById(R.id.btn3);
    Button btn4 =(Button)findViewById(R.id.btn4);

    btn1.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i = new Intent(MainActivity.this, ButtonFragment1.class);
            setContentView(R.layout.activity_main);
            startActivity(i);
        }
    });

    btn2.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i = new Intent(MainActivity.this, ButtonFragment2.class);
            i.setAction(Intent.ACTION_VIEW);
            i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            setContentView(R.layout.agil_main);
            startActivity(i);
        }
    });


    btn3.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i = new Intent(MainActivity.this, ButtonFragment3.class);
            i.setAction(Intent.ACTION_VIEW);
            i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            setContentView(R.layout.int_main);
            startActivity(i);
        }
    });


    btn4.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i = new Intent(MainActivity.this, ButtonFragment4.class);
            i.setAction(Intent.ACTION_VIEW);
            i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            setContentView(R.layout.misc_main);
            startActivity(i);
        }
    });
}}

ButtonFragment1.java

public class ButtonFragment1 extends Fragment implements  OnClickListener 
{ 

private static final String TAG = null;
protected static final Context context=null;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

}   
 public void onActivityCreated(Bundle savedInstanceState) 
{       
super.onActivityCreated(savedInstanceState);

}    

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

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

 Button Btn = (Button)  view.findViewById(R.id.btn1);

    Btn.setOnClickListener(this);

            return view;
 }


 public void onClick(View v)
 {

        if(v.getId()==R.id.btn1) 
    {
                 // TODO Auto-generated method stub

    startActivity(new Intent(v.getContext(),MainActivity.class));    
            }
     }
}

buttonbar.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:shrinkColumns="*"  
android:stretchColumns="*" 
android:background="#6B1414">    
<TableRow
    android:id="@+id/tableRow1"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <Button
        android:id="@+id/btn1"
        android:text="@string/Str"
        android:textStyle="bold"
        android:layout_width="0dip"
        android:layout_height="wrap_content" 
        android:padding="18dip"
        android:layout_weight="1" 
        android:background="#424242"
        android:textColor="#ffffff"
        android:gravity="center"/>
    <Button
        android:id="@+id/btn2"
        android:text="@string/Agl"
        android:textStyle="bold"
        android:layout_width="0dip"
        android:layout_height="wrap_content" 
        android:padding="18dip"
        android:layout_weight="1" 
        android:background="#424242"
        android:textColor="#ffffff"
        android:gravity="center"/>
    <Button
        android:id="@+id/btn3"
        android:text="@string/Int"
        android:textStyle="bold" 
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:padding="18dip"
        android:layout_weight="1" 
        android:background="#424242"
        android:textColor="#ffffff"
        android:gravity="center"/>
    <Button
        android:id="@+id/btn4"
        android:text="@string/Misc"
        android:textStyle="bold" 
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:background="#424242"
        android:textColor="#ffffff"
        android:padding="18dip"/>
</TableRow> 
</TableLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.killerphone.dota2sounds"
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="@android:style/Theme.NoTitleBar" >
    <activity
        android:name="com.killerphone.dota2sounds.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainAgil"></activity>
    <activity android:name=".MainInt"></activity>
    <activity android:name=".MainMisc"></activity>
    <activity android:name=".ButtonFragment1"></activity>
    <activity android:name=".ButtonFragment2"></activity>
    <activity android:name=".ButtonFragment3"></activity>
    <activity android:name=".ButtonFragment4"></activity>
</application>

Update: Post Trace Errors - I can get to the next layout/activity, but then it crashes right after.

03-25 15:08:16.705: D/AndroidRuntime(3438): Shutting down VM
03-25 15:08:16.705: W/dalvikvm(3438): threadid=1: thread exiting with uncaught    exception (group=0x4001d5a0)
03-25 15:08:16.705: E/AndroidRuntime(3438): FATAL EXCEPTION: main
03-25 15:08:16.705: E/AndroidRuntime(3438): java.lang.RuntimeException: Unable to  instantiate activity  ComponentInfo{com.killerphone.dota2sounds/com.example.test.ButtonFragment2}:  java.lang.ClassCastException: com.example.test.ButtonFragment2
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1849)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1960)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.ActivityThread.access$1500(ActivityThread.java:145)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1045)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.os.Looper.loop(Looper.java:150)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.ActivityThread.main(ActivityThread.java:4369)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at java.lang.reflect.Method.invoke(Method.java:507)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:846)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at dalvik.system.NativeStart.main(Native Method)
03-25 15:08:16.705: E/AndroidRuntime(3438): Caused by: java.lang.ClassCastException: com.example.test.ButtonFragment2
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
03-25 15:08:16.705: E/AndroidRuntime(3438):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1841)
03-25 15:08:16.705: E/AndroidRuntime(3438):     ... 11 more
03-25 15:08:16.806: D/dalvikvm(3438): GC_CONCURRENT freed 234K, 48% free 2974K/5639K,  external 0K/0K, paused 3ms+2ms
03-25 15:08:18.157: I/Process(3438): Sending signal. PID: 3438 SIG: 9

Update #2 Button before adding Fragment, but was getting animation from previous layout/activity. Basically, the buttons are placed on the top of the application in all layouts and activities.

     btn1.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i = new Intent(getApplicationContext(), MainActivity.class);
            i.setAction(Intent.ACTION_VIEW);
            i.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            setContentView(R.layout.activity_main);
            startActivity(i);
        }
    });

1 Answers1

1

You cannot start a Fragment like an Activity:

new Intent(MainActivity.this, ButtonFragment1.class);

You need to fetch an instance of the FragmentManager then call add(), replace()`, etc to use Fragments in your Activity.

The Developer's Guide has multiple detailed examples and tutorials on this subject.


You should also remove the errant declarations in your manifest, since you now know that a Fragment is not an Activity:

<activity android:name=".ButtonFragment1"></activity>


how would I fetch the activity class and the FragmentManager class for each button?

Honestly I don't understand what you are trying to do with your ButtonFragments, so I won't be able to help you with specifics but I can point you in the right direction. Inside your Activity (or FragmentActivity), when you want to create a copy of you Fragment use:

ButtonFragment buttonFrag = new ButtonFragment();
// Use setArguments() to pass extras to your Fragment
getFragmentManager().beginTransaction().add(R.id.fragment_container, buttonFrag).commit();

This is the correct way to add a Fragment to your layout.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • how would I fetch the activity class and the FragmentManager class for each button? – DuckiesUnite Mar 25 '13 at 22:26
  • I was getting a animation from the previous activity when I pressed the button for the next layout/activity. Also, when I hit the back button it took me to the previous activity. I wanted to remedy this so I did some research and found that fragmenting would correct it. Sorry in advance, for the hassle, I'm a student working on this as one of my class projects. – DuckiesUnite Mar 25 '13 at 22:44
  • Why do you call `setContentView()` before `startActivity()`? That will change the layout of the activity you are about to leave... Anyway while Fragments are great for "compartmentalizing" your Activity, you can change the transition from one Activity to another with code [like this.](http://stackoverflow.com/q/3389501/1267661) – Sam Mar 26 '13 at 00:26