Opening one screen from another - java

I am trying to open one screen from another in an Android App. I copied the outline of the following code from a tutorial online and then tried to substitute my own screens. When I try to run the following code, I get a "Force Close" when I click the button on the first screen (the second screen never displays). Can someone tell me what I am doing wrong? There are four files: ScreenTestActivity.java, main.xml, screen2.java and screen2.xml.
ScreenTestActivity.java
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ScreenTestActivity extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ScreenTestActivity.this, screen2.class);
startActivity(i);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
</LinearLayout>
screen2.java:
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.os.Bundle;
public class screen2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
}
}
screen2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the New Screen"
/>
</LinearLayout>

<activity android:name="screen2"></activity>
<activity android:name="ScreenTestActivity"></activity>
put this 2 line of code in your androidmanifest.xml file

Related

Cannot find the element by Id in the mainActivityJava

I want to select the button element by ID, but when I run the program, I get the app keeps stopping error.
I ran the program in debug mode and the error Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference
Even when I click on R.id.button, the button element is displayed in the xml file.
mainActivityFile :
package com.example.app;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity6 extends AppCompatActivity {
private Button btn;
private TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
btn.findViewById(R.id.button);
txt.findViewById(R.id.txt);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txt.setText("This is a new text.");
}
});
// Toast.makeText(this, "str", Toast.LENGTH_LONG).show();
}
}
xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity6">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="25sp"
android:autoSizeMaxTextSize="100dp"
android:autoSizeStepGranularity="2sp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
btn.findViewById(R.id.button);
You're not supposed to use it this way because btn is null at this point.
You have to do the following:
btn = (Button)findViewById(R.id.button);

Getting white screen when trying to open the app. App is getting launched on my phone without errors

When I am trying to run my app on my phone it is getting successfully installed in my phone but I am getting white screen instead of my splash page . Attaching my xml code and java code for your reference . Your help is highly appreciated !!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:fontFamily="sans-serif-black"
android:text="MY APP"
android:textColor="#color/Black"
android:textSize="50sp" />
</Relativelayout>
activity_main.java
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
}
The finish() method is called and the activity destroys and returns to the home screen.
Your code should be like this
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
remove the last line which is :-
finish();

Cannot switch activities in Android except one activity

So I have been making a simple four button app for my IT 1 class and have been having a problem with some code. I have four buttons that are supposed to call a startActivity and change to a different screen. The first button, labeled fire, works perfectly fine and works 100% of the time. The next button, labeled Earthquake doesn't work, even though the code is the same (without the parts that need to change). The other 2 buttons also do not work. I have tried making a new project, copying the working xml, copying the exact code. I cannot figure out why it doesn't work.
MainActivity 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"
android:background="#81848b"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.carrieowen.testproject.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#eff2f9"
android:text="Emergency Drills"
android:gravity="center"
android:textSize="32sp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="40dp"
android:text="Fire"
android:background="#e71e1e"
android:id="#+id/button"
android:onClick="Fire"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#009c12"
android:padding="40dp"
android:text="Earthquake"
android:id="#+id/button2"
android:onClick="Earthquake"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#eef035"
android:padding="40dp"
android:text="Tornado"
android:id="#+id/button3"
android:onClick="Tornado"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#1b4fdd"
android:padding="40dp"
android:text="Code Blue"
android:id="#+id/button4"
android:onClick="CodeBlue"/>
</LinearLayout>
</RelativeLayout>
The MainActivity Java
package com.example.carrieowen.testproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Fire (View view){
Intent changeScreenFire = new Intent(this, firescreen.class);
startActivity(changeScreenFire);
}
public void Earthquake (View view){
Intent changeScreenEarthquake = new Intent(this, earthquake.class);
startActivity(changeScreenEarthquake);
}
public void Tornado (View view){
Intent changeScreenTornado = new Intent(this, tornado.class);
startActivity(changeScreenTornado);
}
public void CodeBlue (View view){
Intent changeScreenCodeBlue = new Intent(this, codeblue.class);
startActivity(changeScreenCodeBlue);
}
}
The FireScreen Java
package com.example.carrieowen.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class firescreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firescreen);
}
}
The Earthquake Java
package com.example.carrieowen.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class earthquake extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_earthquake);
}
}
The XML for the Fire and Earthquake are the same other than the Text boxes being different. If needed will add.
Make sure your activities are listed in the manifest? The manifest file is called AndroidManifest.xml and is listed in the project at the top, in android studio. This is what the content should look like:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<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=".Tornado">
</activity>
<activity android:name=".CodeBlue">
</activity>
</application>
I have something like that as well, but my code is a bit different. Try it like this. The button that you used to create open the new activity is labeled in the line starting with Button Pause, then i set a onclicklistner so that when you click on it, something happens, and i have it to currently oped a new activity in the method past the #Override part.
public class GameWindow extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_window);
Button pause = (Button) findViewById(R.id.pausebutton);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
Intent intent = new Intent(GameWindow.this, PauseMenu.class);
startActivity(intent);
}
});
}

embed MapView into Activity

I am trying to embed MapView into FrameLayout of an activity for example ,
UserActivity layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#+id/map_frame" android:layout_gravity="center_horizontal" android:padding="0.0dip" android:clipChildren="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4.0dip">
<ImageView android:id="#+id/map_img" android:layout_gravity="center" android:layout_width="294.0dip" android:layout_height="136.0dip" android:src="#drawable/place_map_frame" />
</FrameLayout>
</LinearLayout>
Here , inside FrameLayout i want to display MapView when ever the list item gets clicked
for some reason its not possible to directly put inside framelayout so just because of that i created another activity which extends MapActivity
<com.google.android.maps.MapView
android:layout_gravity="center"
android:id="#+id/mapview"
android:clickable="true"
android:layout_width="294.0dip"
android:layout_height="136.0dip"
android:layout_centerInParent="true"
android:apiKey="#string/google_maps_key" />
UserMap layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView android:layout_gravity="center" android:id="#+id/mapview" android:clickable="true" android:layout_width="294.0dip" android:layout_height="136.0dip" android:layout_centerInParent="true" android:apiKey="#string/google_maps_key" />
</LinearLayout>
Here is the code for both the activities
import com.google.android.maps.MapActivity;
import android.app.Activity;
import android.os.Bundle;
public class UserMap extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_map);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class UserActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
}
}
Now , i want to embed a UserMap into UserActivity's FrameLayout and UserActivity will be called when a list item get clicked.
Remember to include the Google API instead of the single Android API.
You also need to include the following line in your Manifest, under the <application> tag:
<uses-library android:name="com.google.android.maps" />

Video player not working - Android Studio

I am trying to add a video played on button click with android studio.
However, when I click the button a "sorry, this video cannot be played" message box appears on the emulator screen.
Can you help me see where I'm going wrong.
Below is the code I approached the goal with
Trialvideo.java
package android.com.trialvideo;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class TrialVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** // Video view: to view our video
VideoView video = (VideoView) findViewById(R.id.surface_view);
//set video path to our video(in this case man-cheetah-gazalle.3gp)
video.setVideoPath("/raw/jeewan.mp4");
video.start();
**/
final Button play =(Button)findViewById(R.id.play);
play.setOnClickListener(new OnClickListener(){
public void onClick(View V){
videoPlayer();
}
});}
public void videoPlayer(){
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = (VideoView)findViewById(R.id.surface_view);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoPath("/TrialVideo/raw/lic.3gp");
videoHolder.requestFocus();
videoHolder.start();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
Hi try the following code:
VideoPlaying.java
public class VideoPlaying extends Activity {
private MediaController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.requestFocus();
vd.setVideoURI(uri);
vd.start();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView"></VideoView>
</LinearLayout>
place the video on the raw folder and run the code. Sometimes video will not be correctly shown on the emulator, try to check it also on the actual device.
Android does not have a C: drive. You need to put the video file on the device (e.g., copy it to the device's external storage), then supply VideoView with an appropriate path to the file (e.g., using Environment.getExternalStorageDirectory()).

Categories

Resources