2012-11-01 101 views
1

我在布局文件夹中有2个java类和2个xml文件。在location.java类中,我通过位置文件和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> 

位置.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> 

回答

0

只需从布局文件中删除所有标签。看起来你并不知道他们需要什么。 例如,如果您为您的按钮设置android:onClick="Product"标签,则必须在代码中实现此功能才能处理onClick事件。

@Override 
public void Product(){ 
... 
} 

你绝对,如果你在代码中定义onClick回调并不需要在你的布局这样的标签。

相关问题