Reputation: 871
I am using this tutorial for reference
I am trying to execute a simple program from the tutorial
MainActivity.java
package com.example.fragmentsfirstproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main,
container, false);
Button nextButton = (Button) view.findViewById(R.id.button1);
nextButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:text="@string/hello_world" />
<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_below="@+id/textView1"
android:layout_marginTop="134dp"
android:text="Click Me !" />
</RelativeLayout>
Apparently in the line::
nextButton.setOnClickListener(this);
I am getting a error
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
Why is this error occurring, can't i use OnclickListener
in
fragments
How can i correct myself
Please go easy on answers . i am new to fragments !
Upvotes: 0
Views: 277
Reputation: 585
Remove the following IMPORT statement..
import android.content.DialogInterface.OnClickListener;
And then in ECLIPSE press Ctrl+Shift+o
Remove the implemented onClick() function.
and again override it.
U'll get the correct function.
Upvotes: 1
Reputation: 886
Try to import..import android.view.View.OnClickListener;
and implement
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
Upvotes: 1
Reputation: 23638
You have added the dialog click listener besides a simple click listener.
Implement the method as below for your Button
Click listener and import import android.view.View.OnClickListener;
:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
Upvotes: 1
Reputation: 14590
You are importing wrong onClickListener
import android.content.DialogInterface.OnClickListener;
which is used for dialogs..
Try to import..import android.view.View.OnClickListener;
and set it to button..
and implement that onClick()
method..
Upvotes: 1