我在布局文件夹中有 2 个 java 类和 2 个 xml 文件。在 location.java 类中,我通过位置文件和 location.xml 文件设置内容视图有 5 个按钮,但在位置 java 类中没有实现 onClick 方法。当我单击位置选项卡中的按钮时,程序已停止,我不知道如何解决此问题?我是否在 Location.java 类中实现所有按钮的 onClick 方法?我在下面写我的代码:
请帮我
主.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;
}
}
};
}
位置.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>
位置.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>