Problem Onclick + "v" does not recognize the methods in android studio code - android

I am fairly new to programming, I was writing an application code in Android Studio but had a problem
It turns out that when I put this line in my code (In image)
it does not detect it and an error occurs both with "onClick" and with "(v)" I reached this point and I don't know how to solve it
Attached images:
[1]: https://i.stack.imgur.com/huZLu.png Error Code
[2]: https://i.stack.imgur.com/hCJYJ.png All Code
I would really appreciate the answer
This problem is eating my head!
Thank you

it's not a big problem, you can write like this, it will solve your issue
mOpicon2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});

Related

Source not found for the file Activity.class

I have a problem with creating more than one activity in an Android project. The problem occurs only when i'm debugging. It's not a runtime error. I don't have any clue why i still keep getting this error. I already did a lot of research but now I'm a bit desperate because solutions on the web didn't solve the problem.
The error more detailed:
Source not found
Source not found for the file Activity.class error
The source attachment does not contain the source for the file Activity.class
[Button] Change Attached Source
When i click on that button i see the path to the android.jar file:
C:/Program Files/android/platforms/android-19/android.jar
[SOLVED]
This is an good tutorial how to solve this problem.
https://www.youtube.com/watch?v=IdIZ7r2d4J4
Looking at your code i notice a few anomalies.Please use
protected void onCreate()
and not public void onCreate()
I'm not familiar with Java for Android but it looks like your code is trying to use the same UI space for two fullscreen windows.
May be it is something with casing?
Can you be more specific about your problem.
1) Is it a compile error or runtime?
2) What message you got in either case?
Original doc: http://developer.android.com/training/basics/firstapp/starting-activity.html
Simple code for starting second activity by button with id = button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
}

setOnLongClickListener causes UNEXPECTED TOP-LEVEL EXCEPTION

When I add this line of code in my Activity
findViewById(R.id.btFilter).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
ResultadoBuscaMapa.this.finish();
return true;
}
});
I cannot compile, and get this error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.util.ExceptionWithContext
at com.android.dx.util.ExceptionWithContext.withContext(ExceptionWithContext.java:46)
...
in both Intellij IDEA and Android Studio (latest versions)
Funny thing is that it's an old code and worked just fine until yesterday.
Is it possible that Android Studio broke something in my project structure?
Where do you declare this listener ? It's better to do ContextView.findViewById(R.id.btFilter);
And i usually declare my widget before setting a listener:
Button filter = (Button) findViewById(R.id.btFilter);
filter.setOnLongClickListener(....
Another way, do you try without ResultadoBuscaMapa.this.finish(); cause error seems to be link to your context.
Hope it helps
Strange as it my seem, after the last update of Android Studio, the error just vanished in BOTH IDE's. Now it runs fine also in Intellij IDEA

Eclipse Shift+Ctrl+O stopped functioning (Android 4.0.3)

Using Eclipse's Shift+Ctrl+O to organize imports has been working fine until it stopped working.
For some reason, it is now taking away my necessary imports and causing classes to be unresolved. Example below, Button, onClick, Toast are not resolved due to when Shift-Ctrl-O was pressed, it cleaned out the needed file.
It was working before and I don't know what I did to cause it to not work anymore. Any help would be greatly appreciated. I cleaned project, restart eclipse...to no avail.
final Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Perform action on clicks
Toast.makeText(HelloFormStuffActivity.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
Thanks very much in advance.
I have/had the same problem. Best I could figure out is that eclipse need to have a 'source' item on the main tool bar which has has 'options' like format and 'Organize Imports'. Clicking on a project in the "Package Explorer" causes the 'source' menu item to appear between 'refactor' and 'run'.
I had this happen when I created a file without a .java extension and renamed it with Refactor. Not until I noticed there was no syntax highlighting did I realize I still had it open in the text editor, so I closed it and reopened and Ctrl+Shift+O started working again.

How link 2 layout with a Button

I don't speak english very well, so please try to understand me.
iìm trying to develop a little app for android in eclipse. I've create 2 layout where, in first there are a button and in a second there is only the text "helloword". I want that the button to send me at the "helloworld" layout
I followed a tutorial but it's not correct.
Someone can help me posting th code to implement??
Excuse my English still
and thanks for any help
You need to create two different Activities, with separate layouts. You set their layouts using setContentView inside onCreate. Don't forget to include your activities in the manifest (eclipse generated one for you. follow how it was done in the first activity and you should be fine. no need to add the filters and such, just the .Activity)
then in your first activity's button, do this:
my_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, MySecondActivity.class);
startActivty(i);
}
});
If you want to use just 1 activity then use the following code,
my_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
FirstActivity.this.setContentView(R.layout.second_layout);
}
});
Its not so hard just do this:
#Override
public void onClick(View args0) {
setContentView(R.layout.aardelayout);
}
just a simple Search on google and you find your answer .
see this tutorials ( two are in frensh if you are prefering frensh tutorials ) :
tuto1
tuto2
tuto3

ScrollView FOCUS_DOWN, not quite there yet

I apologize for this, and I am sure it's a very rookie question... but I am still trying to grasp java and how everything works (I'm surprised I made it this far.)
I have a TextView inside of a ScrollView and I am trying to get it to focus on the bottom entry each time a new entry is added.
I have this in the code:
getScrollView().post (new Runnable() {
#Override
public void run() {
getScrollView().fullScroll(ScrollView.FOCUS_DOWN);
}
});
that is inside the
public void onClick(View src) {
switch(src.getId()){
case R.id.buttonOk:
(So that when I click ok, it will focus to the bottom.)
now.. I am getting this error: The method getScrollView() is undefined for the type
do i need to call out the name of the scroll view within that first set of code, android:id="#+id/scrollView1"
Again, I am sorry I am so confused on this. I am obviously not getting something right here. Any help is greatly appreciated.
Thanks
you can used this.
scrollview=((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
#Override
public void run() {
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
});
A couple things:
To get access to the view from code, you use (ScrollView)findViewById(R.id.scrollView1) -- getScrollView() doesn't exist, though you can write it using the command I described.
Based on what it sounds like you're trying to do, you might want to look into ListView instead (rather than appending lines to a TextView).

Categories

Resources