In Android Studio, when i try to run my project i get a ClassCastException as following:
Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
when the View object is 100% RelativeLayout and not EditText.
The problem occurs on another View element if i comment out the above-related code..
I have tried cleaning and rebuilding the project, i have tried editing the XML file and change the ID, i have tried deleting the XML file and restarting Android Studio and then make a new XML file, all that to no avail.
Thank you for your help.
EDIT
When i add new views to the xml, with id that i never used, somehow the activity before it gets nulls on its own (and totally seperate) xml file.. this is weird because theres no reason why a simple view on a different xml file should impact another xml that is loaded even before it..
EDIT2
I inspected View rl = activityView.findViewById(R.id.notification); and found out that it gets etMessage which is another view element in the same XML and is infact an EditText type view.
The code before the exception occurs:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setInitialView();
}
private void setInitialView() {
RelativeLayout frame = (RelativeLayout) findViewById(R.id.activity_frame);
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View activityView = layoutInflater.inflate(R.layout.chat, null,false);
frame.removeAllViews();
frame.addView(activityView);
chatMap.put("Public", new ArrayList<ChatMessageBox>());
// move the notification layout outside the screen
RelativeLayout rl = (RelativeLayout) findViewById(R.id.notification);
TranslateAnimation moveAnim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
moveAnim.setDuration(0);
moveAnim.setFillAfter(true);
rl.startAnimation(moveAnim);
}
This activity's parent XML, which is loaded with setContentView in its onCreate:
<?xml version="1.0" encoding="utf-8"?>
<!-- Making the drawer layout the root view -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- MAIN CONTENT -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<!-- The ListView to be displayed when the drawer layout is active -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
And the XML that has the appropriate view:
<?xml version="1.0" encoding="utf-8"?>
<!-- Making the drawer layout the root view -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/chat_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:id="#+id/scroller"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#+id/bSendMessage"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/chat_bg" >
<LinearLayout
android:id="#+id/chatWindowContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/bSendMessage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/ic_send" />
<EditText
android:id="#+id/etMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/bSendMessage"
android:layout_toLeftOf="#+id/bSendMessage"
android:paddingLeft="10dp"
android:background="#drawable/text"
android:textColor="#000000"
android:imeOptions="actionNone"
android:inputType="textMultiLine"
android:maxLines="4"
android:singleLine="true"
android:layout_alignParentRight="false"
android:layout_alignWithParentIfMissing="false" />
<!-- the desired element -->
<RelativeLayout
android:id="#+id/notification"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/bg_upload"
>
<TextView
android:id="#+id/tvNotification"
android:layout_gravity="center"
android:text="New Message Recieved"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
</RelativeLayout>
</RelativeLayout>
Error log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.raad/com.example.raad.activities.ChatActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
at com.example.raad.activities.ChatActivity.setInitialView(ChatActivity.java:336)
at com.example.raad.activities.ChatActivity.onCreate(ChatActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
The problem was probably with the cache of the application in the device itself.
I ran the application on a different device and there was no issue, so i cleared the data and cache from the device (Settings -> Apps) and uninstalled the app (just uninstalling didnt work), and now it works.
Change your onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutXmlName);
setInitialView();
}
Edit:
Try changing this line:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.notification);
to
RelativeLayout rl = (RelativeLayout) activityView.findViewById(R.id.notification);
Hope it helps you.
R.id.activity_frame
does not exists in your xml file. Try to resolve this.
And post the complete code.
Related
After updating the android studio to version 3.1.2 & gradle to version 4.4, I've faced weird problem to show snackbar in an activtiy.
here is the code:
#BindView(R.id.layout_coordinator)
CoordinatorLayout coordinatorLayout;
Snackbar.make(coordinatorLayout,"hello", Snackbar.LENGTH_LONG).show();
my XMl layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.LoginActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="70dp"
android:paddingRight="70dp">
<EditText
android:id="#+id/edtMobileNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/txt_RegisterTitle"
android:layout_marginTop="19dp"
android:gravity="center"
android:hint="#string/enterYourMobilePhone"
android:inputType="number"
android:maxLength="11"
android:maxLines="1" />
<Button
android:id="#+id/btn_Confirm"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_below="#+id/edtMobileNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:background="#drawable/button_background_green"
android:text="#string/confirm"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
And this is the errro I've got:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myproject/com.company.myproject.Activities.LoginActivity}: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at android.view.LayoutInflater.inflate(LayoutInflater.java:476)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.support.design.widget.Snackbar.make(Snackbar.java:143)
at com.company.myproject.Activities.LoginActivity.onCreate(LoginActivity.java:98)
as you can see, the error is about inflating layout but can't understand the reason:
Caused by: android.view.InflateException: can be used only with a valid ViewGroup root and attachToRoot=true
It must be mentioned that I've used every solution that are suggested :
my support design version is : implementation com.android.support:design:27.1.1'
the activtiy is extend from AppCompatActivity
The AppTheme is Theme.AppCompat.Light.NoActionBar
I've used getWindow().getDecorView().findViewById(R.id.layout_coordinator), but nothing is solved
be glad someone could help me.
Finally, I've found the solution:
There is a MERGE tag in the snackbar layout that cause exception while inflating layout. By commenting the related row, the problem is solved.
I have tried a lot to find solution. As Same Question found but It doesn't have answer what I want.
I have following XML File:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/add_appointment_parent_layout"
tools:context="com.potionowl.app.activity.AppointmentAddActivity">
<include layout="#layout/layout_toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="#dimen/padding_small"
android:layout_margin="#dimen/padding_medium"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/add_appointment_doctor_spinner"
android:layout_width="match_parent"
android:prompt="#string/string_select_doctor_patient"
android:spinnerMode="dialog"
android:layout_margin="#dimen/padding_medium"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_margin="#dimen/padding_medium"
android:layout_height="wrap_content"
android:baselineAligned="false">
<android.support.design.widget.TextInputLayout
android:id="#+id/add_appointment_date_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="#dimen/padding_very_small"
android:layout_height="wrap_content">
<EditText
android:id="#+id/add_appointment_date_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/string_add_app_date_for_appointment"
android:focusable="false"
android:onClick="PickDate"
android:singleLine="true"
android:inputType="datetime"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/add_appointment_time_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="#dimen/padding_very_small"
android:layout_height="wrap_content">
<EditText
android:id="#+id/add_appointment_time_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/string_add_app_time_for_appointment"
android:focusable="false"
android:onClick="PickTime"
android:singleLine="true"
android:inputType="datetime"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/add_appointment_symptoms_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/padding_medium">
<android.support.design.widget.TextInputEditText
android:id="#+id/add_appointment_symptoms_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:gravity="start"
android:hint="#string/string_add_app_symptoms"
android:lines="5"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_gravity="center"
style="#style/Base.Widget.AppCompat.Button.Colored"
android:layout_margin="#dimen/padding_medium"
android:onClick="addAppointmentButtonClick"
android:layout_height="wrap_content"
android:text="#string/apply_for_appointment"/>
</LinearLayout>
</LinearLayout>
I am getting Errors like:
05-10 23:27:16.942 13455-13455/com.potionowl.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.potionowl.app, PID: 13455
java.lang.IllegalArgumentException: Expected receiver of type com.potionowl.app.activity.AppointmentAddActivity, but got android.support.v7.widget.TintContextWrapper
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4456)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
I don't know why it happens. Is there any solution? Thank you.
This is related to the way AppCompat inflates your layout. Try adding a programmatic onClick listener to your buttons rather than doing it within XML.
There's a discussion about it on Google Code. It's because you are using the AppCompat Buttons.
You could use butterknife annotations instead
#OnClick(R.id.IdForAppCompatButton)
public void doAction() {doSomething();}
In my case the problem was from setting the android:theme attribute to the view, (and also is related to the AppCompat library ... see #Peter Marozzi's answer).
So I simply removed the android: namespace from this line (from the view's style):
<item name="android:theme">#style/someTheme</item>
and make it likes:
<item name="theme">#style/someTheme</item>
and it works fine.
The interactive note is the problem is only on high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both methods (with or without android: namespace) work.
It worked for me to register the listener on the current DialogFragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
dialogView = inflater.inflate(/*your layout*/, container, false);
dialogView.findViewById(R.id.close_button).setOnClickListener(this);
return dialogView;
}
#Override
public void onClick(View v) {
//Handle the button click event here
}
i have the following code block, which i took reference from stackoverflow itself
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:isScrollContainer="true">
<!-- Include Action Bar -->
<include layout="#layout/actionbar_layout" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="900px"
android:scrollbars="vertical"
android:layout_x="0px"
android:layout_y="25px"
android:layout_marginTop="40dp">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="90"
android:layout_alignParentRight="true"
android:scrollbars="vertical"
android:layout_marginTop="22dp"
android:text="Web Designing\n-Rs.30/-\n\nTechno Hunt\n-Rs.30/-\n\nDebugging\n-Rs.30/-\n\nGoogle Me\n-Rs.30/-\n\nCryptography\n-Rs.30/-\n\nQuery Master\n-Rs.30/-\n\nNeed For Speed: Most Wanted\n-Rs.50/-\n\nCounter Strike 1.6\n-Rs.150/-\n\nFifa-15\n-Rs.50/-\n\nBlind Typing\n-Rs.30/-\n\nAsphalt 8: Airborne\n-Rs.50/-\n\nLogo Designing\n-Rs.30/-\n\nPC Assembling\n-Rs.30/-\n\nVirtual Hunting\n-Rs.150/-\n\nRC-Maze\n-Rs.50/-\n\nFootball Skills\n-Rs.50/-\n\nBeg Borrow Steal\n-Rs.60/-\n\nTreasure Hunt\n-Rs.100/-\n\n60 Seconds To Fame\n-Rs.30/-\n\nPlay Fix Win\n-Rs.80/-\n\nPhotography\n-Rs.50/-\n\nChess\n-Rs.50/-\n\nCarrom Singles\n-Rs.30/-\n\nCarrom Doubles\n-Rs.60/-\n\nTable Tennis Singles\n-Rs.40/-\n\nTable Tennis Doubles\n-Rs.80/-\n\nCricket\n-Rs.700/-\n\nRink Football\n-Rs.200/-\n\nTug Of War\n-Rs.250/-\n\nBadminton Singles\n-Rs.50/-\n\nBadminton Doubles\n-Rs.100/-\n\nKabaddi\n-Rs.500/-"/>
</ScrollView>
the Activity looks like this
public class EntryFeeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.entry_fee_layout);
ScrollView scroller=new ScrollView(this);
TextView tv=(TextView)findViewById(R.id.textView1);
scroller.addView(tv);
tv.setMovementMethod(new ScrollingMovementMethod());
}
}
the code looks fine...also it gives a correct preview in the preview pane
image link :http://imgur.com/t7NslEJ
but still i am getting this error on running the app
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manthan.bvimit.manthan16/com.manthan.bvimit.manthan16.EntryFeeActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2373)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5375)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3937)
at android.view.ViewGroup.addView(ViewGroup.java:3787)
at android.widget.ScrollView.addView(ScrollView.java:278)
at android.view.ViewGroup.addView(ViewGroup.java:3728)
at android.widget.ScrollView.addView(ScrollView.java:260)
at android.view.ViewGroup.addView(ViewGroup.java:3701)
at android.widget.ScrollView.addView(ScrollView.java:251)
at com.manthan.bvimit.manthan16.EntryFeeActivity.onCreate(EntryFeeActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6865)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5375)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
and the app crashes when i click on the activity icon
Do it in this way :
Your xml should looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
and then handle it like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.entry_fee_layout);
ScrollView scroller=(ScrollView )findViewById(R.id.scrollView1);
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setMovementMethod(new ScrollingMovementMethod());
}
Try this xml code
use android:layout_width="fill_parent and android:layout_height="fill_parent"
instead of match_parent..
not sure but i hope it will work for you
try it
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:id="#+id/TEXT_STATUS_ID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
</ScrollView>
just add this ScrollView scroller=(ScrollView )findViewById(R.id.scrollView1);
instead ofScrollView scroller=new ScrollView(this); and assign id to ScrollView like android:id="#+id/scrollView1"
Class:
public class EntryFeeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_fee_layout);
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="900px"
android:layout_marginTop="40dp"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="22dp"
android:lines="90"
android:scrollbars="vertical"
android:text="Web Designing\n-Rs.30/-\n\nTechno Hunt\n-Rs.30/-\n\nDebugging\n-Rs.30/-\n\nGoogle Me\n-Rs.30/-\n\nCryptography\n-Rs.30/-\n\nQuery Master\n-Rs.30/-\n\nNeed For Speed: Most Wanted\n-Rs.50/-\n\nCounter Strike 1.6\n-Rs.150/-\n\nFifa-15\n-Rs.50/-\n\nBlind Typing\n-Rs.30/-\n\nAsphalt 8: Airborne\n-Rs.50/-\n\nLogo Designing\n-Rs.30/-\n\nPC Assembling\n-Rs.30/-\n\nVirtual Hunting\n-Rs.150/-\n\nRC-Maze\n-Rs.50/-\n\nFootball Skills\n-Rs.50/-\n\nBeg Borrow Steal\n-Rs.60/-\n\nTreasure Hunt\n-Rs.100/-\n\n60 Seconds To Fame\n-Rs.30/-\n\nPlay Fix Win\n-Rs.80/-\n\nPhotography\n-Rs.50/-\n\nChess\n-Rs.50/-\n\nCarrom Singles\n-Rs.30/-\n\nCarrom Doubles\n-Rs.60/-\n\nTable Tennis Singles\n-Rs.40/-\n\nTable Tennis Doubles\n-Rs.80/-\n\nCricket\n-Rs.700/-\n\nRink Football\n-Rs.200/-\n\nTug Of War\n-Rs.250/-\n\nBadminton Singles\n-Rs.50/-\n\nBadminton Doubles\n-Rs.100/-\n\nKabaddi\n-Rs.500/-" />
</LinearLayout>
</ScrollView>
I receive the error below, mostly on Android 4.1 and 4.4:
java.lang.RuntimeException: Unable to create service se.weblink.weblinkunified.services.FloatingWindowService: android.view.InflateException: Binary XML file line #68: Error inflating class <unknown>
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2406)
at android.app.ActivityThread.access$1700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #68: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at se.weblink.weblinkunified.services.FloatingWindowService.addViewToWindow(FloatingWindowService.java:122)
at se.weblink.weblinkunified.services.FloatingWindowService.onCreate(FloatingWindowService.java:92)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2396)
... 10 more
The addViewToWindow function:
final LayoutInflater layoutInflater = LayoutInflater.from(this);
popupWindow = layoutInflater.inflate(R.layout.window_floating_call, null);
final WindowManager.LayoutParams params = setupLayoutParams();
final View.OnClickListener clickListener = getViewClickListener();
final View.OnTouchListener touchListener = getTouchListener(params);
popupWindow.setOnClickListener(clickListener);
popupWindow.setOnTouchListener(touchListener);
final View overlayView = popupWindow.findViewById(R.id.overlay_view);
overlayView.setOnClickListener(clickListener);
overlayView.setOnTouchListener(touchListener);
final View topContainer = popupWindow.findViewById(R.id.call_top_container);
buttonContainer = (LinearLayout) popupWindow.findViewById(R.id.call_bottom_container);
buttonContainer.addView(getActiveCallButtons());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final Resources resources = getResources();
topContainer.setBackgroundColor(sharedPreferences.getInt(Constants.THEME_COLOR_PRIMARY, resources.getColor(R.color.accent_color_default)));
setupCloseButton();
windowManager.addView(popupWindow, params);
popupWindow.setTranslationY(Tools.getDensityIndependentSize(-120, resources));
popupWindow.setVisibility(View.GONE);
And the xml file "window_floating_call":
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
android:id="#+id/call_top_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#color/accent_color_default"
>
<ImageView
android:id="#+id/contact_image"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_contact_picture"
/>
<TextView
android:id="#+id/contact_initial_label"
android:layout_width="55dp"
android:layout_height="55dp"
android:gravity="center"
android:layout_marginRight="10dp"
android:textSize="25dp"
android:fontFamily="sans-serif-light"/>
<TextView
android:id="#+id/incoming_call_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/contact_image"
android:textColor="#color/text_color_bright"
android:text="#string/incoming_call"
android:textSize="10sp"
android:fontFamily="sans-serif"/>
<TextView
android:id="#+id/contact_name_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/incoming_call_label"
android:layout_marginTop="4dp"
android:layout_toRightOf="#+id/contact_image"
android:textColor="#color/text_color_bright"
android:textSize="18sp"
android:fontFamily="sans-serif"/>
<TextView
android:id="#+id/contact_number_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/contact_image"
android:gravity="top"
android:layout_below="#+id/contact_name_label"
android:fontFamily="sans-serif-light"
android:textSize="14sp"
android:textColor="#color/text_color_bright"/>
<View
android:id="#+id/overlay_view"
android:layout_width="match_parent"
android:layout_height="54dp"/>
<ImageButton
android:id="#+id/close_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_action_cancel"
android:alpha="0.7"
style="#style/Base.Widget.AppCompat.ActionButton"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/call_bottom_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/call_top_container"
android:orientation="vertical"
android:weightSum="1"
>
</LinearLayout>
I can't figure out what causes the error. The problem does not occur on Android 5.1. And I don't seem to get why this code wouldn't be able to run on Android 4.1, 4.2, 4.4.
I appreciate if someone could help me. Thanks in advance!
Forgot to include the android.content.res.Resources$NotFoundException error. But the exception says that the resource file is not a drawable. I have the image ic_action_cancel in all other drawable folders (drawable-hdpi, drawable-mdpi etc.) except the main drawable folder(drawable). I tried adding the .png file to the main folder but it did not work.
I've faced the same problem as yours.I firstly used my inflateUtils that write by myself.
The code is:
public static View inflate(int id) {
return View.inflate(getContext(), id, null);
}
And the getContext() method return a applicationContext. And it occurred InflateExcepition.
And now I change to use View.inflate(context, resId, null), and the context is my activity. And it worked fun now! hope that can help you.
sorry for my poor English!
I have been tweaking my main xml layout, adjusting the different views' positions in my relative layout, and suddenly when I started it on my emulator, it crashed with the message:ClassCastException
My XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<com.example.bingbong.Render
android:id="#+id/render"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/pattern5"
android:clickable="true" />
<TextView
android:id="#+id/fps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="fpasds"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/score_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="asd"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/score_bot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="asfa"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
The error log:
10-30 20:25:33.266: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bingbong/com.example.bingbong.Main_Activity}: java.lang.ClassCastException: android.widget.TextView
...
10-30 20:25:33.266: E/AndroidRuntime(269): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 20:25:33.266: E/AndroidRuntime(269): at com.example.bingbong.Main_Activity.onCreate(Main_Activity.java:26)
The Main_Activity - where line 26 is :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_layout);
r = (Render) findViewById(R.id.render); // <--- Line 26.
r.setView((TextView)findViewById(R.id.fps),(TextView)findViewById(R.id.score_top),(TextView)findViewById(R.id.score_bot));
}
I get this occasionally if I am moving around views or changing the types of views (ie, a TextView change to a Button, or something like that). What usually fixes it for me is to do a clean build.
In the "Project" menu in Eclipse, select "Clean..." and either do all projects or select that particular project.
Yo need clean the project, I got this error frequently