The start of the activity is causing my app to crash. It isn't the firts OnClick on the app.
NOT WORKING
public void start_motora(View view){
Intent intent = new Intent(getApplicationContext(), MotorActivity.class);
startActivity(intent);
}
WORKING
public void start_app(View view){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
NOT WORKING XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/white">
<ImageButton
android:onClick="start_motora"
android:id="#+id/motor"
android:layout_width="0dp"
android:layout_height="54dp"
android:layout_marginStart="44dp"
android:layout_marginTop="32dp"
android:background="#drawable/purplebutton"
android:src="#drawable/ic_face_white"
app:layout_constraintEnd_toStartOf="#+id/alfabeto"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text11" />
</androidx.constraintlayout.widget.ConstraintLayout>
WORKING XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
android:background="#ffd500">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/logo_sam"
android:onClick="start_app"
android:outlineAmbientShadowColor="#color/yellow"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="?android:attr/borderlessButtonStyle"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Console Error
The error. I've changed the onClick name and the Activity name, but it's the same problem.
2021-11-03 14:34:03.659 29506-29506/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 29506
java.lang.IllegalStateException: Could not find method teste(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button2'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Help please. The error does not link to some error in my code, i'm so confused
You must register your MotorActivity activity in your manifest.
<activity android:name=".MotorActivity" />
As i can see the above code,
if you are using Activity then you don't
need to use getApplicationContext() instead
of this you should use this.ActivityName or only this
because Context is define for their specific reason like
example if you are using fragment then you can use context
from onAttach and for Apdater you can use application context.
Please share your error without error i am not sure what is the reason of crash in your app.
for reference check this answer : Android button onClick changing activity cause app to crash
and for context description :- https://medium.com/#banmarkovic/what-is-context-in-android-and-which-one-should-you-use-e1a8c6529652
Related
This question already has answers here:
Could not find method in parent or ancestor context
(3 answers)
Closed 4 years ago.
I have a TextView which contains onclick in xml.The onclick works sometimes and sometimes the app crashes.I renamed the TextView in xml to android.support.v7.widget.AppCompatTextView,but it didn't solved the issue.I found similar questions but none of solved mine.
code
public void customerDetails(View view){
Intent intent=new Intent(Customers.this,CustomerDetails.class);
intent.putExtra(Common.CUSTOMERID,view.getTag().toString());
startActivity(intent);
}
My XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:cardMaxElevation="0dp"
app:cardUseCompatPadding="true"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/call_button"
android:layout_toLeftOf="#+id/amount"
android:layout_toRightOf="#+id/call_button"
android:layout_toStartOf="#+id/amount"
android:onClick="customerDetails"
android:text="Customer Name"
android:textColor="#color/colorAccent"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/customer_name"
android:text="ContactPersonName"
android:textColor="#color/primary_text"
android:textSize="12sp"
android:id="#+id/contact_person_name"
android:layout_toLeftOf="#+id/amount"
android:layout_toStartOf="#+id/amount"
android:layout_toRightOf="#+id/call_button"
android:layout_toEndOf="#+id/call_button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Error is
java.lang.IllegalStateException: Could not find method customerDetails(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'customer_name'
You have to use your own class in xml, as standrt TextView classes do not contain your custom method
Try Adding this line to the root tag of the xml:
tools:context="<yourpackaggename.activityname>"
You should add your listeners in your Activity onCreate method. By using lambdas, it won't grow your code more than a line for every listener:
findViewById(R.id.textView).setOnClickListener(v -> customerDetails(v));
And you can change customerDetails method visibility to private now.
This is a better practice, and you will have a better control on your listeners if you need anything more complex.
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've begun learning how to program for Android using Google's tutorials, but I've had a huge problem.
I have a Button which when clicked should switch the user to another screen (Activity), but whenever I run the app, it crashes.
This is the code that should start the next Activity when the Button is clicked:
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.BeginShoppingButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent beginShopping = new Intent(getApplicationContext(), BeginShoppingScreen.class);
startActivity(beginShopping);
}
});
}
This method is called in the onCreate() method so that the listener is active.
This is the XML for the Main Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="eddgroceryapp.cartcourse2.Activities.MainActivity">
<Button
android:text="Begin Shopping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/BeginShoppingButton"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:text="Browse Stores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_below="#+id/BeginShoppingButton"
android:layout_alignParentStart="true"
android:onClick="findAStore"/>
<Button
android:text="Report Locations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="#+id/ReportLocationsButton"
android:layout_below="#+id/BeginShoppingButton"
android:layout_alignParentStart="true"
android:onClick="findAStore"/>
</RelativeLayout>
And this is the XML for the other Activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Choose From Saved Lists"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Choose" />
<Button
android:text="Create A List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Create" />
<Button
android:text="Use Someone Else's List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button4" />
</LinearLayout>
Crash Log:
11-01 13:51:28.166 1345-1345/eddgroceryapp.cartcourse2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: eddgroceryapp.cartcourse2, PID: 1345
android.content.ActivityNotFoundException: Unable to find explicit activity class {eddgroceryapp.cartcourse2/eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1855)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546)
at android.app.Activity.startActivityForResult(Activity.java:4284)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4231)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:4568)
at android.app.Activity.startActivity(Activity.java:4536)
at eddgroceryapp.cartcourse2.Activities.MainActivity$1.onClick(MainActivity.java:58)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10850)
at android.view.View$PerformClick.run(View.java:22523)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-01 13:51:31.951 1345-1345/eddgroceryapp.cartcourse2 I/Process: Sending signal. PID: 1345 SIG: 9
Did you add <activity> tag for BeginShoppingScreen in Manifest.xml like below:
<activity
android:name="your.package.name.BeginShoppingScreen"
android:label="#string/app_name"
/>
EDIT:
As per your logs:
android.content.ActivityNotFoundException: Unable to find explicit
activity class
{eddgroceryapp.cartcourse2/eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen};
have you declared this activity in your AndroidManifest.xml?
Add below code in you Manifest file:
<activity
android:name="eddgroceryapp.cartcourse2.Activities.BeginShoppingScreen"
android:label="#string/app_name"
/>
It seems you forget to declare your activity in Android Manifest file.
So, all activities from your project must be declare in manifest file.
Add below code into your manifest file :
<activity android:name=".BeginShoppingScreen"/>
You can also set other properties like your activity orientation from here, like this :
<activity
android:name=".BeginShoppingScreen"
android:screenOrientation="portrait">
</activity>
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.
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
}