Its a simple login screen with two edit text one checkbox and a imagebutton ... it was working fine recently i have changed Button to imageButton and now it giving me problem
logcat
04-30 02:11:24.699: E/AndroidRuntime(9571): FATAL EXCEPTION: main
04-30 02:11:24.699: E/AndroidRuntime(9571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.markitberry/com.example.markitberry.Login}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.EditText
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
here is java
public class Login extends Activity implements AnimationListener {
ImageButton btnLogin;
EditText inputEmail,inputPassword;
CheckBox loginRemember;
// Animation
Animation animBounce1,animBounce2,animBounce3,animBounce4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// load the animation
animBounce1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
animBounce2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out_right);
animBounce3 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
animBounce4 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
// set animation listener
animBounce1.setAnimationListener(this);
animBounce2.setAnimationListener(this);
animBounce3.setAnimationListener(this);
animBounce4.setAnimationListener(this);
inputEmail=(EditText)findViewById(R.id.etloginEmail);
inputPassword=(EditText)findViewById(R.id.etloginPassword);
loginRemember=(CheckBox)findViewById(R.id.cbRemember);
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
inputEmail.startAnimation(animBounce1);
inputPassword.startAnimation(animBounce2);
loginRemember.startAnimation(animBounce3);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_call:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9871952704"));
startActivity(callIntent);
// help action
return true;
case R.id.action_email:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","markitberry#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "message");
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
case R.id.action_locate:
Intent i = new Intent(Login.this, Locate.class);
startActivity(i);
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (animation == animBounce3) {
Intent it=new Intent(Login.this,Home.class);
startActivity(it);
}
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
}
login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_bck"
android:gravity="center">
<include
layout="#layout/login_cover"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_cover.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:background="#drawable/login_gradient_bck"
android:orientation="vertical" >
<EditText
android:id="#+id/etloginPassword"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/cbRemember"
android:layout_alignLeft="#+id/etloginEmail"
android:layout_alignRight="#+id/etloginEmail"
android:layout_marginBottom="26dp"
android:background="#BFFFFFFF"
android:ems="10"
android:gravity="center"
android:hint="Enter Password"
android:inputType="textPassword"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/etloginEmail"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/etloginPassword"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:layout_marginBottom="36dp"
android:background="#BFFFFFFF"
android:ems="10"
android:gravity="center"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:textColor="#FFFFFF" />
<CheckBox
android:id="#+id/cbRemember"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/btnLogin"
android:layout_alignLeft="#+id/etloginPassword"
android:layout_alignRight="#+id/etloginPassword"
android:layout_marginBottom="62dp"
android:text="Remember Me"
android:textColor="#E6E6E6" />
<ImageButton
android:id="#+id/btnLogin"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="#drawable/button"
android:src="#drawable/login_icon_2" />
</RelativeLayout>
Maybe you're having this problem because you didn't change your Button control to ImageButton in your view (login.xml). If that's not the case, you should clean your project and build it again, the R.java file sometimes does not refresh it self with the new changes (this issue is kind of weird, but sometimes it happens).
Just for the record... when this kind of problem happens and you have your code with no errors, it's because you changed the position of your control (For example... your Button was at the beginning but after a while you moved it to the end of the layout) for some reason the project doesn't realize this change and still thinks your Button is the first control in the layout.
Sorry to write this suggestion in an answer... I don't have enough points to comment in your question.
Button extends TextView while ImageButton extends ImageView. Kind of wierd, but that's Android for you.
I would suggest possibly using Button then using android:background="#drawable/MyDrawable" and see if that will achieve what you are trying to do.
You are using the ImageButton two times in you class:
ImageButton btnLogin;
And;
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
Please remove the ImageButton btnLogin; if you are using the ImageButton somewhere else..
Secondly, note that you are not registering the ImageButton anywhere in the onCreate()..
My suggestion, just remove the ImageButton btnLogin; before the onCreate() and try it.
Or change this :
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
to:
btnLogin =(ImageButton) findViewById(R.id.btnLogin);
Should be working now.
Related
I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}
Re-Edit: For those interested the method for onClick was resolved below. Turns out you cannot create separate classes for controls so the button controls need to be inside the current active class.
The issue: In my fragment I have included several controls, one of which is a button that should open a new activity. When clicking the button I get the following crash...
This is the LogCat error when clicking the button in my current active Fragment
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paymentdemo, PID: 14352
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_next_trans'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This is the fragment hosting the controls
<?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"
android:padding="16dp">
<TextView
android:id="#+id/transaction_header"
style="?textGroupheader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="20dp"
android:text="#string/group_header"
android:textStyle="bold" />
<!-- Spinner displays accounts available using SpinnerActivity -->
<include
layout="#layout/fragment_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Radio checks on or off. Still need to link selected as fragments for button on click behaviour -->
<include
layout="#layout/fragment_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Button will take selected radio and open related activity, for now we just go to MainActivity -->
<include
layout="#layout/fragment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The button control code
<FrameLayout 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"
tools:context=".ButtonActivity">
<Button
android:id="#+id/button_next_trans"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#drawable/action_button"
android:paddingStart="24dp"
android:paddingLeft="24dp"
android:paddingTop="8dp"
android:paddingEnd="24dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:text="#string/next"
android:textColor="#color/colorWhite"
android:onClick="goToPayments"/>
</FrameLayout>
The button activity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class ButtonActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_button);
}
public void goToPayments(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
I'm not experienced so my implementation may be a bit rusty. If you have any advice on how to properly implement a button onClick method I would be grateful.
You need to declare the button and View for framgment
Like this
Button button;
public View myView;
Then in onCreate initialize like this
myView = inflater.inflate(R.layout.fragment_button,container,false);
button = (Button) myView.findViewById(R.id.button_next_trans);
then create method like this this,
public void payments(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),MainActivity.class)
startActivity(i);
}
}
}
And at end of onCreate() method return view.
return myView;
And call payments(); in onCreate
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_button);
Button btnPay = (Button)findViewById(R.id.button_next_trans);
btnPay.setOnClickListener(this);
}
public void goToPayments(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
#Override
public void onClick(View v) {
{
switch (v.getId()) {
case R.id.button_next_trans:
goToPayments();
break;
}
}
}}
Remove below line from xml
android:onClick="goToPayments"
I have these declarations for 4 ImageButtons on my Android app, but so far they aren't clickable, this is my class:
public class WelcomeScreen extends Activity {
ImageButton completeprofile;
ImageButton gotoportfolio;
ImageButton findfriends;
ImageButton readnews;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity);
completeprofile = (ImageButton) findViewById(R.id.completeprofile);
gotoportfolio = (ImageButton) findViewById(R.id.gotoportfolio);
findfriends = (ImageButton) findViewById(R.id.findfriends);
readnews = (ImageButton) findViewById(R.id.readnews);
completeprofile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, ProfileMember.class);
startActivity(i);
}
});
gotoportfolio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, PortfolioMember.class);
startActivity(i);
}
});
findfriends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
startActivity(i);
}
});
readnews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, WebActivity.class);
startActivity(i);
}
});
} }
This is my layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:layout_marginLeft="105dp">
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="#+id/completeprofile"
android:background="#drawable/completeprofile"
android:layout_marginLeft="75dp"
android:clickable="true" />
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="#+id/gotoportfolio"
android:background="#drawable/gotoportfolio"
android:layout_marginLeft="65dp"
android:clickable="true" />
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="#+id/findfriends"
android:background="#drawable/findfriends"
android:layout_marginLeft="65dp"
android:clickable="true" />
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="#+id/readnews"
android:background="#drawable/readnews"
android:layout_marginLeft="65dp"
android:clickable="true" />
</LinearLayout>
They show perfectly, but so far I cannot click any of them, no stacktrace error, I'm quite puzzled about it =/
Anybody can shed some light on this?
Thanks in advance!
You are using PNG images as background. You should use the android:src attribute instead of android:background to get the touch feedback when the button is being clicked. If you want to change the background you must use an XML drawable selector. See the docs
This Work fine to me.
Use This in Xml
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_marginBottom="48dp"
android:onClick="AddInfo"
android:background="#mipmap/ea_logo"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
Use This in Activity Class
public void AddInfo(View view) {
///// Use Your Own code /////////
}
I recently started developing apps, and I've gotten my first gig. It's a simple app with a logo and a button linking to a website. Everytime I run it, it crashes, and I'm stumped as to why, as it's such a simple program. I've spent a lot of time looking for similar problems on SO, but to no avail. I've also gone through eclipse and eliminated any compiler warnings. Does anyone have an idea as to what could have gone wrong?
Error messages:
I've kept cutting things out over and over to the app's most simple function. It still won't work. Here is my .java and main activity. (It's a one activity and one class app)
//necessary imports are omitted. I get no errors for imports
public class FullscreenActivity extends Activity {
Button button = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goToUrl("http://switchemup.com");
}
});
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fullscreen, menu);
return true;
}
}
<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=".FullscreenActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:src="#drawable/switchuplogo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="94dp"
android:text="View Website" />
</RelativeLayout>
This line Button button = (Button) findViewById(R.id.button1); should create an error and this should cause the crash you app. You set a variable which contains a method that should be in onCreate method.
Try this instead:
// init your variable only
Button button;
// Then in onCreate, as you already did:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// set findViewById method only here
button = (Button) findViewById(R.id.button1);
// ...
}
Let me know if this helps.
I have a "Menu" imageview in all the pages of my app, if clicked on menu, all the menuitems like login, home, jobs, about will get open and If i click on "about" menuitem the app gets force close and getting null pointer exception on "OnClicklistener of menu" but other Menuitems are not having this issue, please can anybody solve this?
public class About extends Activity {
LinearLayout line1, line2;
ImageView menu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});
ImageView home = (ImageView) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Home.class));
}
});
ImageView jobs = (ImageView) findViewById(R.id.jobs);
jobs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Jobs.class));
}
});
ImageView log = (ImageView) findViewById(R.id.log);
log.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Login.class));
}
});
ImageView about = (ImageView) findViewById(R.id.about);
about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(getApplicationContext(), About.class));
}
});
}
XML file
<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:background="#color/black" >
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/menu" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:background="#color/black"
android:layout_toRightOf="#+id/ll1"
android:visibility="gone"
>
<ImageView
android:id="#+id/about"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/jobs"
android:src="#drawable/about" />
<ImageView
android:id="#+id/jobs"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/jobs" />
<ImageView
android:id="#+id/log"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/log" />
<ImageView
android:id="#+id/home"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/home" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#color/white"
android:textColor="#color/white"/>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="710dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="46dp"
android:text="#string/AboutPage"
android:textColor="#color/white" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll1"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="#string/WelcometoRebuixcom"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
ImageView menu;
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
Look at these code lines, the Second one is cause of Force Close, as menu is NULL.
Just interchange it like,
menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
Update:
You are declaring ImageView menu;
Now the second line, menu.setOnClickListener(new View.OnClickListener() {
without defining ImageView menu you are setting setOnCLickListener() to it, which cause NullPointerException.
So you have to define ImageView menu like menu = (ImageView)findViewById(R.id.menu); after declaring.
Just go through for basic Android Programming and Core Java tutorial for How to declare and define Objects and Member Variables. As I think you have poor programming concepts.
you should move the code
ImageView menu = (ImageView)findViewById(R.id.menu);
before
menu.setOnClickListener(new View.OnClickListener() {}
try this
ImageView menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {............
you should put
ImageView menu = (ImageView)findViewById(R.id.menu);
befor
menu.setOnClickListener
Initialize your imageview befor onclick listener as below:
ImageView menu = (ImageView)findViewById(R.id.menu); <<---- Here
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});