我在从片段中打开一个新活动时遇到麻烦我的活动包含一个地图片段v2 ..它在api 4.0及以上版本上正常工作..但是我在姜饼上面临麻烦2.3.3它给了我一个力量关闭错误。
public class PerxListDetailFragment extends ListFragment {
// Hashmap for ListView
ArrayList<PerxDetailListModel> datalist ;
protected PerxDetailListAdapter adapter;
protected ListView listview;
protected String url;
protected static String id;
protected static String name;
//JSON names
private static final String TAG_businessId = "b_id";
private static final String TAG_businessName = "businessName";
private static final String TAG_businessDescription = "businessDescription";
private static final String TAG_businessLocation = "businessLocation";
private static final String TAG_businessAddress = "businessAddress";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = new Bundle();
bundle = this.getArguments();
SharedPreferences user_data = getActivity().getSharedPreferences("userData", 0);
String bid = user_data.getString("BUSINESSROLEID", null);
id = bundle.getString("id");
name = bundle.getString("name");
url = "http://projects.gaditek.com/mobile/ihirsto/services/getBusiness.php?businessCategoryID="+id +"&businessRole="+bid;
return inflater.inflate(R.layout.perx_list_detail, null, true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView t = (TextView) getView().findViewById(R.id.perx_list_heading);
t.setText(name);
datalist = new ArrayList<PerxDetailListModel>();
listview = getListView();
if(datalist.isEmpty()){
new DownloadData().execute();
}
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// Starting new intent
String venueName = datalist.get(position).getName().toString();
String desc = datalist.get(position).getDescription().toString();
String address = datalist.get(position).getAddress().toString();
String maplong = datalist.get(position).getlongitude().toString();
String maplat = datalist.get(position).getlatitude().toString();
Intent intent = new Intent(getActivity().getApplicationContext(), PerxDetailedActivity.class);
intent.putExtra("heading", name);
intent.putExtra("venueName", venueName);
intent.putExtra("desc", desc);
intent.putExtra("address", address);
intent.putExtra("longitude", maplong);
intent.putExtra("latitude", maplat);
startActivity(intent);
}
});
}
public void addFragment(Fragment fragment, boolean addToBackStack,int transition)
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, fragment, null);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
// =============================
@SuppressLint("NewApi")
public class PerxDetailedActivity extends Activity{
String headingName;String venueName;String desc ;String address;
String latitude;String longitude;
static LatLng HAMBURG;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.perx_detailed);
Intent intent = new Intent();
Bundle extras = getIntent().getExtras();
headingName = extras.getString("heading");
venueName = extras.getString("venueName");
desc = extras.getString("desc");
address = extras.getString("address");
longitude = extras.getString("longitude");
latitude = extras.getString("latitude");
Double longi = Double.parseDouble(longitude);
Double lati = Double.parseDouble(latitude);
HAMBURG = new LatLng(lati, longi);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title(venueName));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
记录
06-01 18:26:16.546: E/AndroidRuntime(6549): FATAL EXCEPTION: main
06-01 18:26:16.546: E/AndroidRuntime(6549): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaditek.ihistro/com.gaditek.ihistro.PerxDetailedActivity}: android.view.InflateException: Binary XML file line #151: Error inflating class fragment
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.os.Looper.loop(Looper.java:143)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.ActivityThread.main(ActivityThread.java:4196)
06-01 18:26:16.546: E/AndroidRuntime(6549): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 18:26:16.546: E/AndroidRuntime(6549): at java.lang.reflect.Method.invoke(Method.java:507)
06-01 18:26:16.546: E/AndroidRuntime(6549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-01 18:26:16.546: E/AndroidRuntime(6549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-01 18:26:16.546: E/AndroidRuntime(6549): at dalvik.system.NativeStart.main(Native Method)
06-01 18:26:16.546: E/AndroidRuntime(6549): Caused by: android.view.InflateException: Binary XML file line #151: Error inflating class fragment
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-01 18:26:16.546: E/AndroidRuntime(6549): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.Activity.setContentView(Activity.java:1702)
06-01 18:26:16.546: E/AndroidRuntime(6549): at com.gaditek.ihistro.PerxDetailedActivity.onCreate(PerxDetailedActivity.java:40)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
06-01 18:26:16.546: E/AndroidRuntime(6549): ... 11 more
06-01 18:26:16.546: E/AndroidRuntime(6549): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.gaditek.ihistro-1.apk]
06-01 18:26:16.546: E/AndroidRuntime(6549): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-01 18:26:16.546: E/AndroidRuntime(6549): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-01 18:26:16.546: E/AndroidRuntime(6549): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
06-01 18:26:16.546: E/AndroidRuntime(6549): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-01 18:26:16.546: E/AndroidRuntime(6549): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
06-01 18:26:16.546: E/AndroidRuntime(6549): ... 21 more
答案 0 :(得分:3)
你好,这是一个兼容性错误:
E/AndroidRuntime(6549): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.gaditek.ihistro-1.apk] 06-01 18:26:16.546:
您尝试在代码中使用Fragment
,但要使Fragments在Gingerbread上运行,您必须从Google导入兼容性库:在此处查看Android Support Library和此{{ 3}}
因此,请确保将Activity
变为FragmentActivity
并使用getSupportFragmentManager()
代替getFragmentManager()
public void addFragment(Fragment fragment, boolean addToBackStack,int transition)
{
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, fragment, null);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
答案 1 :(得分:1)
我只是弄清楚愚蠢的我这里的问题
map =((MapFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap();
应该是
map =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap();
同样如@Glenn所述,活动需要由FragmentActivity而不是Activity扩展 ..因此解决了