在片段中,我在RelativeLayout上包含了一个地图和一个按钮。上面的按钮和它下面的地图。
这是片段活动的代码:
module gcd(
input [15:0] a,
input [15:0] b,
input clk,
input rst);
reg [15:0] ra;
reg [15:0] rb;
reg [15:0] gcd;
reg done;
reg [2:0] state;
parameter start = 2'h1;
parameter check = 2'h2;
parameter comp = 2'h3;
parameter lastend = 2'h4;
always @ (posedge clk or posedge rst)
if (rst)
begin
ra <= 16'h0;
ra <= 16'h0;
gcd <= 16'h0;
state <= start;
end
else begin
case(state)
start: //status 0
begin
ra <= a;
rb <= b;
state <= check;
end
check: //Status 1
begin
if ((ra == 16'h0) || (ra == 16'h0))
begin
state <= lastend;
end
else begin
state <= comp;
end
end
comp: //status 2
begin
if(ra > rb) //Compare ra and rb
begin
ra = ra - rb;
if((ra < 16'h0) || (rb < 16'h0)) //Compare ra and rb and if either has become 0
begin
done = 1'h0;
state <= lastend;
end
else begin
state <= comp;
gcd <= ra;
end
end
else if (rb > ra) //Compare ra and rb
begin
rb = rb - ra;
if((ra < 16'h0) || (rb < 16'h0))//Compare ra and rb and if either has become 0
begin
done <= 1'h0;
state <= lastend;
end
else begin
state <= comp;
gcd <= ra;
end
end
else if(ra == rb) //Finally gcd found ra == rb
begin
gcd <= ra;
done <= 1'h0;
state <= start;
end
lastend: //status 3
begin
gcd <= 16'h0;
done <= 1'h0;
end
endcase
end
endmodule
我需要的是点击按钮,然后在地图上显示更多标记。 就像现在一样,应用程序在行
处抛出异常=&gt;这是第107行mostrarButton.setOnClickListener(new OnClickListener(){
09-26 15:34:08.400 1513-1513 / com.solinpromex.elpasojuarezexperience
这是输出异常:
public class Tab2 extends Fragment implements OnMapReadyCallback {
private View view;
private Context mContext;
private double latitud_del_hotel, longitud_del_hotel;
private String nombre_del_hotel;
private static final String url = "..hidden here..";
private ProgressDialog pDialog;
private List<Hotel> hotelList = new ArrayList<Hotel>();
private Button mostrarButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_2, container, false);
mContext = getActivity();
nombre_del_hotel = getActivity().getIntent().getStringExtra("nombre_hotel");
latitud_del_hotel = getActivity().getIntent().getDoubleExtra("latitud_hotel", 0);
longitud_del_hotel = getActivity().getIntent().getDoubleExtra("longitud_hotel",0);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
SupportMapFragment fragment = new SupportMapFragment();
transaction.add(R.id.mapView, fragment);
transaction.commit();
fragment.getMapAsync(this);
mostrarButton = (Button) getActivity().findViewById(R.id.boton_mostrar);
mostrarButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Procesando datos de marcadores...");
pDialog.show();
}
});
return view;
}
@Override
public void onMapReady(GoogleMap map) {
//map is ready
// latitude and longitude
double latitude = latitud_del_hotel;
double longitude =longitud_del_hotel;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(nombre_del_hotel).icon(BitmapDescriptorFactory.fromResource(R.drawable.poi));
// Changing marker icon
// marker.icon(BitmapDescriptorFactory
// .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
map.addMarker(marker);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitud_del_hotel, longitud_del_hotel)).zoom(15).build();
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
我该如何解决这个问题? 谢谢。
注意:在插入setOnclickListener之前,地图会按预期显示。
答案 0 :(得分:1)
你的问题在那一行
mostrarButton =(Button)getActivity()。findViewById(R.id.boton_mostrar);
您无法在Activity
onCreateView()
方法中获得Fragment
,因为片段尚未添加到Activity
。因此,在上面的行中,您可以null
上的Button
引用。
唯一的方法是从onAttach(Activity activity)
的{{1}}(或后来的Fragments lifecicle方法)方法中的活动初始化你的按钮。
参见示例代码:
Fragment
答案 1 :(得分:1)
您收到该错误是因为mostrarButton
为空,这意味着findViewById
未在您的活动中找到R.id.boton_mostrar
。
如果R.id.boton_mostrar
位于R.layout.tab_2
,那么您可以使用
mostrarButton = (Button) view.findViewById(R.id.boton_mostrar);
如果它确实在您的活动布局中,我建议在调用onActivityCreated
之前不要与调用活动进行交互。从理论上讲,活动从onAttach
开始提供,但我发现在onActivityCreated
之前与活动互动是不安全的。