the app that I am creating has two buttons on the main screen, that then link to another screen. However, when I link to the other screeen, I do get the xml file to show up, but the class associated with that file does not seem to work. Like I have buttons on my other screen that do not do what they are supposed to (I have a back button that does not go back to the main page, and an image button that does not link to the video.xml screen).
Does anyone know the problem?
Main_Activity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button videoNext = (Button) findViewById(R.id.videoButton) ;
videoNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Screen2.class);
MainActivity.this.startActivity(myIntent);
//setContentView(R.layout.screen2xml);
}
});
Button newsNext = (Button) findViewById(R.id.newsButton);
newsNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Screen1.class);
MainActivity.this.startActivity(myIntent);
//setContentView(R.layout.screen2xml);
}
});
}
}
Screen 2.java
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class Screen2 extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2xml);
Button back = (Button)this.findViewById(R.id.backButton);
back.setBackgroundColor(5);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
//finish();
//Intent intent = new Intent(SImpleRssReader2Activity.this, rahul.SRR2.SimpleRssReader2.Screen1.class);
//startActivity(intent);
}
});
ImageButton vid1 = (ImageButton)this.findViewById(R.id.imageButton1);
vid1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.video);
//finish();
//Intent intent = new Intent(SImpleRssReader2Activity.this, rahul.SRR2.SimpleRssReader2.Screen1.class);
//startActivity(intent);
}
});
}
}
screen2xml.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/backButton" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="120dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:background="@drawable/ic_button1"
android:src="@drawable/ic_button1" android:contentDescription="TODO"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/imageButton1"
android:layout_marginTop="81dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="@string/text" />
</RelativeLayout>
Video1.java
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
public class Video1 extends Activity {
VideoView videoView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
videoView = (VideoView) findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
//Set video link (mp4 format )
Uri video = Uri.parse("http://vimeo.com/15615625");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
setContentView(R.layout.video);
}
@SuppressLint("ParserError") @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
video.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:orientation="vertical" >
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wunapp.newsvideoapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Screen1">
</activity>
<activity
android:name=".Screen2">
</activity>
<activity
android:name=".video">
</activity>
</application>
</manifest>
I get this logcat error as well (after implementing the changes made in the comments)
07-16 15:46:01.259: E/AndroidRuntime(15421): FATAL EXCEPTION: main
07-16 15:46:01.259: E/AndroidRuntime(15421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wunapp.newsvideoapp/com.wunapp.newsvideoapp.Screen2}: java.lang.NullPointerException
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.os.Looper.loop(Looper.java:154)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.ActivityThread.main(ActivityThread.java:4977)
07-16 15:46:01.259: E/AndroidRuntime(15421): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 15:46:01.259: E/AndroidRuntime(15421): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 15:46:01.259: E/AndroidRuntime(15421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 15:46:01.259: E/AndroidRuntime(15421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 15:46:01.259: E/AndroidRuntime(15421): at dalvik.system.NativeStart.main(Native Method)
07-16 15:46:01.259: E/AndroidRuntime(15421): Caused by: java.lang.NullPointerException
07-16 15:46:01.259: E/AndroidRuntime(15421): at com.wunapp.newsvideoapp.Screen2.onCreate(Screen2.java:30)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.Activity.performCreate(Activity.java:4538)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-16 15:46:01.259: E/AndroidRuntime(15421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-16 15:46:01.259: E/AndroidRuntime(15421): … 11 more
When you click videoNext, you’re simply loading screen2’s xml into MainActivity.
If you want to load Screen2 activity, then you need to launch it with an intent like the one you have commented out. That way the second activity will be shown and actually use the screen2’s xml correctly. Like this: