java.lang.IllegalStateException: Could not find a method in Android - android

Hi I am developing an Android Application
In which I am using onClick method to call Imageview.
My Code in Java file is:
public void DeleteSendMsg(View view)
{
// My code
}
My XML file is:
<ImageView
android:id="#+id/iv_delete_home_1"
android:layout_width="30px"
android:layout_height="40px"
android:src="#drawable/delete"
android:onClick="DeleteSendMsg"/>
In That I got following Exception:
java.lang.IllegalStateException: Could not find a method DeleteSendMsg(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageView with id 'iv_delete_home_1'
How to Solve following Exception and why it occured please help.
Thanks.

From your layout
<ImageView
android:id="#+id/iv_delete_home_1"
android:layout_width="30px"
android:layout_height="40px"
androidrc="#drawable/delete"
androidnClick="DeleteSendMsg"/>
last two line are wrong:
change
androidrc="#drawable/delete" to android:src="#drawable/delete"
androidnClick="DeleteSendMsg" to android:onClick="DeleteSendMsg"/>

Related

Attempt to invoke virtual method on a null object reference (Android)

I'm trying to make a mini mobile wallet which stores users' details on firebase. I'm getting the error
Attempt to invoke virtual method 'void
android.widget.TextView.setText(int)' on a null object reference
while using the setText() method to show user's balance. I've referred to many other similar questions on stackoverflow, but nothing helped me out.
Here are code files I think I have problem with.
The below code if for a fragment (wallet_info.xml)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PassBook">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="25dp"
android:id="#+id/walletText"
android:text="Wallet Balance"/>
<TextView
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="40dp"
android:layout_below="#id/walletText"
android:id="#+id/balanceText"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/balanceText"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Request Balance"
android:onClick="onRequest"/>
</RelativeLayout>
The android:onClick="onRequest" shows a red underline asking me to create a public method even though I've created the method in the class on which the fragment is attached.
The below code is for home.java (a class on which wallet_info.xml fragment is attached.) I'm pasting the part where I'm getting the error.
public void onRequest(View view){
tv = (TextView)findViewById(R.id.balanceText);
wallet.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
tv.setText(u.getBalance());
//tv.setText(u.getBalance());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The line tv.setText(u.getBalance()); throws the mentioned error.
Thank you in advance.
This is because you can't use the following attribute:
android:onClick="onRequest"
inside a layout for a Fragment. It's only work for an Activity. Hence, you need to bind the balanceText view and create the View.onClickListener for button2 button.
Though questions like this on stackoverflow didn't have the answer I needed, I came up with the solution. I don't know why it shows a NULL POINTER EXCEPTION even when I created an object of TextView. The error got solved by changing the data type of parameter in setText() to string.
The problem got resolved after changing the code as follows.
wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
tv.setText(u.getBalance());
The above lines of code were changed to -
wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
g = String.valueOf(u.getBalance());
tv.setText(g);
This solved my problem.

Facebook SDK button in android studio

I am trying to add a facebook login to my app, in addition to my custom log in.
I added facebook sdk as a module but when I try to add the Facebook Connect button I get an error.
Error:
Rendering Problems The following classes could not be instantiated:
- com.facebook.widget.LoginButton (Open Class, Show Exception)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE Exception Details android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F040001.
Code:
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp" />
First it is possible problem of the Eclipse, you just need to rester environment. To see if help.
Secondary, try to use this code snippet to solve problem.
if (isInEditMode())
{
//do something else, as getResources() is not valid.
} else {
//you can use getResources()
String mystring = getResources().getString(R.string.mystring);
}

Error inflating row layout in a listview

I'm trying to implement flowtextview into bauerca / drag-sort-listview but I'm getting an error inflating a row. Could you help me?
Error line is in (ResourceDragSortCursorAdapter.java (Bauerca)):
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(mLayout, parent, false);
}
Eclipse says:
Ecipse says "FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflataing class com.pagesuite.flowtext.FlowTextView"
The row layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="67dp"
android:orientation="horizontal">
<com.pagesuite.flowtext.FlowTextView
android:id="#+id/nuevo_ingr_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#id/drag_handle"
android:layout_width="wrap_content"
android:layout_height="67dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="10dip"
android:background="#drawable/ic_move" />
<ImageView
android:id="#id/click_remove"
android:layout_width="67dp"
android:layout_height="67dp"
android:layout_alignParentRight="true"
android:layout_marginTop="400dip"
android:padding="10dip"
android:background="#drawable/ic_close" />
</com.pagesuite.flowtext.FlowTextView>
</LinearLayout>
It probably fails to find your com.pagesuite.flowtext.FlowTextView class.
First check if this class is actually in that package? Did you use a library or copy this class into your own project? If you copied it, you'll need to change the package to the correct value aka point it to your own class.
You got that error just because your xml layout file is not recognise the package com.pagesuite.flowtext.FlowTextView
If you change it to right one then error will not be there.
Feel free to comments.
I wrote the FlowTextView class. The project has had quite a few bug fixes and the JAR file hasn't been updated so I would advise against using the JAR file and check the whole source out and use it as a library project.
This means you get the latest version and it will probbly solve your import issue at the same time.
IF you are not familiar with library projects in eclipse then check this

Add Adwhirl in Dialog causes Inflate Exception

i get an Inflate Exception,
android.view.InflateException: Binary XML file line #76: Error inflating class,
while adding AdWhirl Layout in the XML and set this XML as Content-View of the Dialog, i don't know why this exception is occurring and how to resolve it please guide me to solve this issue, i think following code will help u to diagnose that where problem resides:
This is my XML, where i have added Adwhirl layout:
<LinearLayout
android:id="#+id/LinearLayout04"
android:layout_marginBottom="20dip"
android:gravity="center"
android:layout_height="250dip"
android:layout_gravity="center"
android:layout_width="fill_parent"
>
<com.adwhirl.AdWhirlLayout <!-- Exception occurred on this line-->
android:id="#+id/adwhirl_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And in the Java Code:
runOnUiThread(new Runnable() {
#Override
public void run() {
isShowed = true;
dialog = new Dialog(getApplicationContext());
dialog.setContentView(R.layout.replay_screen);//Exception occurred on this line
}
});
I have resolved this issue as now i am adding AdWhirl View by Code.

Clickable image - android

How do i make a image clickable? I have tried some ways, but without success.
Here's the last code i tried (it's clickable but gets error):
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff
}
});
and here's the part from xml:
<ImageView
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickImage"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
When running this code, and clicking the image i get this error:
01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity
HERE'S THE SOLUTION:
The XML:
<ImageButton
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickNew"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#null" />
The code :
public void clickNew(View v)
{
Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
As other said: make this an ImageButton and define its onClick attribute
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:onClick="scrollToTop"
android:src="#drawable/to_top_button"
/>
The image is here encoded in a file res/drawable/to_top_button.png. If the user clicks on the button, the method scrollToTop() is called. This method needs to be declared in the class that sets the Layout with the ImageButton as its content layout.
public void scrollToTop(View v) {
...
}
Defining the OnClick handler this way saves you a lot of typing and also prevents the need to anonymous inner classes, which is beneficial for the memory footprint.
Does an ImageButton do what you want?
The error message you get implies that you do not have a method in your activity that matches your onClick handler.
You should have something like clickImage(View view) in your activity with the click handling implementation.
You could just use the ImageButton class...
http://developer.android.com/reference/android/widget/ImageButton.html
Use a ImageButton ;)
You've set the onclick method to call "clickImage" when the image is clicked in your XML, but you haven't created a clickImage method in your code. You shouldn't need to set the onclick listener at all. Just implement the method from your XML and you should be set.

Categories

Resources