I've read everywhere - couldn't find a solution.
My project was working fine, I stated the methods I wanted to be called to the onClick inside the XML file like this:
<RelativeLayout 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:theme="#style/AppTheme"
android:id="#+id/actualMainActivity"
tools:context=".ActualMainActivity"
><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add new noob"
android:id="#+id/addNewNoob"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:onClick="addNewNoob"
android:layout_alignTop="#+id/listOfNoobs"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /></RelativeLayout>
the method inside my java class (ActualMainActivity) was like this:
public void addNewNoob(View view) {
Log.i("Tag", "you clicked the add button");
}
Only after I've implemented the Parse SDK to work with, my Gradle resynced and now when I run the application I get the 'Method not found exception'.
I would like to point out that when I assign an onClickListener directly to the button like this:
final Button a = (Button) findViewById(R.id.addNewNoob);
a.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Log.i("Tag", "you clicked the add button");
}
});
that works perfectly fine...
What have I changed when I installed the SDK ?
How can I get back to the way that I write the methods on XML and they are executed accordingly.
Really confused... thanks in advance
here's the logcat:
02-09 11:29:34.917 2669-2669/com.example.shaked.sqliteexample02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.shaked.sqliteexample02, PID: 2669
java.lang.IllegalStateException: Could not find a method listOfNoobs(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'listOfNoobs'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: listOfNoobs [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:3987)
Thanks in advance
Create a New Project move the old Classes to the New one and Check!
its giving methodexception error
so see to it that the method named listofnoobs() is not missing.
also clean your project and then rebuild it
Related
I was building another larger app and at some point I needed to dynamically change images and my app crashed. So, to test functionality I made another really simple app that just displays a picture with code:
package com.example.imagetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageResource(R.drawable.pugs);
}
}
The error reported by Android Studio is: 03-07 11:30:01.146
20758-20758/? E/Trace: error opening trace file: No such file or
directory (2) 03-07 11:30:01.284 20758-20758/com.example.imagetest
E/dalvikvm: Could not find class
'android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper',
referenced from method
android.support.v4.view.ViewCompat.addOnUnhandledKeyEventListener
03-07 11:30:01.286 20758-20758/com.example.imagetest E/dalvikvm: Could
not find class 'android.view.WindowInsets', referenced from method
android.support.v4.view.ViewCompat.dispatchApplyWindowInsets 03-07
11:30:01.298 20758-20758/com.example.imagetest E/dalvikvm: Could not
find class 'android.view.WindowInsets', referenced from method
android.support.v4.view.ViewCompat.onApplyWindowInsets 03-07
11:30:01.300 20758-20758/com.example.imagetest E/dalvikvm: Could not
find class 'android.view.View$OnUnhandledKeyEventListener', referenced
from method
android.support.v4.view.ViewCompat.removeOnUnhandledKeyEventListener
03-07 11:30:01.306 20758-20758/com.example.imagetest E/dalvikvm: Could
not find class 'android.support.v4.view.ViewCompat$1', referenced from
method
android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener
03-07 11:30:01.354 20758-20758/com.example.imagetest E/dalvikvm: Could
not find class 'android.graphics.drawable.RippleDrawable', referenced
from method
android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
03-07 11:30:01.396 20758-20758/com.example.imagetest E/AndroidRuntime:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagetest/com.example.imagetest.MainActivity}:
android.content.res.Resources$NotFoundException: Resource ID
0x7f060063
and a lot of other stuff. Here's the weird thing: I changed the picture to another folder "mipmap" (before it was in drawable: res/drawable) and the app doesn't crash! But it doesn't show the image either, just nothing on screen. the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Not sure if the image is important, but it was a really cute picture of 3 pugs(Resolution approx 4k) and I added it to drawable just with copy/paste.
Your problem is the wrong placement of resources(images). You are using a device with API below 24 , so resource for API 23 and below is null, because they only API 24 and up.
please put your pugs picture in drawable folder not drawable-v24 , and also don't use a huge image!
imageView.setImageResource(R.drawable.pugs);
You need specify which R it is in the import part.
example:
import com.sample.me.mydraft.R
whenever i hit the imageview i get this error, this happens to me when i switch to test my app on different screen sizes, here is my xml
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dp"
app:srcCompat="#drawable/search"/>
and here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ahmed.electionadmin, PID: 30789
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.electionadmin/com.example.ahmed.electionadmin.Search}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
I have tried so many solutions that asks me to replace src with srccompat and so on, but still is working, ant help would be appreciated
To use srcCompat you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Or you can use src instead of srcCompat to resolve the error.
android:src="#drawable/search"
Replace this:
app:srcCompat="#drawable/search"
With:
android:src="#drawable/search"
Pay attention, not only src with srcCompat, also app with android.
Let me know if it works.
Explanation: You're not using a support library ImageView, but a regular one. app namespace is usually used for support library, same as srcCompat. So you needed to change those 2 things, not just one. That's why merely changing the srcCompat with src yielded no results.
i fixed it by uploading my image inside draawable no 24
you are doing like fitting image to different resolutions , you should have image with different resolutions like hdpi xhdpi , you you can use vector drawable image
I am new to android and following the Udacity Android developer course to learn.
I am on chapter 3 where they teach about the adding the settings in your app. I have added the SettingActivity from android studio. When I click on settings menu, my app crashes.
This is my pref_general.xml file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:key="#string/pref_location_key"
android:defaultValue="#string/pref_location_default"
android:maxLines="1"
android:inputType="text"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/pref_location_label" />
I have made these changes in SettingActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));}
I am getting following exception when I click the Setting menu
Process: com.example.android.sunshine.app, PID: 10131
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.SettingsActivity}: java.lang.RuntimeException: Modern two-pane PreferenceActivity requires use of a PreferenceFragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.RuntimeException: Modern two-pane PreferenceActivity requires use of a PreferenceFragment
at android.preference.PreferenceActivity.requirePreferenceManager(PreferenceActivity.java:1441)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1511)
at com.example.android.sunshine.app.SettingsActivity.onCreate(SettingsActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Any help? Sorry if its very naive thing
I was able to solve this.
I was changing the OnCreate of my SettingActivity instead of changing my OnCreate of SettingFragment which is static private class of SettingActivity.
#mayank agrawal
He meant that the changes are to be done in class which extends PreferenceFragment. i.e you'll have to either extend PreferenceFragment.
Although the code is quite explanatory on the Developers site, here I've taken some part of just to mention what he meant (by putting comments in the code).
public class PreferenceWithHeaders extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
//static class extending PreferenceFragment
public static class Prefs1Fragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getActivity(),
R.xml.advanced_preferences, false);
// Here's where you've to load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences);
}
}
private void private void bindPreferenceSummaryToValue(Preference preference) {
...
}
}
If your app supports API 10 or low then it's mandatory that you use Preference Activity. However you can also use that deprecated way on new API as well. Here's a nice explanation in details for the beginners.
(I too am learning. So if anybody expert find any mistake then please let me know.)
I am running a basic application that passes information between two screens, but I'm crashing as soon as I select to move to the second activity.
Commenting out blocks of code, I have narrowed the problem down to a NULL object.
XML File "activity_two"
<RelativeLayout
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"
tools:context="com.id11434343.exercise2.ActivityTwo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/hintName"
android:id="#+id/textViewName"
android:layout_below="#+id/textViewHeader"
android:layout_marginTop="15dp"/>
Java File "ActivityTwo"
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent parentIntent = getIntent();
String name = parentIntent.getStringExtra(ActivityOne.NAME_MESSAGE);
TextView finalName = (TextView) findViewById(R.id.textViewName);
finalName.setText(name);
}
When I run TextView finalName = (TextView) findViewById(R.id.textViewName);, it passes a null reference. I have a similar line in the first activity, EditText name = (EditText) findViewById(R.id.editTextName);, but experience no problem.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference com.id11434343.exercise2.ActivityTwo.onCreate ActivityTwo.java:34)"
I receive this error, and have tried debug to get a closer look. My EditText name = null, however, the R.id.editTextName = 2131558488, which appears to be a valid ID, in line with the IDs from other elements. I have found a couple of similar questions, on here, but they previously appear to have come down to simple errors, such as using incorrect references, so I double checked my variables, and compared it directly to the working version, to ensure I was not missing something small.
Am I missing something here? I can't understand why I'm having troubles with a ViewText, where I didn't have any problems doing the same thing with an EditText.
This passes null when you use a wrong layout in setContentView() means that the given id doesn't exist in the given layout.
When I open this activity my app crashes and says that it is due to this line of code:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);
The layout for alarm_details_repeat_weekly is right here:
<CheckBox
android:id="#+id/alarm_details_repeat_weekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/alarm_details_label_wednesday"
android:layout_alignBottom="#+id/alarm_details_label_wednesday"
android:layout_alignParentRight="true"
android:checked="true" />
Since some people asked, here is the logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2083)
at minutemovement.mintemovement.com.minutemovement.AlarmDetailsActivity.<init>(AlarmDetailsActivity.java:28)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
Here is the full code: http://pastebin.com/EygRhAj4
You have a typo. You declared your id as alarm_details_repeat_wednesday in your xml but then tried to get an object with id alarm_details_repeat_weekly. Change your code to this:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_wednesday);
Try remove the private,
CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly); or declare the private CheckBox outside oncreate and initialize the object this way: chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);