Reputation: 933
I have a code, it was working before and now it's not working.
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
.
.
.
The first FindViewById in the SetVisibilitySuggestions function is not working. What could be the problem? Someone offered to clean R file. He said that sometimes they go messy. How do you do that for starters?
EDIT: 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 File:
<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" />
Upvotes: 2
Views: 4456
Reputation: 132982
as in Log :
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
means you are trying to cast TextView to Button in current code.so just make sure you are using right id's for initializing View's and also clean project from Project->Clean
menu Option if you are using Eclipse IDE
Upvotes: 3