Java RelativeLayout应用程序中的Android片段崩溃
我正在将一个应用程序转换到android上,在碎片方面遇到了一些问题 当我没有Java RelativeLayout应用程序中的Android片段崩溃,java,android,android-fragments,Java,Android,Android Fragments,我正在将一个应用程序转换到android上,在碎片方面遇到了一些问题 当我没有RelativeLayout而只是Fragment时,它工作得很好,但是随着布局(rel或线性)的引入,它崩溃了。我正试图在地图的顶部添加一些按钮,最好是重叠的,而不是在它自己的空间中 event_layout.xml: <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent
RelativeLayout
而只是Fragment
时,它工作得很好,但是随着布局(rel或线性)的引入,它崩溃了。我正试图在地图的顶部添加一些按钮,最好是重叠的,而不是在它自己的空间中
event_layout.xml:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout">
<fragment
android:id="@+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Start service"/>
</RelativeLayout>
EventFragment.java:
public class EventFragment extends FragmentActivity {
private int userIcon, foodIcon, drinkIcon, shopIcon, otherIcon;
private GoogleMap theMap;
private LocationManager locMan;
private Marker userMarker;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.event_layout, container, false);
// now run code below the inflate
if(theMap==null){
//map not instantiated yet
theMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.the_map)).getMap();
}
//theMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//updatePlaces();
theMap.setMyLocationEnabled(true);
theMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(54.328337, -2.743149) ,14) );
JSONArray arrayResultFromURL;
// store the result of the data into the variable
arrayResultFromURL = getJSONfromURL("http://www.websitewebsitestr.org/json.php?f=all&type=dateAndTime");
try{
//Log.d("array_len", String.format("%d", arrayResultFromURL.length()));
for(int i=0;i<arrayResultFromURL.length();i++){
JSONObject json_data = arrayResultFromURL.getJSONObject(i);
// assign attributes from the JSON string to local variables
String id =json_data.getString("id");
String title =json_data.getString("title");
String strap; // define this for the try catch
// this try catch is in case the client does not
// put a strap in, title is mandatory
try{
strap =json_data.getString("strap");
}catch(Exception e){
strap = "";
}
Double lat = (double) 0;
Double lon = (double) 0;
// if the lat and long have not been entered then set them to 0
// otherwise an exception is thrown, and if it is lat and long will
// become 0, putting them down south.
try{
lat = (double) json_data.getDouble("lat");
}catch(Exception e){
lat = (double) 0;
}
try{
lon = (double) json_data.getDouble("long");
}catch(Exception e){
lon = (double) 0;
}
theMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.title(title)
.snippet(strap));
}
}catch (Exception e){
Log.e("log_tag", "error in array: " + e.toString());
}
//Log.d("log_tag", "result: " + res);
//Log.d("log_tag", "point debug!");
//Log.d("myApp", res.toString());
userIcon = R.drawable.common_signin_btn_icon_dark;
/*foodIcon = R.drawable.red_point;
drinkIcon = R.drawable.blue_point;
shopIcon = R.drawable.green_point;
otherIcon = R.drawable.purple_point;*/
//return V;
return inflater.inflate(R.layout.event_layout, container, false);
}
public static JSONArray getJSONfromURL(String url){
//initialize
InputStream is = null;
String result = "";
JSONArray jArray = null;
//http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
//try parse the string to a JSON object
try {
//Log.d("log_tag", "jresult: " + result + "finish");
jArray = new JSONArray(result);
//Log.e("log_tag", "result: " + jArray.toString());
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
public boolean onMarkerClick(Marker marker) {
Log.d("Debug_log", "clicked!!");
return true;
}
}
我曾尝试使用其他stackoverflow帖子中建议的框架,但应用程序要么崩溃,要么被窃听,我无法解决它。在过去的一周里,我在安卓系统中学到了很多东西,我很兴奋将来能学到更多 试试这段代码
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
还有进口
import android.support.v4.app.FragmentActivity;
在你这么做之前
右键单击项目->属性->构建路径->java构建路径->库。。然后单击addexternaljars
去
\android SDK\extras\android\support\v4
然后选择android-support-v4.jar嘿,谢谢你的评论,我已经添加了v4-support jar并按照步骤操作,我在
getSupportFragmentManager
上收到一个错误,说它对于类型EventFragmentextends FragmentActivity是未定义的。。扩展此功能并查看谢谢,这已解决了错误。应用程序仍在崩溃,但会显示ClassCastException
作为错误,您可以发布日志吗?您的概念是错误的…创建选项卡主机活动并将所有活动放在其中
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
import android.support.v4.app.FragmentActivity;