Error using findViewById method - android

I'm trying to use the findViewById() method to to assign an action to a button I created, however it is giving me the error:
findViewById(int) in android.support.v7.app.AppCompatActivity cannot be applied to (android.widget.Button)
at the lines:
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
My code is as follows:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class UserTypeSelection extends AppCompatActivity implements View.OnClickListener{
private Button buttonStudentAccess;
private Button buttonGuestAccess;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_type_selection);
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
buttonStudentAccess.setOnClickListener(this);
buttonGuestAccess.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view == buttonStudentAccess) {
}
if (view == buttonGuestAccess) {
//startActivity(new Intent(this, MainActivity.class));
}
}
The corresponding xml file contains the buttons
<Button
android:text="Guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonStudentAccess"
android:layout_alignLeft="#+id/buttonStudentAccess"
android:layout_alignStart="#+id/buttonStudentAccess"
android:layout_marginTop="30dp"
android:id="#+id/buttonGuestAccess"
android:layout_alignRight="#+id/buttonStudentAccess"
android:layout_alignEnd="#+id/buttonStudentAccess" />
<Button
android:text="Student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/buttonStudentAccess"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true" />
I used the same syntax to create and assign actions to buttons in my login and registration classes and it didn't create this error. Any help is appreciated, thank you

You need to specify where you're getting the id "buttonStudentAccess" from. so here's how you can find the view:
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
To specify that the button or any other view is located in the "resources" package you refer to it as R and .id specifies that you want to grab the view using its id.

buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);

Related

How to add correctly button listner in fragment [duplicate]

This question already has answers here:
How to handle button clicks using the XML onClick within Fragments
(19 answers)
Closed 5 years ago.
I write code and it look ok.
I dont know what is the reson that I get ERROR when I write setOnClickListener(this)
all I want is to create button in fragment.
"Error:(25, 78) error: incompatible types: void cannot be converted to Button"
"Error:Execution failed for task ':android:compileDebugJava'.
Compilation failed; see the compiler error output for details."
Many Thanks for any Help
the source code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.popthejam.game.android.R;
public class Wellcome extends Fragment implements View.OnClickListener {
private Button btnLOGIN ;
public Wellcome() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wellcome,container,false);
btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
return view;
}
public void onClick(View view) {
Toast.makeText(getActivity(), "LOGIN BUTTON PRESS", Toast.LENGTH_SHORT).show();
}
}
----
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/popthejam"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.Wellcome">
<TextView
android:gravity="center"
android:textSize="#dimen/profile_entry_text_size"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/welcome_to_app" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#android:color/holo_orange_dark"
android:text="Login"
/>
</FrameLayout>
The error is that you're trying to assign the result of setOnClickListener() (void) to your button variable.
So change this
btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
to this
btnLOGIN = (Button) view.findViewById(R.id.btnLogin);
btnLOGIN.setOnClickListener(this);
(Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
Above code return a void function. So :
btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
not working because u register a button by the function.
Let do like #Headcracker suggess
Use this
btnLOGIN = (Button) view.findViewById(R.id.btnLogin);
btnLOGIN.setOnClickListener(this);
And implement the overridden onClick in your fragment class
#Override
public void onClick(View view) {
switch (v.getId()) {
case R.id.btnLogin:
Toast.makeText(getActivity(), "LOGIN BUTTON PRESS", Toast.LENGTH_SHORT).show();
break;
}
}

Android button IllegalStateException with onClick(MainActivity)

Brand new to android. I have a button that is supposed to trigger an onClick method that is an override of the onClick method used with the onClickListener. The problem is that when I press the button an IllegalStateException is thrown saying that the onClick(MainActivity), as defined in the XML of the button, method cannot be found.
The code below is the code that throws the IllegalStateException. However if I change the XML of the button to android:onClick="onClick" then the button works completely fine. Why is this?
Button XML:
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:id="#+id/button2"
android:onClick="onClick (MainActivity)" />
MainActivity Code:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener{
private TextView textView;
private EditText username;
private EditText password;
private Button accept;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView3);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
accept = (Button) findViewById(R.id.button2);
}
public void logIn(View view)
{
Log.i("testUsername", username.getText().toString());
Log.i("testPassword", password.getText().toString());
}
#Override
public void onClick(View view)
{
if((Button)view == accept)
{
Log.i("testUsername", username.getText().toString());
Log.i("testPassword", password.getText().toString());
}
}
}
Why is this?
Because android:onClick="onClick" is correct syntax. None of the following are correct syntax:
android:onClick="onClick (MainActivity)"
android:onClick="onClick (View)"
android:onClick="onClick(View)"
android:onClick="onClick and anything else"
Quoting the documentation for android:onClick:
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
OK, with the help of #HappyRavi on Twitter, I was able to reproduce the problem. It's an IDE bug that you will encounter if you do things in a certain order:
Add the method that you want to route the click event to
Drag the Button into the GUI editor
Click the onClick drop-down in the Properties pane of the GUI editor
Doing things in a different order will not reproduce the problem.
With luck, they can get this fixed in some patch release for Android Studio 2.3.
It should be implemented like below
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:id="#+id/button2"
android:onClick="buttonClickHandler" />//name of method should be given here
And add the method in Activity file
public void buttonClickHandler(View view) {
//handle button click here
}
This will work only when method is added in Activity class and not with Fragment

Cannot resolve symbol issues in making a stopwatch

(Keep in mind i'm very new to coding) I'm having an issue when coding a stopwatch app, I've scavenged this site and found no solution to my issue, comparing my code to another user's who attempted the same tutorial, i found that they are the same, yet I am getting several unresolved symbols when to my knowledge, everything is correct...
Ok, i updated the file with some corrections, now im not having any more errors with start/stop, but i have new errors with the m(start/stop/etc)Listener code...
(Here's the Main Activity Java File)
package com.jackson.eason.stopwatch.;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import your.package.R;
public class MainActivity extends Activity {
Chronometer mChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button;
mChronometer = (Chronometer) findViewById(R.id.Chronometer);
// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
button = (Button) findViewById(R.id.set_format);
button.setOnClickListener(mSetFormatListener);
button = (Button) findViewById(R.id.clear_format);
button.setOnClickListener(mClearFormatListener);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
}
}
(Also, here's my xml file, which Android Developer says is fine)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Chronometer android:id="#+id/chronometer"
android:format="#string/chronometer_initial_format"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0"
android:paddingBottom="30dip"
android:paddingTop="30dip"
/>
<Button android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start">
<requestFocus />
</Button>
<Button android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chronometer_stop">
</Button>
<Button android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset">
</Button>
<Button android:id="#+id/set_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="format">
</Button>
<Button android:id="#+id/clear_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear_format">
</Button>
</LinearLayout>
I appreciate any help anyone can offer!
Your problem is that you are importing the wrong R class. android.R is different from the R file generated for your app and referencing your resources, it is the R file from the framework itself, allowing you to use convenience resources, see this question for an example.
import android.R;
replace that with
import your.package.R;
Also, as Thanos suggested, put your listener declarations inside the onCreate method.
UPDATE :
When I said your.package.R, it was just an example. The package depends on what you chose it to be when creating your project, but judging by the code you provided, you probably need import com.jackson.eason.stopwatch.R
Secondly, you need to declare your listeners first before you use them. If you don't, if you call setOnClickListener(mClearFormatListener) but mClearFormatListener is declared after, the compiler doesn't know about mClearFormatListener yet so it shows a compilation error. The below code should work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get references to views
mChronometer = (Chronometer) findViewById(R.id.Chronometer);
Button button1 = (Button) findViewById(R.id.start);
Button button2 = (Button) findViewById(R.id.stop);
Button button3 = (Button) findViewById(R.id.reset);
Button button4 = (Button) findViewById(R.id.set_format);
Button button5 = (Button) findViewById(R.id.clear_format);
// Declare the listeners
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
// You forgot to declare a listener for set format in your updated code
View.OnClickListener mSetFormatListener = new OnClickListener() {
public void onClick(View v) {
// TODO : set the format
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
// Assign the listeners to your buttons
button1.setOnClickListener(mStartListener);
button2.setOnClickListener(mStopListener);
button3.setOnClickListener(mResetListener);
button4.setOnClickListener(mSetFormatListener);
button.setOnClickListener(mClearFormatListener);
}
One last thing : in the notation mVariable, m stands for "member", meaning a member variable of a class, as opposed to a variable inside a method. Since your listeners are only declared inside a method, we usually don't use mListener but rather listener. Of course, this is just a naming convention and it will not prevent the code to compile and run ;)
I think that all the listeners should be inside a method. Am I wrong? Try placing them inside the onCreate method (at the very end of it).

Android: Simpify many classes and xml

Edit: I cannot get this to work correctly. Probably because I have no idea what I am doing. Anyways, here's my code. If anyone could help, I'd be very grateful: I needs to get it to display the battery mood image for the corresponding mod...
Themes:
package com.cydeon.plasmamodz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Themes extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button Plus = (Button) findViewById(R.id.button1);
Button Blue = (Button) findViewById(R.id.button2);
Plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Themes.this, Bmod.class);
i.putExtra("drawableResource", R.drawable.blue);
Themes.this.startActivity(i);
}
});
Blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent a = new Intent(Themes.this, Bmod.class);
a.putExtra("drawableResource1", R.drawable.plus);
Themes.this.startActivity(a);
}
});
}
}
Bmods:
package com.cydeon.plasmamodz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Bmod extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
ImageView img = (ImageView) findViewById(R.id.iv1);
img.setImageResource(R.drawable.blue);
Intent a = getIntent();
int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
ImageView img1 = (ImageView) findViewById(R.id.iv1);
img1.setImageResource(R.drawable.plus);
}
}
battery(xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bInstall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Install" />
<Button
android:id="#+id/bReturn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Return" />
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="800dp" />
</RelativeLayout>
Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);
Then in that Activity's onCreate():
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);
Don't trust this code to compile, but that's the general idea. Use the same Activity for all of them, pass along the proper resource ID in the intent (e.g. for mod1, send the drawable ID for mod1), then in the activity, check for that resource ID and set it programmatically.
You shouldn't need 50 classes and 50 xml layouts if every one of them does the same thing. Make one activity and one layout. When the user selects something, pass an id of some kind as an Intent extra to the second activity so it can load whatever item is appropriate. I don't know how your data is modeled, but there should be a way to uniquely identify each option (and if there isn't, you should implement one).
Your first activity also doesn't need a button for each item. Use a ListView and an Adapter, and then you just need to provide a layout for one row.
make a single activity only. Inside of it get a reference to your image:
ImageView iv = (ImageView)findViewById(R.id.yourImgId);
Then set the picture to whichever one you want like:
iv.setImageResource(R.drawable.battery_img_1);

Android Null Pointer Exception Button

Ok this may seem like a pointless example but if I can figure this out then the program I am trying to make will work. So I have two activities test and test two each with one button.
Test 1:
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
and test2
package thompson.cameron.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test2 extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
View test = findViewById(R.id.testButton);
test.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.testButton:
System.exit(1);
}
}
}
When I click the button on Test it is supposed to launch test2 however it is at this point I get an null pointer exception that I have narrowed down to test.setOnClickListener(this); line of code. Below are my two xml files for the layout. I can get the button to work when I only have one activity but as soon as I add a second activity with a different layout file it all falls apart
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
I'm still new at Android programming so thanks for all your help.
Open the Debug perspective in Eclipse
Choose the 'Breakpoints' view tab.
Select the 'Add Java Exception Breakpoint' and choose
NullPointerException.
Launch your activity, either by using 'Debug As...' or attaching
the debugger to a running instance via DDMS.
Execute the offending workflow. It will break on the line that
caused the NullPointerException.
In your test.java file give:
implements View.OnClickListener
Initialize your button as:
Button testButton = (Button) findViewById(R.id.testButton);
and inside your onClick method, check whether you are clicking button:
if(v == testButton) {
//give ur intent code
}
There are different ways to perform onClick functionality.
One is the above method which I have mentioned.
Another one is what ankit has mentioned.
Third way is through your layout.
Inside your layout for your button tag, you may give as:
<Button android:id="#+id/testButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Click" android:onClick="onTestButtonClick" />
And inside your class just mention the below details for button:
public void onTestButtonClick(View view) {
//give your intent code
}
You may refer to the link also:
http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html
Make sure that both activities are register at the application's manifest file.
As a side note never call System.exit in your code. You can call finish() to close an Activity and this will bring at the front the previous Activity on the stack.
The issue here is that you haven't typecasted your views to buttons.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // Needed to add this import for the button casting below
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// I have changed View to Button and then typecasted
// with the "(Button)" the return of findViewById
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
Let me know if you have any issues with this. I just completed my first experiment through using the onClickListener implementation through the main class instead of individual anonymous listeners.
Andrew
I had the same problem, you may put the same content view that the button,
setContentView(R.layout.main); if the button is in that content view, in other case, you will put:
setContentView(R.layout.buttoncontentview);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
setContentView(R.layout.main);
sorry for my bad english, but i'm spanish
Implement OnClickListener interface
and set button.setOnClickListener(this);
and override
public void onClick(View v) {
}
I think your buttons IDs need to be different in different activities. R.id.testButton would refer to only one button.
The final solution is that you may modify the AndroidManifest.xml file, i finally solved my error in this link How to register a new activity in AndroidManifest.xml?
You can try this.it may work.
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity{
private Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i=new Intent().setClass(Test.this,Test2.class);
startActivity(i);
}
});
}
}
First of all in main.xml and main2.xml chage the button's ids like in below code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
It throws nullpointerexception because of id confict with each other so in your java file use following code to find button.
In Activity 1
Button button = findViewById(R.id.testButton);
button.setOnClickListener(this);
and
In Activity 2
Button button = findViewById(R.id.testButton1);
button.setOnClickListener(this);

Categories

Resources