Runtime error (it cause my application force closed)

407 views Asked by At

This is my logcat

12-02 00:07:37.039: D/AndroidRuntime(13719): Shutting down VM
12-02 00:07:37.039: W/dalvikvm(13719): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-02 00:07:37.039: E/AndroidRuntime(13719): FATAL EXCEPTION: main
12-02 00:07:37.039: E/AndroidRuntime(13719): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pmss/com.pmss.Login}: java.lang.ClassCastException: android.widget.TextView
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.os.Looper.loop(Looper.java:130)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at java.lang.reflect.Method.invokeNative(Native Method)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at java.lang.reflect.Method.invoke(Method.java:507)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at dalvik.system.NativeStart.main(Native Method)
12-02 00:07:37.039: E/AndroidRuntime(13719): Caused by: java.lang.ClassCastException: android.widget.TextView
12-02 00:07:37.039: E/AndroidRuntime(13719):    at com.pmss.Login.onCreate(Login.java:23)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 00:07:37.039: E/AndroidRuntime(13719):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-02 00:07:37.039: E/AndroidRuntime(13719):    ... 11 more

This is my Login.java

package com.pmss;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Login extends ActionBarActivity implements OnClickListener  {

Button login,register;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    register = (Button) findViewById(R.id.register);
    login = (Button) findViewById(R.id.login);
    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, MainMenu.class);
            startActivity(intent);
        }
    });

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, Register.class);
            startActivity(intent);
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

the line 23 in Login.java is this

login = (Button) findViewById(R.id.login);

I really dont know where should I correct it since there is no compilation error. Oh ya before I forgot this is my Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pmss"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.pmss.Login"
        android:label="@string/app_main" >
        <intent-filter android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.pmss.Register"
        android:label="@string/button_register"
        android:parentActivityName="com.pmss.Login" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.Login" />
    </activity>
    <activity
        android:name="com.pmss.MainMenu"
        android:label="@string/title_activity_main_menu"
        android:parentActivityName="com.pmss.Login" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.Login" />
    </activity>
    <activity
        android:name="com.pmss.UserInfo"
        android:label="@string/title_activity_user_info"
        android:parentActivityName="com.pmss.MainMenu" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.MainMenu" />
    </activity>
    <activity
        android:name="com.pmss.TrackParcel"
        android:label="@string/title_activity_track_parcel"
        android:parentActivityName="com.pmss.MainMenu" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.MainMenu" />
    </activity>
    <activity
        android:name="com.pmss.CheckCard"
        android:label="@string/title_activity_check_card"
        android:parentActivityName="com.pmss.MainMenu" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.MainMenu" />
    </activity>
</application>

</manifest>

I really quite frustrated about where to change the programming code in order for it to run again.

This is my activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText" >

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/register"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/password"
    android:layout_marginBottom="26dp"
    android:gravity="center"
    android:text="@string/button_register" />

<TextView
    android:id="@+id/newuser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="14dp"
    android:text="@string/newuser" />

<EditText
    android:id="@+id/userid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignRight="@+id/password"
    android:layout_marginBottom="14dp"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/userid"
    android:layout_centerHorizontal="true"
    android:text="@string/userid"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/password"
    android:layout_centerHorizontal="true"
    android:text="@string/password"
    android:textSize="20sp" />

<Button
    android:id="@+id/login"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/password"
    android:layout_marginTop="14dp"
    android:gravity="center"
    android:text="@string/button_login" />

</RelativeLayout>
1

There are 1 answers

3
SuppressWarnings On BEST ANSWER

If you're coding with Eclipse IDE try going to Projects--> clean, and tick only the project you working with. Sometimes when working with layouts eclipse mess up with the generated R.java and missplaces references, so at compilation gives weird errors regarding class cast exceptions sometimes.