Reputation: 131
I get ClassCastException while compilating sources in Android Studio. I can't understand what's causing them. I hope you could help me fix it. This is for my classes but to even start it I wanted to test the basics with emulator with API 16(got Genymotion so if you want me to, I can test it using any available API) but couldn't get it to work.
MainActivity
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
private android.support.v7.app.ActionBar.Tab tabOne;
private android.support.v7.app.ActionBar.Tab tabTwo;
private android.support.v7.app.ActionBar.Tab tabThree;
private Fragment fragmentOne = new FragmentOne();
private Fragment fragmentTwo = new FragmentTwo();
private Fragment fragmentThree = new FragmentThree();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabOne = actionBar.newTab().setText("fragmentOne").setTabListener( new TabListener<FragmentOne>(this, "fragmentOne", FragmentOne.class));
tabTwo = actionBar.newTab().setText("fragmentTwo").setTabListener( new TabListener<FragmentTwo>(this, "fragmentOTwo", FragmentTwo.class));
tabThree = actionBar.newTab().setText("fragmentThree").setTabListener( new TabListener<FragmentThree>(this, "fragmentThree", FragmentThree.class));
actionBar.addTab(tabOne);
actionBar.addTab(tabTwo);
actionBar.addTab(tabThree);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and my TabListener
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class TabListener<T extends Fragment> implements android.support.v7.app.ActionBar.TabListener {
private android.support.v4.app.Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
if (mFragment == null){
mFragment = android.support.v4.app.Fragment.instantiate(mActivity, mClass.getName());
fragmentTransaction.add(android.R.id.content, mFragment, mTag);
}
else{
fragmentTransaction.attach(mFragment);
}
}
@Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
if(mFragment != null){
fragmentTransaction.detach(mFragment);
}
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
}
}
fragment classes are exact the same at the moment because I just wanted to see if the xml content is displayed properly
public class FragmentThree extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.three_layout, container, false);
return rootView;
}
}
Stack trace:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.patryk.android4/com.example.patryk.android4.MainActivity}: java.lang.ClassCastException: com.example.patryk.android4.FragmentOne cannot be cast to android.support.v4.app.Fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.patryk.android4.FragmentOne cannot be cast to android.support.v4.app.Fragment
at android.support.v4.app.Fragment.instantiate(Fragment.java:420)
at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
at com.example.patryk.android4.TabListener.onTabSelected(TabListener.java:30)
at android.support.v7.internal.app.WindowDecorActionBar.selectTab(WindowDecorActionBar.java:634)
at android.support.v7.internal.app.WindowDecorActionBar.addTab(WindowDecorActionBar.java:563)
at android.support.v7.internal.app.WindowDecorActionBar.addTab(WindowDecorActionBar.java:549)
at com.example.patryk.android4.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) at android.app.ActivityThread.access$600(ActivityThread.java:130)
MainActivity.java:34 is this line
actionBar.addTab(tabOne);
(TabListener.java:30) is this one
mFragment = android.support.v4.app.Fragment.instantiate(mActivity, mClass.getName());
Upvotes: 1
Views: 2497
Reputation: 31
First: public class MainActivity extends AppCompatActivity
Second:
private android.support.v4.app.Fragment fragmentOne = new FragmentOne();
private android.support.v4.app.Fragment fragmentTwo = new FragmentTwo();
private android.support.v4.app.Fragment fragmentThree = new FragmentThree();
Third:
public class FragmentThree extends android.support.v4.app.Fragment
public class FragmentOne extends android.support.v4.app.Fragment
public class FragmentTwo extends android.support.v4.app.Fragment
Upvotes: 3
Reputation: 15824
You didn't post the stacktrace, but I already see some mismatch in classes. You use some from the support library: android.support.v4.app.FragmentTransaction
, some from the main set: import android.app.FragmentTransaction;
.
Please check this aspect and make sure you use same classes.
Upvotes: 2