Imagebutton导致崩溃

时间:2014-09-16 17:39:26

标签: java android imagebutton

我正在制作具有登录注销功能的应用。我使用了一个imagebuton来注销但是在读完logcat报告之后登录后崩溃似乎图像按钮导致了它

这是我的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>

这是我的主要活动

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报告

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)

4 个答案:

答案 0 :(得分:2)

您需要展示com.learn2crack.tab.Android.onCreateView,但最有可能的是,您正在执行ImageButton img = (ImageButton) findViewById(<some_textview_id>)之类的操作。确保使用正确的ID(logoutbtn)。

答案 1 :(得分:0)

这是你在这里注销的按钮。根据你的问题,它应该是logoutbtn 因为在你的xml中它是ImageButton所以你应该得到它的参考:

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

onClickListerner应该是这样的:

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

答案 2 :(得分:0)

查看代码后,您在.java代码中使用的ID似乎与.xml中设置的id属性无关。要获得对图像按钮的引用,您需要在相应的.java文件中添加一行代码:

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

行号有助于诊断错误。错误说

android.widget.TextView无法强制转换为android.widget.ImageButton

这让我相信存在一系列代码不匹配的代码,例如:

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

其中findViewById传递文本视图的id,实际上你需要图像按钮的id。

答案 3 :(得分:0)

有点奇怪,虽然我能够通过改变图像来使它工作。我使用带有.ico扩展名的图像而不是使用png我只是没有得到逻辑,虽然有些教程使用了具有相同概念的png,或者只是导致问题的图像本身?