我正在创建的应用程序在主屏幕上有两个按钮,然后链接到另一个屏幕。但是,当我链接到另一个屏幕时,我确实显示了 xml 文件,但与该文件关联的类似乎不起作用。就像我在另一个屏幕上的按钮没有做他们应该做的事情(我有一个不返回主页的后退按钮,以及一个不链接到 video.xml 屏幕的图像按钮)。
有谁知道这个问题?
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);
}
});
}
}
画面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;
}
}
视频.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>
我也收到了这个 logcat 错误(在实现了评论中所做的更改之后)
07-16 15:46:01.259: E/AndroidRuntime(15421): 致命异常: main 07-16 15:46:01.259: E/AndroidRuntime(15421): java.lang.RuntimeException: 无法启动活动 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): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在 android.app .ActivityThread.access$600(ActivityThread.java:139) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) 07-16 15: 46:01.259: E/AndroidRuntime(15421): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在 android.os.Looper.loop(Looper.java:154) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在android.app.ActivityThread.main(ActivityThread.java:4977) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在 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): 在 com.wunapp.newsvideoapp.Screen2.onCreate(Screen2.java:30) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在 android.app.Activity.performCreate(Activity .java:4538) 07-16 15:46:01.259: E/AndroidRuntime(15421): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 07-16 15:46:01.259: E/AndroidRuntime(15421 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 07-16 15:46:01.259: E/AndroidRuntime(15421): ... 11 更多E/AndroidRuntime(15421): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 07-16 15:46:01.259: E/AndroidRuntime(15421): ... 11 更多E/AndroidRuntime(15421): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 07-16 15:46:01.259: E/AndroidRuntime(15421): ... 11 更多