Reputation: 969
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);
}
Upvotes: 1
Views: 1774
Reputation: 4114
You can go to an Activity from one Activity using Intent class
public void startRecording(View v) {
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
Add your Android Manifest configuration file
<activity android:name="NewActivity"></activity>
Upvotes: 1
Reputation: 87064
To make it work this should be your startRecording
method:
public void startRecording(View v) {
// setContentView(R.layout.next_page); this will modify the current activity view
// if you want to start a new activity:
Intent i = new Intent(this, next_page.class);
startActivity(i);
}
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.xml
file:
// ...
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".next_page" />
// ...
You should read from the android developers site or some tutorials, this is basic stuff.
Upvotes: 3
Reputation: 3701
In onCreate method of first activity write like below
Button button1=(Button)findViewById(r.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
});
Upvotes: 0
Reputation: 6578
startRecording - bad code. need write:
startActivity(new Intent(this, next_page.class));
And learn how to format code
Upvotes: 0
Reputation: 10830
Well you assuming what triggers the next Activity is the Button, I would do:
Button butt = (Button)findViewById(R.id.button1);
butt.setOnClickListener(new View.onClickListener(
public void onClick(View v) {
Intent intent = new Intent(v.getContext());
//optionally, you could provide it stuff to send to the second Activity if you wish
startActivity(intent);
}
));
But honestly, you could have figured this out on your own through research.
Upvotes: 0
Reputation: 7822
Use the indent to switch between the activity. Sample code
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), SecondScreenActivity.class);
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("email", inputEmail.getText().toString());
Log.e("n", inputName.getText()+"."+ inputEmail.getText());
startActivity(nextScreen);
}
});
Upvotes: 0