这是我的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
这是我的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
}
}
Login.java中的第23行是
login = (Button) findViewById(R.id.login);
我真的不知道我应该在哪里纠正它,因为没有编译错误。 哦,之前我忘了这是我的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>
我真的很沮丧在哪里更改编程代码以便再次运行。
这是我的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>
答案 0 :(得分:2)
如果您使用Eclipse IDE进行编码,请尝试转到项目 - &gt;清理,并仅勾选您正在使用的项目。 有时在使用布局时,eclipse会混淆生成的R.java和missplaces引用,因此在编译时会出现有关类别转换异常的奇怪错误。