Reputation: 3
I'm new with android and I have created a new activity with some radio buttons, but when I click on the button the application crash and It give me some errors, I tried to find some information on some errors but I can't find the problem.
This is my XML Code
<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>
This is my activity code
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();
}
}
And these are the errors:
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
Can please someone help me.
Upvotes: 0
Views: 372
Reputation: 596
I'm pretty sure there are a bunch of buggy problems with using the XML to define an onClick method. I would suggest two possible solutions:
Upvotes: 0
Reputation: 4277
Java is case-sensitive in your code the method is starts with lower-case:
insertTrafficData(View)
and on XML you have (I guess):
InsertTrafficData(View)
Upvotes: 0
Reputation: 12169
To avoid this kind of exceptions, you should implement onClickListener
and override the onClick
function, than assign listener to your button.
Upvotes: 0
Reputation: 3847
I'm guessing you're defining an onClick() in your xml that doesn't match a method in your Activity. Did you define InsertTrafficData(View) in com.example.maltatraffic.AddTraffic?
I looked closer, it looks like in XML you use 'InsertTrafficData' but in your activity it's 'insertTrafficData'. Those must be identical, check your case.
Upvotes: 1