I just started to study the android so im asking such a simple question. I tried to navigate(move from one view to another). This code shows no error and the button in first view is showing. But when i click the button nothing happens and app crashed.Can anyone please help me with where i’m going wrong in my code.
pushActivity.java
package com.myapp.pus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class PushActivity extends Activity {
Button mybtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mybtn = (Button)findViewById(R.id.mybtn);
mybtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Intent nextScreen = new Intent(getApplicationContext(), SecondScreen.class);
// startActivity(new Intent(action));
startActivity(nextScreen);
}
});
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/mybtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Second Screen
package com.myapp.pus;
import android.app.Activity;
import android.os.Bundle;
public class SecondScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// Binding Click event to Button
}
}
Screen2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Thanks in advance.Hope for your help.
You need to define the SecondScreen Activity to your
AndroidManifest.xmlas below,<activity android:name=".SecondScreen"></activity>before the tag is finish.