I just started learning Android yesterday and would like to code a very simple app that consists of 2 views for learning purpose.
Activity/View 1: 1 button and a text “Hello World”
On clicking the button, it should go to the next activity/view which only has text “testing”.
Here’s my code for activity 1:
package helloworld.app;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem; import
android.support.v4.app.NavUtils;
/*import AudioRecordTest;*/
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void startRecording() {
setContentView(R.layout.next_page);
}
}
Here’s my code for activity 2:
package helloworld.app;
import android.os.Bundle;
import android.app.Activity; import
android.view.Menu;
import android.view.MenuItem; import
android.support.v4.app.NavUtils;
/*import AudioRecordTest;*/
public class next_page extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_page);
} }
Here’s my code for xml file for activity 1:
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="40dp"
android:layout_marginTop="32dp"
android:text="Start Recording"
android:onClick="startRecording" />
</RelativeLayout>
EDIT:
Here are my error messages from logcat
08-01 07:00:49.253: I/Choreographer(1326): Skipped 40 frames! The
application may be doing too much work on its main thread.08-01 07:01:11.653: D/AndroidRuntime(1326): Shutting down VM
08-01 07:01:11.653: W/dalvikvm(1326): threadid=1: thread exiting with
uncaught exception (group=0x40a13300)08-01 07:01:11.673: E/AndroidRuntime(1326): FATAL EXCEPTION: main
08-01 07:01:11.673: E/AndroidRuntime(1326):
java.lang.IllegalStateException: Could not find a method
startRecording(View) in the activity class helloworld.app.MainActivity
for onClick handler on view class android.widget.Button with id
‘button1’08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.view.View$1.onClick(View.java:3578)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.view.View.performClick(View.java:4084)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.view.View$PerformClick.run(View.java:16966)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.os.Handler.handleCallback(Handler.java:615)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.os.Handler.dispatchMessage(Handler.java:92)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.os.Looper.loop(Looper.java:137)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.app.ActivityThread.main(ActivityThread.java:4745)08-01 07:01:11.673: E/AndroidRuntime(1326): at
java.lang.reflect.Method.invokeNative(Native Method)08-01 07:01:11.673: E/AndroidRuntime(1326): at
java.lang.reflect.Method.invoke(Method.java:511)08-01 07:01:11.673: E/AndroidRuntime(1326): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)08-01 07:01:11.673: E/AndroidRuntime(1326): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)08-01 07:01:11.673: E/AndroidRuntime(1326): at
dalvik.system.NativeStart.main(Native Method)08-01 07:01:11.673: E/AndroidRuntime(1326): Caused by:
java.lang.NoSuchMethodException: startRecording [class
android.view.View]08-01 07:01:11.673: E/AndroidRuntime(1326): at
java.lang.Class.getConstructorOrMethod(Class.java:460)08-01 07:01:11.673: E/AndroidRuntime(1326): at
java.lang.Class.getMethod(Class.java:915)08-01 07:01:11.673: E/AndroidRuntime(1326): at
android.view.View$1.onClick(View.java:3571)08-01 07:01:11.673: E/AndroidRuntime(1326): … 11 more
EDIT2:
I found my mistake! I am supposed to pass in View view for my startRecording function. Here’s the edited code:
public void startRecording(View view)
{
Intent intent = new Intent(this, next_page.class);
startActivity(intent);
}
To make it work this should be your
startRecordingmethod:Make sure you have declared both activities in the manifest file.
For android to “see” and use your activities the must be declared in the
AndroidManifest.xmlfile:You should read from the android developers site or some tutorials, this is basic stuff.