Android按钮onClick没有反应

时间:2016-06-20 14:20:35

标签: java android eclipse

我知道有很多线程针对这个问题,但没有任何解决方案可以帮助我。

我遇到了以下问题:

我希望我的按钮在开始屏幕上使用我的主要活动中的方法,该方法将TextField中的数据放入Intent并使用该Intent启动一个新的Activity。相同的代码在Android Studio上的工作方式确实有效,但在Eclipse上却没有。没有错误或任何东西,LogCat只给我这个:

  

06-20 14:08:39.003:D / AudioFlinger(1199):调音台(0xb44c0000)节气门结束:节气门时间(68)

我在我的acitivity_main.xml文件中定义了Button,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.jule.bachelorapp.MainActivity">

    <EditText
        android:id="@+id/edit_standort"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_standort"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_zeit"/>
    <Button
        android:id="@+id/button_sendData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_sendData"
        android:onClick="viewAdress"
        />
</LinearLayout>

我的MainActivity.java 文件如下所示:

package com.example.bachelorapp;

import com.example.bachelorapp.R;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    Button button;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void viewAdress(View view){
        EditText editText = (EditText) findViewById(R.id.edit_standort);
        String adresse = editText.getText().toString();
        Uri mapIntentUri = Uri.parse("geo:0,0?q="+ Uri.encode(adresse));
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapIntentUri);
        mapIntent.setPackage("com.google.android.apps.maps");
        if(mapIntent.resolveActivity(getPackageManager()) != null){
            startActivity(mapIntent);
        }
    }
}

Acitivty我想在按下按钮后打开我的MapActivity.java:

package com.example.bachelorapp;

import android.support.v4.app.FragmentActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap map) {
        LatLng adress = new LatLng(48.362206, 10.908704);
        map.addMarker(new MarkerOptions()
                .position(adress)
                .title("Zuhause"));
    }
}

有谁知道,为什么带有android:onClick的按钮没有反应?

我希望你能帮助我,

问候 巨乐

4 个答案:

答案 0 :(得分:0)

如果你想将EditText中的文本传递给Activity,你可以这样做。

public void viewAdress(View view) { 
    Intent mapIntent = new Intent(this, MapActivity.class);
    //Place string as putExtra or place inside a bundle
    mapIntent.putExtra("editTextString", editText.getText().toString());
    startActivity(mapIntent);
}

答案 1 :(得分:0)

答案 2 :(得分:0)

您应该设置您的活动包名称,而不是mapIntent.setPackage("com.google.android.apps.maps")此调用您尚未在设备或模拟器上安装的谷歌地图。

答案 3 :(得分:0)

<Button
    android:layout_width="150dip"
    android:id="@+id/button1"
    android:layout_height="50dip"
    android:text="@string/login"
    android:layout_marginRight="10dip">
</Button>

你必须拨打按钮 - &gt; findViewById()

   Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
            //Do stuff here
    }
});