I can't find a solution for my problem! I keep getting the error:
onClickListener cannot be resolved to a
type
Please Help!! Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements onClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Try adding this method according to your aim:
#Override
public void onClick(View view)
{
//....
//....
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
onClickListener should be like this OnClickListener
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
}
}
Create a method like addOnClickListener();
and work on it like:
public void addOnActionListener(){
Button send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//...
//...
Related
I am creating an app in android.
In this app i want to show google Map after click on button.
I have declared button and i declared listener.
But my problem is i want to call another activity class in the action listener and show google map.
But it is not working, I changed emulator from google to Android it work but google API is not called and shows me error.
for this i used this code But my Map is not show and my Application is Terminated .
//----------------Main Activity ---------------//
import java.security.PublicKey;
import com.google.android.maps.MapView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
EditText TextFeild;
Button button1;
Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOnListenerAction();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void addOnListenerAction()
{
TextFeild=(EditText) findViewById(R.id.editText1);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(context,MapViwer.class);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
//_-------------------MapViwer Class------------//
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MapViwer extends MapActivity
{
MapView map;
public void onCreate(Bundle setInstanceState)
{
super.onCreate(setInstanceState);
setContentView(R.layout.activity_main);
map=(MapView) findViewById(R.id.mapView);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
So you're saying, you have a class named (MapViwer) that defines a layout and the class (MapViwer) contains information for the map to work. To deploy a class from your MainActivity.class, use the following method.
in MainActivity, under the public class add this
private Button button1;
then add this function -
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), MapViwer.class);
startActivityForResult(i, 100);
}
});
Use it like this..It worked for me..
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MapViewer.class);
startActivity(intent);
}
});
and in the Mapviewer class just call the mapviewer.xml
Now, the mapviewer.xml should look like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="34dp" />
</LinearLayout>
Try it out..
Replace context with Mainactivity.this in the code
Intent intent=new Intent(context,MapViwer.class);
You just only defined intent but didn't call the Mapviewr activity
Correct code:
public void addOnListenerAction()
{
TextFeild=(EditText) findViewById(R.id.editText1);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Mainactivity.this,MapViwer.class);
startActivity(intent);
}
});
}
I was trying to learn how to program with eclipse and I can't figure out how to on click of a button make a new window or just get rid of my current buttons and show others.
main activity.java
`import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
main activity.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
If you want to create a new activity on a button click you could use the below code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mybutton = (Button)findviewbyId(R.id.button1);
mybutton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
package mine.app;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void changeBackgroundColor(View view) {
setContentView(R.layout.color_list);
}
public void setBackgroundRed(View view) {
findViewById(R.layout.activity_main).setBackgroundColor(Color.RED);
setContentView(R.layout.activity_main);
}
}
When the app runs it creates a button that runs changeBackgroundColor. It then shows a button that runs setBackgroundRed which crashes the app.
Create a method in your MainActivity something like....
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
then call this function from your OnClickListener passing in colour whatever you want.
Change Your onCreate as Following
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setBackgroundDrawableResource(R.color.blue);// specify background drawable resource or background color resource here
}
I am new to android and I tried to implement onclicklistener on image view. But it's not working.. Please help. When I click on the image it dose not responds.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
ImageView ad = (ImageView) findViewById(R.id.imageView1);
ad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, ads.class));
}
});
return true;
}
}
this is my code...
I think you should declare your ImageView in onCreate() as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView ad = (ImageView) findViewById(R.id.imageView1);
ad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, ads.class));
}
});
}
You have two problems
First:
startActivity(new Intent(MainActivity.this, ads.class));
The second argument should be an activity
Second:
In your case, the ImageView and the listener should be in your onCreate()
When I had a similar issue, I got it fixed it by adding android:layout_width="44dp" to make sure user would have enough area to click on. Then, you can align image content the way you wannt. In my case:
<ImageView
android:id="#+id/locationImageView"
android:layout_width="44dp"
android:layout_height="16dp"
android:layout_centerVertical="true"
android:layout_marginStart="4dp"
android:layout_toEndOf="#id/locationText"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/icn_edit_small_white"/>
So what I've done in Eclipse, in layouts I have: activity_main.xml and activity_main2.xml. What I tried is to create a button in activity_main.xml and on click to go on screen of activity_main2.xml
so in com.example.myfirstapp I have
MainActivity.Java:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void click1(View v) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(
MainActivity.this,
MainActivity2.class);
startActivity(i);
}
}
MainActivity2.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Write below code in your MainActivity.java file instead of your code.
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mBtn1 = (Button) findViewById(R.id.mBtn1);
mBtn1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
And Declare MainActivity2 into your Androidmanifest.xml file using below code.
<activity
android:name=".MainActivity2"
android:label="#string/title_activity_main">
</activity>
Change your FirstyActivity to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_go=(Button)findViewById(R.id.YOUR_BUTTON_ID);
btn_go.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks","You Clicked B1");
Intent i=new Intent(
MainActivity.this,
MainActivity2.class);
startActivity(i);
}
}
});
}
Hope it will help you.
There is more than one way to do this.
Here is a good resource straight from Google:
http://developer.android.com/training/basics/firstapp/starting-activity.html
At developer.android.com, they have numerous tutorials explaining just about everything you need to know about android. They even provide detailed API for each class.
If that doesn't help, there are NUMEROUS different resources that can help you with this question and other android questions.