Reputation:
I'm calling a fragment class from an activity so I am not able to call this fragment class because I'm getting an error which is given below and here is my full code so any one can help me please thanks in advance.
Activity class
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_second);
}
public void onClickNext(View view) {
Intent intent = new Intent(Second.this, MainActivity.class);
startActivityForResult(intent, 400);
overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_bottom);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_top);
}
public void btnHome(View view) {
Intent intent = new Intent(Second.this, Welcome.class);
startActivity(intent);
}
public void aboutUsClick(View view) {
Fragment fragment = null;
fragment = new AboutFragment();
startActivity(new Intent(this, AboutFragment.class));
}
}
Fragment class
package com.example.lunetta;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AboutFragment extends Fragment{
public AboutFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_layout_about, container,
false);
return view;
}
}
Android Manifest
<activity
android:name="com.example.lunetta.AboutFragment"
android:label="@string/title_fragment_layout_about" >
</activity>
I got this type error in log cat
03-30 10:24:34.876: E/AndroidRuntime(5299): FATAL EXCEPTION: main
03-30 10:24:34.876: E/AndroidRuntime(5299): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lunetta/com.example.lunetta.AboutFragment}: java.lang.ClassCastException: com.example.lunetta.AboutFragment cannot be cast to android.app.Activity
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.os.Looper.loop(Looper.java:137)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-30 10:24:34.876: E/AndroidRuntime(5299): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 10:24:34.876: E/AndroidRuntime(5299): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 10:24:34.876: E/AndroidRuntime(5299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 10:24:34.876: E/AndroidRuntime(5299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 10:24:34.876: E/AndroidRuntime(5299): at dalvik.system.NativeStart.main(Native Method)
03-30 10:24:34.876: E/AndroidRuntime(5299): Caused by: java.lang.ClassCastException: com.example.lunetta.AboutFragment cannot be cast to android.app.Activity
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-30 10:24:34.876: E/AndroidRuntime(5299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-30 10:24:34.876: E/AndroidRuntime(5299): ... 11 more
Upvotes: 0
Views: 1757
Reputation: 2126
You are not using fragment properly. Fragment is supposed to be part of an activity and can't be started using startActivity(). You should either
i) Add the fragment to your activity's xml layout file or
ii) Programmatically add it to your activity
Upvotes: 0
Reputation: 1024
This is wrong:
startActivity(new Intent(this, AboutFragment.class));
You are starting an activity but the AboutFragment.class is a fragment and not an activity. You can use replace in order to initialize a fragment.
Upvotes: 0
Reputation: 8106
The reason is, that a Fragment is not an Activity. It's a part of an Activity which uses almost the same livecycle like an activity, but at least you must have an activity which replaces an container with an fragment.
Remember: Fragments are used for several reason. One is that you can use several fragments (layouts whatever) within your activity and replace them. Second is, that it can be build more flexible.
You could do it this way if you want to use a fragment:
public void aboutUsClick(View view) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
while the Fragment class extends DialogFragment.
Another idea is creating a Class which loads an layout which replaces your fragment.
For this you will need to create a new layout which may look like this (from andorid developer samples)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
and then start the Activity which loads/holds the fragment.
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
}
Upvotes: 1