美好的一天!请具体帮助我。我已经读过有关解决此问题的方法,但是我看不出如何将其应用于自己。
07-13 18:55:28.339 3850-3850 / com.zheil.zodiac E / Android运行时:致命异常:主要 流程:com.zheil.zodiac,PID:3850 java.lang.RuntimeException:无法实例化活动ComponentInfo {com.zheil.zodiac / com.zheil.zodiac.FirstFragment}: java.lang.ClassCastException:com.zheil.zodiac.FirstFragment不能为 强制转换为android.app.Activity 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2608) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1514) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:163) 在android.app.ActivityThread.main(ActivityThread.java:6221) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 造成原因:java.lang.ClassCastException:com.zheil.zodiac.FirstFragment无法转换为android.app.Activity 在android.app.Instrumentation.newActivity(Instrumentation.java:1079) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2598) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1514) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:163) 在android.app.ActivityThread.main(ActivityThread.java:6221) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
片段:
package com.zheil.zodiac;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment {
private TextView textView2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
textView2 = (TextView) view.findViewById(R.id.textView2);
String textFromActivity = getArguments().getString("1");
textView2.setText(textFromActivity);
return view;
}
}
部分代码的主要活动:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Создадим новый фрагмент
Fragment fragment = null;
Class fragmentClass = null;
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
// Handle the camera action
fragmentClass = FirstFragment.class;
break;
case R.id.nav_gallery:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_slideshow:
break;
case R.id.nav_manage:
break;
case R.id.nav_share:
break;
}
try {
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Вставляем фрагмент, заменяя текущий фрагмент
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
// Выделяем выбранный пункт меню в шторке
item.setChecked(true);
// Выводим выбранный пункт в заголовке
setTitle(item.getTitle());
Bundle bundle = new Bundle();
bundle.putString("1", String.valueOf(getMapLastIntent().get("aries")));
fragment.setArguments(bundle);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
请在您的片段类中添加实例
public static YourFragment newInstance() {
return new YourFragment();
}
答案 1 :(得分:0)
无需创建Class
实例并进行类型转换,只需执行
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
fragment = new FirstFragment();
break;
case R.id.nav_gallery:
fragment = SecondFragment();
break;
case R.id.nav_slideshow:
break;
case R.id.nav_manage:
break;
case R.id.nav_share:
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString("1",String.valueOf(getMapLastIntent().get("aries")));
fragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
item.setChecked(true);
setTitle(item.getTitle());
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}