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>
Related
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
I'm using the following library: https://github.com/deano2390/MaterialShowcaseView
I've been trying to find a way (for 2 days straight!) to always position the "GOT IT" button on the bottom left corner of the screen. How do I achieve that simply and effectively?
I will immediately accept the working answer. As you can see, I have a history of always accepting answers.
With Osama's answer, I get error:
com.example.me.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.me.myapplication, PID: 4445
java.lang.IllegalStateException: Cannot start this animator on a detached view!
at android.view.RenderNode.addAnimator(RenderNode.java:812)
at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:300)
at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:282)
at android.animation.RevealAnimator.<init>(RevealAnimator.java:37)
at android.view.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:55)
at uk.co.deanwild.materialshowcaseview.CircularRevealAnimationFactory.animateInView(CircularRevealAnimationFactory.java:29)
at uk.co.deanwild.materialshowcaseview.MaterialShowcaseView.animateIn(MaterialShowcaseView.java:825)
at uk.co.deanwild.materialshowcaseview.MaterialShowcaseView$1.run(MaterialShowcaseView.java:794)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity:
package com.example.me.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// single example
new MaterialShowcaseView.Builder(this)
.setTarget(findViewById(R.id.hi))
.setDismissText("GOT IT")
.setContentText("This is some amazing feature you should know about! Indeed, this is just an example of a bug that can be easily fixed!")
.show();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.me.myapplication.MainActivity">
<TextView
android:id="#+id/hi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.419" />
</android.support.constraint.ConstraintLayout>
This lib is based on ShowcaseView, take a look at it.
You can also go to the showcase_content.xml at the library module and put the tv_dismiss that is the "GOT IT" TextView into a RelativeLayout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_box"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:weightSum="1">
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:paddingLeft="5dp"
android:textColor="#android:color/white"
android:textSize="30dp" />
<TextView
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="#android:color/white"
android:textSize="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/tv_dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:textColor="#android:color/white"
android:textSize="22dp"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
Go to MaterialShowcaseView.java and empty the applyLayoutParams() method and update the setDelay to 1000
Within the builder, add the following code.
mDismissButton.setGravity(Gravity.BOTTOM);
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
}
This is the error message I keep getting when I run my app in the emulator.
There's nothing in the Error Log and it doesn't really tell me where the error is coming from but I get this in the LogCat:
WARN/ActivityManager(51): Launch timeout has expired, giving up wake lock!
WARN/ActivityManager(51): Activity idle timeout for HistoryRecord{44e0fb68 com.gamer.network2/.FirstActivity}
WARN/InputManagerService(51): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#44e98158
ERROR/gralloc(51): [unregister] handle 0x494d08 still locked (state=40000001)
This is my Java for the Layout that comes up when the app is started: (I would think if this is what comes up first, that would be where the problem is, but I really don't know.)
package com.gamer.network2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class FirstActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView next1 = (TextView) findViewById(R.id.signuptext);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Screen2.class);
startActivityForResult(myIntent, 0);
finish();
}
});
}
}
Here's my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gamer.network2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/newlogo2"
android:label="#string/app_name">
<activity android:name=".FirstActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Screen2"></activity>
<activity android:name=".Screen3"></activity>
<activity android:name=".Screen4"></activity>
<activity android:name=".MyOnItemSelectedListener"></activity>
</application>
</manifest>
And the XML code for the layout/main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:weightSum="1"
>
<ImageView android:src="#drawable/newlogo2"
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_marginTop="30dip"
android:layout_height="112dp"
android:layout_weight="0.11"
android:layout_width="135dp"></ImageView>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView2"
android:text="Sign in to GameNet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"></TextView>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView3"
android:text="Username:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dip"
android:layout_marginTop="15dip"></TextView>
<EditText android:id="#+id/EnterUsername"
android:layout_height="wrap_content"
android:layout_width="190dp"
android:layout_gravity="center">
<requestFocus></requestFocus>
</EditText>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView4"
android:text="Password: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dip"
android:layout_marginTop="10dip"></TextView>
<EditText
android:id="#+id/EnterPassword"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_gravity="center"
android:layout_width="190dp"></EditText>
<Button android:text="Sign in"
android:id="#+id/button1"
android:layout_width="121dp"
android:layout_gravity="center"
android:layout_height="wrap_content"></Button>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/signuptext"
android:text="Not a member? Sign up now!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"></TextView>
</LinearLayout>
I can post whatever other code you need to help me. =]
Thanks
i think onClickListener is for buttons only, not textviews