2015-12-02 38 views
-5

我的应用程序像往常一样打开,但是当我点击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> 

+3

很明显,使用id'R.id.Commands'查看不是'EditText' – Selvin

+2

请发布activity.xml –

+1

您应该沿着此代码发布xml文件,因为它清楚地表明编辑文本有错误 – Sri

回答

2

12-01 18:36:52.065:E/AndroidRuntime(3069): java.lang.ClassCastException:致android.widget.LinearLayout不能 浇铸到机器人.widget.EditText

final EditText input = (EditText) findViewById(R.id.Commands); 

R.id.CommandsLinearLayout不是EditText。那 是ClassCastException的原因。 请确保您使用的是EditText的正确ID。

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文件,所以我居兴。一旦更新它。所以我可以肯定,哪里是错误

相关问题