我有一个代码,它以前可以工作,现在不能工作。
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" />