• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

First app problems (after hello world)

Karæthon

Newbie
Hello all, I am new to app development and Java, I have studied Oracle's documentation and taken the time to practice with the w3schools tutorial. I have a basic understanding of how Java is put together and have written a few basic test programs to validate my understanding, I am now TRYING to write an Android app and am having trouble... I am using AIDE and have the following code:
Code:
MAINACTIVITY.JAVA
package com.splitinfinitivesoftware.pve;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity 
{
	private ImageButton btnSettings;
	private EditText inputFilePath;
		
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		btnSettings = findViewById(R.id.btnSettings);
		inputFilePath = findViewById(R.id.txtInputFilePath);
		}
		public void btnSettings_onClick(boolean f){
	 		//btnSettings.setImageResource(R.drawable.key);
	}
}
Code:
MAIN.XML
<?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:gravity='bottom|center_horizontal'
	android:id='@+id/layMainBody'
	android:orientation='vertical'>

	<LinearLayout
		android:orientation='vertical'
		android:layout_width='match_parent'
		android:layout_height='match_parent'
		android:layout_weight='100'
		android:id='@+id/layMainPage'
		android:gravity='center_vertical|center_horizontal'>

		<LinearLayout
			android:orientation='vertical'
			android:layout_width='match_parent'
			android:layout_height='wrap_content'
			android:id='@+id/layInputFileControls'>

			<EditText
				android:layout_width='match_parent'
				android:ems='10'
				android:layout_height='wrap_content'
				android:id='@+id/txtInputFilePath'/>

			<Button
				android:layout_width='wrap_content'
				android:layout_height='wrap_content'
				android:text='Button'
				android:layout_gravity='right'
				android:id='@+id/btnInputFileBrowse'/>

		</LinearLayout>

	</LinearLayout>

	<LinearLayout
		android:orientation='horizontal'
		android:layout_width='match_parent'
		android:layout_height='wrap_content'
		android:id='@+id/layButtonBar'
		android:gravity='center_horizontal'>

		<ImageButton
			android:layout_width='65dp'
			style='?android:attr/buttonBarButtonStyle'
			android:layout_height='65dp'
			android:src='@drawable/settings'
			android:scaleType='centerInside'
			android:scaleX='1.5'
			android:scaleY='1.5'
			android:id='@+id/btnSettings'
			android:onClick='btnSettings_onClick'/>

		<ImageButton
			android:layout_width='65dp'
			style='?android:attr/buttonBarButtonStyle'
			android:layout_height='65dp'
			android:src='@drawable/save_new'
			android:scaleType='centerInside'
			android:scaleX='1.5'
			android:scaleY='1.5'
			android:id='@+id/btnFileSystem'/>

		<ImageButton
			android:layout_width='65dp'
			style='?android:attr/buttonBarButtonStyle'
			android:layout_height='65dp'
			android:src='@drawable/start'
			android:scaleType='centerInside'
			android:scaleX='1.5'
			android:scaleY='1.5'/>

		<ImageButton
			android:layout_width='65dp'
			style='?android:attr/buttonBarButtonStyle'
			android:layout_height='65dp'
			android:src='@drawable/key'
			android:scaleType='centerInside'
			android:scaleX='1.5'
			android:scaleY='1.5'
			android:id='@+id/btnSecurity'/>

		<ImageButton
			android:layout_width='65dp'
			style='?android:attr/buttonBarButtonStyle'
			android:layout_height='65dp'
			android:src='@drawable/help'
			android:scaleType='centerInside'
			android:scaleX='1.5'
			android:scaleY='1.5'
			android:id='@+id/btnHelp'/>

	</LinearLayout>

</LinearLayout>
Code:
ANDROIDMANIFEST.XML
<?xml version='1.0' encoding='utf-8'?>
<manifest xmlns:android='http://schemas.android.com/apk/res/android'
    package='com.splitinfinitivesoftware.pve' >

    <application
        android:allowBackup='true'
        android:icon='@drawable/ic_launcher'
        android:label='@string/app_name'
        android:theme='@style/AppTheme'
		android:resizeableActivity = 'true'>
        <activity
            android:name='.MainActivity'
            android:label='@string/app_name' >
            <intent-filter>
                <action android:name='android.intent.action.MAIN' />

                <category android:name='android.intent.category.LAUNCHER' />
            </intent-filter>
        </activity>
    </application>

</manifest>

The problem is, when I run my app to test it crashes. In the log there is this...
Code:
01-14 16:38:34.673 16557 16557 D   AndroidRuntime                               Shutting down VM
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               FATAL EXCEPTION: main
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               Process: com.splitinfinitivesoftware.pve, PID: 16557
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               java.lang.IllegalStateException: Could not find method btnSettings_onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.ImageButton with id 'btnSettings'
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5721)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.view.View$DeclaredOnClickListener.onClick(View.java:5680)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.view.View.performClick(View.java:6897)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.view.View$PerformClick.run(View.java:26100)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.os.Handler.handleCallback(Handler.java:789)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:98)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:164)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:6944)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
01-14 16:38:34.679 16557 16557 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

I can't figure out what is going on.
 
Oops, ignore the boolean f, that was not originally there, that was me trying to figure out if it was erroring because i didnt supply a variable to hold arguments sent.
 
The log is saying it cannot find the button. Try the following...

Change your button code in MainActivity from this...

Java:
public void btnSettings_onClick(boolean f) {
    //btnSettings.setImageResource(R.drawable.key);
}

To this...

Java:
btnSettings.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // code that button will execute goes here
    }
});

Remove this line from your main.xml since the onClick() method is already setup in java and we're using the button id to make the connection...

XML:
android:onClick='btnSettings_onClick'

I'd also recommend using Android Studio instead of AIDE since it's the standard for android app development and also helps with code fixes and completion.
 
Last edited:
Thank you, I appreciate the reply.
Hmm, ok I kinda thought it was something like that.... I originally tried calling setOnClickListener but it's not showing up on the autocomplete and it says unexpected end of declaration between the r in listener and the open parentheses. I suspect I'm missing a dependency. Is that part of appcompat or something?

And I would gladly use Android studio but at the moment the only device I have is my phone, I need an x86 based device sooooooooooo bad!
 
Well your original problem is because you used the wrong parameter type for the btnSettings_onClick() method. You used boolean, but the runtime expected View.
 
Well your original problem is because you used the wrong parameter type for the btnSettings_onClick() method. You used boolean, but the runtime expected View.

You might have missed it, but I said that was not in my initial code. When trying to figure out why it was crashing I swapped out the different types of variables to see if that was the problem. Boolean F was just the last failed attempt.
 
Well as it stands, and according to the stack trace you posted, the method signature is wrong. You should be using a parameter of type View.
 
You are correct, i had tried view when trying other types but it still gave me an error. I accidentally found that what was missing was an imporrt android.view at the top of my file. Its working now. Thank you for your input, it did point me toward the solution.
 
That's great. Always listen to the compiler. It knows what it's doing ;)
 
Yeah, i saw that, but didnt know how to fix it. I accidentally found that clicking on the error gives me suggestions on how to fix.
 
One of those suggestions would be to add the correct import statement for View.
 
Ok... new problem, same app....
What is it telling me.. how do i fix it?

Code:
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               FATAL EXCEPTION: main
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               Process: com.splitinfinitivesoftware.pve, PID: 26272
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               java.lang.IllegalStateException: Could not execute method for android:onClick
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.view.View$DeclaredOnClickListener.onClick(View.java:5689)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.view.View.performClick(View.java:6897)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.view.View$PerformClick.run(View.java:26100)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.os.Handler.handleCallback(Handler.java:789)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:98)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:164)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:6944)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               Caused by: java.lang.reflect.InvocationTargetException
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at android.view.View$DeclaredOnClickListener.onClick(View.java:5684)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               ... 9 more
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.text.ClipboardManager.setText(java.lang.CharSequence)' on a null object reference
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               at com.splitinfinitivesoftware.pve.MainActivity.btnCut_onClick(MainActivity.java:217)
01-19 19:14:59.414 26272 26272 E   AndroidRuntime                               ... 11 more
 
Question to you - do you know how to read a stack trace?

If the answer to the above is "no", then I would strongly suggest that you study the basics of app development and in particular, debugging. It will save you a huge amount of time, improve your productivity, and you'll have the ability to help yourself, rather than have to consult others when you run into problems. Read this thread, which has some useful pointers on the basics

https://androidforums.com/threads/please-read-me-before-posting.987318/

You have a null pointer error in your code. If you don't understand what that is, then you're really going to struggle with anything, because that is the most basic code problem.
 
Last edited by a moderator:
I do understand somewhat, and i knew about the null pointer, java is new to me though and after reading what i have been able to find i beliece i grasp what it is telling me. I know about poubters in that tgey point to memory/variable informatio. And i surmise that it is sating that this pointer is null (not in existence), but i cant figure out what this pointer is or where to fix it. I followed what i found at devekoper.Android.com and im very frustrated with its dictionary like setup. Knowung the spelling and meaning of a word is great, but you cant learn to speak a language just from a ductionary, you need more. I know im doing the equivalent of reing the dictionary and then spewing jibberish that i need others to explain to me.

And yes i have read that stackoverflow piece on null pointers, just read it again too, but i still dont know what pointer its talking about, i didn't use one (i thInk)

my line that ita barfing on is
Code:
clip.setText('test');
and at ny start i have
Code:
private ClipboardManager clip;
i know i need to give clip an inital value but i cant find what its supposed to be.
 
Last edited:
but i cant figure out what this pointer is or where to fix it

When you declare a variable like this

Code:
private ClipboardManager clip;

without initialising, it gets a value of null. So if you try to dereference a null variable, by trying to call a method on it, the system throws a NullPointerException.

There's some advice on how to use the ClipboardManager here. I won't cut and paste code here, but you may want to have a good read:

https://stackoverflow.com/questions/19177231/android-copy-paste-from-clipboard-manager
 
Argh im finding this so frustrating! That page about copy and paste is very informative.... its like if someone came to me about a problem with their car and i handed them a part (lets say a new alternator) and said put this in and it will be fine. If they dont know how/where to put it, if they dont know what belts it needs i havent really helped, have I? I love that link, it really helped me understand HOW the clipboard works... but I still cant get it working because i am missing something and i cant find out what it is.... ARGHHHH!

I really do appreciate the help you are trying to provide, the failure is clearly on my part...
 
Ah I have learned something on stackoverfow... I'm not the only .NET user struggling with this! Ok now I don't feel like a complete idiot anymore...
 
WOOT! I'm starting to figure it out! I now have a working cut, working copy, and working paste button! But Its not pasting what I expect. I have some text, lets say 'The quick brown fox jumps over the lazy dog', and cut or copy appears to work but paste will print
Code:
ClipData.Item { T:The quick brown fox jumps over the lazy dog }
instead of
Code:
The quick brown fox jumps over the lazy dog
Ive tried changing parts of my code, adding tostring and such but no luck...
 
Lol, sorry I was at hour 19 or 20 by the time i posted that.... crashed and slept like a rock not long after.... heres the code:
Code:
public void btnCut_onClick ( View v )
	  {
		btnCopy_onClick ( v );
		textBox.setText ( '' );
	  }

	public void btnCopy_onClick ( View v )
	  {
		textBox = findViewById ( R.id.textEditField );
		clip = ClipData.newPlainText ( 'Pve', textBox.getText ( ) ); 
        clipBoard.setPrimaryClip ( clip );
	  }

	public void btnPaste_onClick ( View v )
	  {
		textBox = findViewById ( R.id.textEditField );
		String paste;
		ClipData clip1 = clipBoard.getPrimaryClip ( ); 
		ClipData.Item item = clip1.getItemAt ( 0 );
		paste = item.toString ( );
		textBox.setText ( paste );
	  }
 
The really odd thing here too is the line
Code:
textBox = findViewById(R.id.textEditField);
seems to only wprk unside my copy and paste methods, if i place that line in my onCreate with all the other findViewById lines, it crashes...
 
Instead of


paste = item.toString ( );
use

paste = item.getText( );

Doh! I didnt see getText in my autocomplete popup.

Ok tries an it tells me, cant jaba.lang.charSequence cannot be assigned to java.lang.string then tells me to cast it? What is cast, is it type conversion?
 
Last edited:
Ah now here's a Java 101 lesson. Interfaces vs classes, and type coercion. If you look at the Javadocs for CharSequence

https://developer.android.com/reference/java/lang/CharSequence

you'll see that it's an Interface. In Java an Interface is a special kind of class that contains only method declarations. You can't create an instance of an Interface. What you must do is create a Class definition which implements the Interface. So the Class definition for String looks something like this

Code:
class String implements CharSequence {
 ...
}

So in terms of polymorphism (that's an object-oriented term for things having multiple identities), a String object IS A CharSequence. So this would be entirely valid

Code:
CharSequence myStr = new String();

You can use CharSequence and String types interchangeably. In technical terms, the CharSequence type is a more 'generic' type than String.

So getting back to your issue, the method getText() is declared to return the generic type (interface) CharSequence. But when you run the code, what it actually returns is an object of type String. This is called dynamic binding.

Now the compiler isn't sure what will happen at run time. All it can say is that your types don't match up, and it's not at all happy about that. But in this case, we do know more than the compiler, and can explicitly tell it that the actual type we expect is in fact a String. So you can write

Code:
paste = (String)item.getText( );

This is called type casting, or coercing, because we're forcing the type of the returned object.
 
Ok, thats kinda how i understood it. Of couse i eliminated the issue alltogeter with
Code:
public void btnPaste_onClick ( View v )
	  {
		ClipData clip1 = clipBoard.getPrimaryClip ( ); 
		ClipData.Item item = clip1.getItemAt ( 0 );
		textBox.setText ( item.getText () );
	  }
 
Back
Top Bottom