我的应用程序像往常一样打开,但是当我单击TextPlay部分时,会显示以下消息:"不幸的是,(应用程序名称)已停止。" 我不知道该怎么做了,拜托,有人帮帮我!!!
控制台中的错误日志:
12-01 18:36:52.065:E / AndroidRuntime(3069):致命异常:主
12-01 18:36:52.065:E / 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):在android.app.ActivityThread.performLaunchActivity(ActivityThread.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):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
12-01 18:36:52.065:E / AndroidRuntime(3069):在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):在android.app.ActivityThread.main(ActivityThread.java:4424)
12-01 18:36:52.065:E / AndroidRuntime(3069):at java.lang.reflect.Method.invokeNative(Native Method)
12-01 18:36:52.065:E / AndroidRuntime(3069):at java.lang.reflect.Method.invoke(Method.java:511)
12-01 18:36:52.065:E / AndroidRuntime(3069):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
12-01 18:36:52.065:E / AndroidRuntime(3069):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-01 18:36:52.065:E / AndroidRuntime(3069):at dalvik.system.NativeStart.main(Native Method)
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):at com.vhcorrea.test1.TextPlay.onCreate(TextPlay.java:24)
12-01 18:36:52.065:E / AndroidRuntime(3069):在android.app.Activity.performCreate(Activity.java:4465)
12-01 18:36:52.065:E / AndroidRuntime(3069):在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更多
My 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>
答案 0 :(得分:2)
12-01 18:36:52.065:E / AndroidRuntime(3069):引起: java.lang.ClassCastException:android.widget.LinearLayout不能 强制转换为android.widget.EditText
final EditText input = (EditText) findViewById(R.id.Commands);
R.id.Commands
是LinearLayout
而不是EditText
。那
是ClassCastException
的原因。
请确保使用EditText
。
答案 1 :(得分:0)
请查看text.xml文件并查看id命令。根据您的错误,您将线性布局转换为编辑文本
@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 LinearLayout input = (LinearLAyout) findViewById(R.id.Commands);
final TextView display = (TextView) findViewById(R.id.tvResults);
因为你没有更新你的xml文件所以我在猜。一旦更新它。所以我会确定错误在哪里