我有一个代码,之前它正在工作,现在它无法正常工作。
public void SetVisibilitySuggestions(int vis)
//4: INVISIBLE, 0: VISIBLE
{
Button b1 = (Button) findViewById(R.id.background1);
b1.setVisibility(vis);
Button b2 = (Button) findViewById(R.id.background2);
b2.setVisibility(vis);
Button b3 = (Button) findViewById(R.id.background3);
b3.setVisibility(vis);
Button b4 = (Button) findViewById(R.id.background4);
b4.setVisibility(vis);
t1.setVisibility(vis);
t2.setVisibility(vis);
t3.setVisibility(vis);
t4.setVisibility(vis);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4 = (TextView) findViewById(R.id.textView4);
t = (EditText) findViewById(R.id.editText);
t.setTypeface(Typeface.createFromAsset(getBaseContext().getAssets(), "fonts/UniversLTStd-Cn.otf"));
SetVisibilitySuggestions(4); //4: INVISIBLE, 0: VISIBLE
.
.
.
SetVisibilitySuggestions函数中的第一个FindViewById无效。可能是什么问题呢?有人提出清理R文件。他说有时他们会变得凌乱。你是如何为初学者做到的?
编辑 :LogCat
06-06 21:40:04.790: E/AndroidRuntime(2840): FATAL EXCEPTION: main
06-06 21:40:04.790: E/AndroidRuntime(2840): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlesearch/com.example.googlesearch.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.os.Looper.loop(Looper.java:137)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-06 21:40:04.790: E/AndroidRuntime(2840): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 21:40:04.790: E/AndroidRuntime(2840): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 21:40:04.790: E/AndroidRuntime(2840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-06 21:40:04.790: E/AndroidRuntime(2840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-06 21:40:04.790: E/AndroidRuntime(2840): at dalvik.system.NativeStart.main(Native Method)
06-06 21:40:04.790: E/AndroidRuntime(2840): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
06-06 21:40:04.790: E/AndroidRuntime(2840): at com.example.googlesearch.MainActivity.onCreate(MainActivity.java:151)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.Activity.performCreate(Activity.java:5104)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-06 21:40:04.790: E/AndroidRuntime(2840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-06 21:40:04.790: E/AndroidRuntime(2840): ... 11 more
XML文件:
<Button
android:id="@+id/background1"
android:layout_width="258px"
android:layout_height="45px"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignLeft="@+id/editText"
android:background="@drawable/sugg_back"
android:clickable="false" />
<Button
android:id="@+id/background2"
android:layout_width="258px"
android:layout_height="45px"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignLeft="@+id/editText"
android:background="@drawable/sugg_back"
android:clickable="false" />
<Button
android:id="@+id/background3"
android:layout_width="258px"
android:layout_height="45px"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignLeft="@+id/editText"
android:background="@drawable/sugg_back"
android:clickable="false" />
<Button
android:id="@+id/background4"
android:layout_width="258px"
android:layout_height="45px"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignLeft="@+id/editText"
android:background="@drawable/sugg_back"
android:clickable="false" />
答案 0 :(得分:3)
在日志中:
java.lang.ClassCastException:无法转换android.widget.TextView 到android.widget.Button
表示您正在尝试将TextView转换为当前代码中的Button。因此,请确保您使用正确的ID来初始化View并清除项目Project->Clean
菜单选项如果您正在使用Eclipse IDE