レイアウトフォルダーに2つのJavaクラスと2つのxmlファイルがあります。location.java クラスでは、location ファイルと location.xml ファイルによって setcontentView に 5 つのボタンがありますが、場所の Java クラスでは onClick メソッドが実装されていません。場所タブのボタンをクリックすると、プログラムが停止してしまい、この問題の解決方法がわかりません。Location.java クラスのすべてのボタンに onClick メソッドを実装しますか? 以下にコードを書きます。
私を助けてください
Main.java
public class Main extends Activity {
private WebView mWebView;
private Button mHome;
private Button mProduct;
private Button mPlaces;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.web_view);
mWebView.loadUrl("http://loyaltier.com/app/mobile/code/home/home.php");
mHome = (Button)findViewById(R.id.button_home);
mProduct=(Button)findViewById(R.id.button_product);
mPlaces=(Button)findViewById(R.id.button_places);
mHome.setOnClickListener(onClickListener);
mProduct.setOnClickListener(onClickListener);
mPlaces.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case R.id.button_home:
mWebView.loadUrl("http://loyaltier.com/app/mobile/code/home/home.php");
break;
case R.id.button_product:
mWebView.loadUrl("http://loyaltier.com/app/mobile/design/catalog/catalog1.php");
break;
case R.id.button_places:
Intent i=new Intent(Main.this,Location.class);
startActivity(i);
break;
}
}
};
}
Location.java
public class Location extends MapActivity {
private FrameLayout linear;
private MapView map;
private MapController controller;
private EditText text_location;
double myLatitude;
double myLongitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
initMapView();
initZoomControls();
initMyLocation();
//Not complete this method
text_location.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View view, int keyCode,KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_ENTER){
// The map should shows a location that user writes in edittex.
return true;
}
return false;
}
});
}
@Override
protected boolean isRouteDisplayed() {
// Required by MapActivity
return false;
}
private void initMapView() {
linear = (FrameLayout) findViewById(R.id.frame);
text_location=(EditText)findViewById(R.id.text_location);
map = (MapView) findViewById(R.id.mapView);
controller = map.getController();
map.setSatellite(true);
}
private void initZoomControls() {
View zoomControls = map.getZoomControls();
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL + Gravity.CENTER_HORIZONTAL);
linear.addView(zoomControls, p);
}
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // no effect in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
myLatitude=(overlay.getMyLocation().getLatitudeE6())/1e6;
myLongitude=(overlay.getMyLocation().getLongitudeE6())/1e6;
}
}
Main_Activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="@+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/home_icon"
android:text="@string/button_home"
android:textColor="@color/text_home" />
<Button
android:id="@+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/product_icon"
android:onClick="Product"
android:text="@string/button_product"
android:textColor="@color/text_product" />
<Button
android:id="@+id/button_places"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/places_icon"
android:onClick="Places"
android:text="@string/button_places"
android:textColor="@color/text_places" />
<Button
android:id="@+id/button_rewards"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/rewards_icon"
android:onClick="Rewards"
android:text="@string/button_rewards"
android:textColor="@color/text_rewards" />
<Button
android:id="@+id/button_more"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/more_icon"
android:onClick="More"
android:text="@string/button_more"
android:textColor="@color/text_more" />
</LinearLayout>
</LinearLayout>
location.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="horizontal"
android:background="@color/backgroundTextSearch">
<EditText
android:id="@+id/text_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:lines="1"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="@+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/home_icon"
android:text="@string/button_home"
android:textColor="@color/text_home"
/>
<Button
android:id="@+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/product_icon"
android:onClick="Product"
android:text="@string/button_product"
android:textColor="@color/text_product"
android:layout_toRightOf="@id/button_home"/>
<Button
android:id="@+id/button_places"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/places_icon"
android:onClick="Places"
android:text="@string/button_places"
android:textColor="@color/text_places"
android:layout_toRightOf="@id/button_product" />
<Button
android:id="@+id/button_rewards"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/rewards_icon"
android:onClick="Rewards"
android:text="@string/button_rewards"
android:textColor="@color/text_rewards"
android:layout_toRightOf="@id/button_places"/>
<Button
android:id="@+id/button_more"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/more_icon"
android:onClick="More"
android:text="@string/button_more"
android:textColor="@color/text_more"
android:layout_toRightOf="@id/button_rewards" />
</RelativeLayout>
</FrameLayout>