单击按钮时应用程序崩溃

时间:2014-01-02 20:25:17

标签: java android

我是android的新手,我用一些单选按钮创建了一个新活动,但是当我点击按钮时应用程序崩溃并且它给了我一些错误,我试图找到一些关于某些错误的信息,但我可以找不到问题。

这是我的XML代码

  

<TextView 
    android:text="@string/gps_location"
    android:id="@+id/lbl_gps_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText 
    android:id="@+id/txtGpsLocationEntry"
    android:hint="@string/gpsLocationHint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/lblAmountTraffic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:text="@string/amount_traffic"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup 
    android:id="@+id/rbAmountTraffic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

  <RadioButton
      android:id="@+id/rbModerate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/moderate" />

      <RadioButton
      android:id="@+id/rbHeavy"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/heavy" />

  <RadioButton
      android:id="@+id/rbStandstill"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/standstill" />
</RadioGroup>

<TextView
    android:id="@+id/lblDirections"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:text="@string/directions"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup 
    android:id="@+id/rbDirections"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

  <RadioButton
      android:id="@+id/rbNorth"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/north" />

      <RadioButton
      android:id="@+id/rbSouth"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/south" />
</RadioGroup>

<Button 
    android:id="@+id/btnSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/submit" 
    android:onClick="insertTrafficData"/>
 </LinearLayout>

这是我的活动代码

double latitude;
    double longitude;
    LatLng location;
    Geocoder geocoder;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_accident);

        //Get action bar
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true); // Enabling up/ Back navigation

        // show the action bar
        actionBar.show();
        Bundle bundle = getIntent().getExtras();
        latitude = bundle.getDouble("latitude");
        longitude = bundle.getDouble("longitude");

        location = new LatLng(latitude,longitude);

        try {

             List<Address> addresses;
             geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
             addresses = geocoder.getFromLocation(latitude, longitude, 1);

             String address = addresses.get(0).getAddressLine(0);
             String city = addresses.get(0).getAddressLine(1);
             String country = addresses.get(0).getAddressLine(2);

             EditText text = (EditText) findViewById(R.id.txtGpsLocationEntry);
            text.setText(address + " " + city + " " + country);


            TextView text2 = (TextView) findViewById(R.id.lbl_gps_location);
            text2.setText(address + " " + city + " " + country);
            Toast.makeText(getApplicationContext(), address + " " + city + " " + country, 2).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public void insertTrafficData(View view){

        try{

            DBTraffic db = new DBTraffic();

            RadioButton amountTraffic = (RadioButton) findViewById(R.id.rbAmountTraffic);
            RadioButton direction = (RadioButton) findViewById(R.id.rbDirections);

            db.insertTraffic(getApplicationContext(), location, amountTraffic.toString(), direction.toString());
        }catch(Exception e){
            e.printStackTrace();
        }

    }

这些是错误:

01-02 20:05:49.411: E/AndroidRuntime(25558): FATAL EXCEPTION: main
01-02 20:05:49.411: E/AndroidRuntime(25558): java.lang.IllegalStateException: Could not find a method InsertTrafficData(View) in the activity class com.example.maltatraffic.AddTraffic for onClick handler on view class android.widget.Button with id 'btnSubmit'
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.view.View$1.onClick(View.java:3601)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.view.View.performClick(View.java:4107)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.view.View$PerformClick.run(View.java:17160)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.os.Handler.handleCallback(Handler.java:615)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.os.Looper.loop(Looper.java:155)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.app.ActivityThread.main(ActivityThread.java:5536)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at java.lang.reflect.Method.invoke(Method.java:511)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at dalvik.system.NativeStart.main(Native Method)
01-02 20:05:49.411: E/AndroidRuntime(25558): Caused by: java.lang.NoSuchMethodException: InsertTrafficData [class android.view.View]
01-02 20:05:49.411: E/AndroidRuntime(25558):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at java.lang.Class.getMethod(Class.java:915)
01-02 20:05:49.411: E/AndroidRuntime(25558):    at android.view.View$1.onClick(View.java:3594)
01-02 20:05:49.411: E/AndroidRuntime(25558):    ... 11 more

请有人帮助我。

5 个答案:

答案 0 :(得分:1)

我猜你在xml中定义了一个与你的Activity中的方法不匹配的onClick()。您是否在com.example.maltatraffic.AddTraffic中定义了InsertTrafficData(View)?

我仔细观察,看起来像是XML,你使用'InsertTrafficData',但在你的活动中它是'insertTrafficData'。那些必须是相同的,检查你的情况。

答案 1 :(得分:0)

为了避免这种异常,你应该实现onClickListener并覆盖onClick函数,而不是将监听器分配给你的按钮。

答案 2 :(得分:0)

Java在代码中区分大小写,方法以小写字母开头:

insertTrafficData(View)

和XML(我猜):

InsertTrafficData(View)

答案 3 :(得分:0)

您能否分享更多信息?比如整个项目。

看起来像insertTrafficData()的错误。

答案 4 :(得分:0)

我很确定使用XML定义onClick方法存在一些错误问题。我建议两种可能的解决方案:

  • 根据this solution,您应该尝试在Eclipse中清理项目。如果这不起作用,他们说最终解决方案是删除模拟器以使其正常工作。
  • 另一个解决方案是在按钮的活动的onCreate方法中实现onClick处理程序。 An example is shown here.