user3508677
user3508677

Reputation: 60

Imagebutton causes crash

I am making an app that has login logout features. I used an imagebuton for logging out but it crashes after logging in after reading the logcat report it seems that the image button causes it

here is my xml

<?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:gravity="center"
    android:orientation="vertical"
    android:background="#0099CC" >

    <TextView
            android:layout_width="125dp"
            android:textColor="#000000"
            android:layout_height="40dp"
            android:text="First Name"
            android:id="@+id/textView3"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:id="@+id/fname"
            android:textColor="#000000"
            android:layout_alignTop="@+id/textView3"
            android:layout_alignParentRight="true"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:text="Last Name"
            android:textColor="#000000"
            android:id="@+id/textView5"
            android:layout_below="@+id/textView3"
            android:textSize="15dp"
            android:layout_toLeftOf="@+id/fname"/>

    <ImageButton
        android:id="@+id/logoutbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logout" />

    <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:id="@+id/lname"
            android:textColor="#000000"
            android:layout_below="@+id/textView3"
            android:layout_toRightOf="@+id/textView5"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:text="E-mail"
            android:textColor="#000000"
            android:id="@+id/textView7"
            android:layout_below="@+id/textView5"
            android:layout_toLeftOf="@+id/textView2"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:id="@+id/email"
            android:textColor="#000000"
            android:layout_below="@+id/textView5"
            android:layout_alignLeft="@+id/fname"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:text="Username"
            android:textColor="#000000"
            android:id="@+id/textView9"
            android:layout_below="@+id/textView7"
            android:textSize="15dp"
            android:textIsSelectable="false"/>

    <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:id="@+id/uname"
            android:textColor="#000000"
            android:layout_below="@+id/email"
            android:layout_alignLeft="@+id/email"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="125dp"
            android:layout_height="40dp"
            android:text="Registered at"
            android:id="@+id/textView11"
            android:textColor="#000000"
            android:layout_below="@+id/textView9"
            android:layout_toLeftOf="@+id/uname"
            android:textSize="15dp"/>

    <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:id="@+id/regat"
            android:textColor="#000000"
            android:layout_below="@+id/textView9"
            android:layout_alignRight="@+id/lname"
            android:textSize="15dp"/>

    <Button
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Back to Login"
            android:id="@+id/login"
            android:textColor="#000000"
            android:layout_below="@+id/textView11"
            android:textSize="13dp"
            android:layout_alignRight="@+id/lname"
            android:layout_alignParentLeft="true"/>

</LinearLayout>

This is my mainactivity

package com.learn2crack.tab;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.learn2crack.library.DatabaseHandler;
import com.learn2crack.tab.Android;


import java.util.HashMap;

public class Android extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) 
    {


            View android = inflater.inflate(R.layout.android_frag, container, false);
            DatabaseHandler db = new DatabaseHandler(getActivity());

            HashMap<String,String> user = new HashMap<String, String>();
            user = db.getUserDetails();

            /**
             * Displays the registration details in Text view
             **/
            final ImageButton logoutbtn = (ImageButton)android.findViewById(R.id.logoutbtn);
            final TextView fname = (TextView)android.findViewById(R.id.fname);
            final TextView lname = (TextView)android.findViewById(R.id.lname);
            final TextView uname = (TextView)android.findViewById(R.id.uname);
            final TextView email = (TextView)android.findViewById(R.id.email);
            final TextView created_at = (TextView)android.findViewById(R.id.regat);
            fname.setText(user.get("fname"));
            lname.setText(user.get("lname"));
            uname.setText(user.get("uname"));
            email.setText(user.get("email"));
            created_at.setText(user.get("created_at"));


            logoutbtn.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                logout();
                }
                });
            return android;}

                public void logout(){
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(Android.this.getActivity());

                alertDialog.setTitle("Logout"); // Sets title for your alertbox

                alertDialog.setMessage("Are you sure you want to Logout ?"); // Message to be displayed on alertbox



                /* When positive (yes/ok) is clicked */
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    Intent login = new Intent(getActivity(), Login.class);
                    login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(login);
                    // Closing dashboard screen
                    getActivity().finish();

                }
                });

                /* When negative (No/cancel) button is clicked*/
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                }
                });
                alertDialog.show();
                }
}

Logcat report

09-16 12:02:21.529: E/AndroidRuntime(1469): FATAL EXCEPTION: main
09-16 12:02:21.529: E/AndroidRuntime(1469): Process: com.learn2crack.tab, PID: 1469
09-16 12:02:21.529: E/AndroidRuntime(1469): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageButton
09-16 12:02:21.529: E/AndroidRuntime(1469):     at com.learn2crack.tab.Android.onCreateView(Android.java:46)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.View.measure(View.java:16497)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.View.measure(View.java:16497)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.View.measure(View.java:16497)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.View.measure(View.java:16497)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.Choreographer.doFrame(Choreographer.java:544)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.os.Handler.handleCallback(Handler.java:733)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.os.Looper.loop(Looper.java:136)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at java.lang.reflect.Method.invoke(Method.java:515)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-16 12:02:21.529: E/AndroidRuntime(1469):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 120

Answers (4)

user3508677
user3508677

Reputation: 60

Kinda weird though I was able to get it working by changing the image. I used an image with the .ico extension instead on using png I just dont get the logic though some tutorials uses png with the same concept or is it just the image itself that causes the problem?

Upvotes: 0

jenndotcodes
jenndotcodes

Reputation: 81

Upon looking at your code, it would seem as though the ids you use in the .java code do not correlate with the id properties that are set in the .xml. To get a reference to your image button, you would have a line of code such as this in the appropriate .java file:

ImageButton button2 = (ImageButton) findViewById(R.id.logoutbtn);

Line numbers would be helpful in diagnosing the error. The error says

android.widget.TextView cannot be cast to android.widget.ImageButton

which leads me to believe that there is a line of code with mismatched types, such as below:

ImageButton logoutButton = (ImageButton) findViewById(R.id.textView1); 

where the findViewById is being passed the id of a text view, when in fact you need the id of an image button.

Upvotes: 0

Rustam
Rustam

Reputation: 6515

this is the button you are doing logout here. according to your question it should be logoutbtn since in your xml it is of ImageButton so you should get it's reference like this:

logoutbtn=(ImageButton)findViewById(R.id.logoutbtn);

and onClickListerner should be like this:

      logoutbtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                  logout();
            }
            });

Upvotes: 0

mbmc
mbmc

Reputation: 5115

You need to show com.learn2crack.tab.Android.onCreateView but most likely, you're doing something like ImageButton img = (ImageButton) findViewById(<some_textview_id>). Make sure you use the correct id (logoutbtn).

Upvotes: 2

Related Questions