我的應用程序像往常一樣打開,但是當我點擊TextPlay部分時出現以下消息:「不幸的是,(應用程序名稱)已停止。」 我不知道該做什麼了,請有人幫助我!有人可以幫我修復此代碼 - Eclipse Android?
在控制檯中的錯誤日誌:
12-01 18:36:52.065:E/AndroidRuntime(3069):致命異常:主
12-01 18:36:52.065:電子/ AndroidRuntime(3069):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.vhcorrea.test1/com.vhcorrea.test1.TextPlay}:java.lang.ClassCastException:android.widget.LinearLayout無法轉換爲android。 widget.EditText
12-01 18:36:52.065:E/AndroidRuntime(3069):at android.app.ActivityThread.performLaunchActivity(Activit yThread.java:1956)
12-01 18:36:52.065:E/AndroidRuntime(3069):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-01 18: 36:52.065:E/AndroidRuntime(3069):at android.app.ActivityThread.access $ 600(ActivityThread.java:123)
12-01 18:36:52.065:E/AndroidRuntime(3069):at android。 app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
12-01 18:36:52.065:E/AndroidRuntime(3069):在android.os.Handler.dispatchMessage(Handler.java:99)
12-01 18:36:52.065:E/AndroidRuntime(3069):在android.os.Looper.loop(Looper.java:137)
12-01 18:36:52.065:E/AndroidRuntime (3069):at android.app.ActivityThread.main(ActivityThread.java:4424)
12-01 18:36:52.065:E/AndroidRuntime(3069):at java.lang.reflect.Method.invokeNative(本機方法)
12-01 18:36:52.065:E/AndroidRuntime(3069):在java.lang.reflect.Method.invoke(Method.java:511)
12-01 18時36分:52.065:E/AndroidRuntime(3069):at co m.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
12-01 18:36:52.065:E/AndroidRuntime(3069):at com.android.internal.os.ZygoteInit 。主要(ZygoteInit.java:551)
12-01 18:36:52.065:E/AndroidRuntime(3069):在dalvik.system.NativeStart.main(本機方法)
12-01 18: 36:52.065:E/AndroidRuntime(3069):引起:java.lang.ClassCastException:android.widget.LinearLayout無法轉換爲android.widget.EditText
12-01 18:36:52.065:E/AndroidRuntime (3069):在com.vhcorrea.test1.TextPlay.onC reate(TextPlay.java:24)
12-01 18:36:52.065:E/AndroidRuntime(3069):在android.app.Activity.performCreate(Activity.java:4465)
12-01在Android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-01 18:36:52。065:E/AndroidRuntime(3069):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-01 18:36:52.065:E/AndroidRuntime(3069):... 11更
我TextPlay.java:
package com.vhcorrea.test1;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
public class TextPlay extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
final Button chkCmd = (Button) findViewById(R.id.bResults);
final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
final EditText input = (EditText) findViewById(R.id.Commands);
final TextView display = (TextView) findViewById(R.id.tvResults);
passTog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(passTog.isChecked()){
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}else{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
chkCmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String check = input.getText().toString();
display.setText(check);
if (check.contentEquals("left")) {
display.setGravity(Gravity.LEFT);
}else if (check.contentEquals("center")) {
display.setGravity(Gravity.CENTER);
}else if (check.contentEquals("right")) {
display.setGravity(Gravity.RIGHT);
}else if (check.contentEquals("green")) {
display.setTextColor(Color.GREEN);
}else if (check.contains("WTF")) {
}else{
display.setText("invalid");
display.setGravity(Gravity.CENTER);
}
}
});
}
}
每個人都表示,錯誤可能會在清單中找到,但我不這麼認爲。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vhcorrea.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.vhcorrea.test1.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.vhcorrea.test1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.vhcorrea.test1.TEXTPLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
很明顯,使用id'R.id.Commands'查看不是'EditText' – Selvin
請發佈activity.xml –
您應該沿着此代碼發佈xml文件,因爲它清楚地表明編輯文本有錯誤 – Sri