Java 多视频视图问题
我的应用程序包含5个按钮,每个按钮打开不同的视频视图,当我创建代码时,我在下面有红色标记:(new MediaController(此)),我试图解决它,但我不能 请给出建议,我是android开发的新手 谢谢 应用更改为:(new MediaController(this))后更新,获取另一个错误 我的代码:Java 多视频视图问题,java,android,Java,Android,我的应用程序包含5个按钮,每个按钮打开不同的视频视图,当我创建代码时,我在下面有红色标记:(new MediaController(此)),我试图解决它,但我不能 请给出建议,我是android开发的新手 谢谢 应用更改为:(new MediaController(this))后更新,获取另一个错误 我的代码: public class MultipleButton extends Activity { String night; /** Called when the act
public class MultipleButton extends Activity {
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,My_videos.class);
intent.putExtra("video",night);
startActivity(intent);
}
}
I/Process(25196): Sending signal. PID: 25196 SIG: 9
D/AndroidRuntime(25447): Shutting down VM
W/dalvikvm(25447): threadid=1: thread exiting with uncaught exception (group=0x40015578)
E/AndroidRuntime(25447): FATAL EXCEPTION: main
E/AndroidRuntime(25447): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsn.dr/com.tsn.dr.My_videos}: java.lang.NullPointerException
E/AndroidRuntime(25447):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1651)
E/AndroidRuntime(25447):at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1667)
E/AndroidRuntime(25447):at android.app.ActivityThread.access$1500 (ActivityThread.java:117)
E/AndroidRuntime(25447):at android.app.ActivityThread$H.handleMessage (ActivityThread.java:935)
E/AndroidRuntime(25447):at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25447):at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(25447):at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime(25447):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25447):at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(25447):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:842)
E/AndroidRuntime(25447):at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:600)
E/AndroidRuntime(25447):at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25447): Caused by: java.lang.NullPointerException
E/AndroidRuntime(25447):at com.tsn.dr.My_videos.initializeVideoViews (My_videos.java:31)
E/AndroidRuntime(25447):at com.tsn.dr.My_videos.onCreate(My_videos.java:24)
E/AndroidRuntime(25447):at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047)
E/AndroidRuntime(25447):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1615)
E/AndroidRuntime(25447): ... 11 more
多按钮类:
public class MultipleButton extends Activity {
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,My_videos.class);
intent.putExtra("video",night);
startActivity(intent);
}
}
I/Process(25196): Sending signal. PID: 25196 SIG: 9
D/AndroidRuntime(25447): Shutting down VM
W/dalvikvm(25447): threadid=1: thread exiting with uncaught exception (group=0x40015578)
E/AndroidRuntime(25447): FATAL EXCEPTION: main
E/AndroidRuntime(25447): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsn.dr/com.tsn.dr.My_videos}: java.lang.NullPointerException
E/AndroidRuntime(25447):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1651)
E/AndroidRuntime(25447):at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1667)
E/AndroidRuntime(25447):at android.app.ActivityThread.access$1500 (ActivityThread.java:117)
E/AndroidRuntime(25447):at android.app.ActivityThread$H.handleMessage (ActivityThread.java:935)
E/AndroidRuntime(25447):at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25447):at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(25447):at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime(25447):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25447):at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(25447):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:842)
E/AndroidRuntime(25447):at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:600)
E/AndroidRuntime(25447):at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25447): Caused by: java.lang.NullPointerException
E/AndroidRuntime(25447):at com.tsn.dr.My_videos.initializeVideoViews (My_videos.java:31)
E/AndroidRuntime(25447):at com.tsn.dr.My_videos.onCreate(My_videos.java:24)
E/AndroidRuntime(25447):at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047)
E/AndroidRuntime(25447):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1615)
E/AndroidRuntime(25447): ... 11 more
我的视频课程:strong文本
public class My_videos extends Activity {
private Button button;
private VideoView vid;
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
initializeVideoViews(); }
private void initializeVideoViews() {
String night = getIntent().getStringExtra("video");
if(night.equalsIgnoreCase("button1")){
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid =(VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.a));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
}
}); }
else if(night.equalsIgnoreCase("button2")){
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.b));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});}
else if(night.equalsIgnoreCase("button3")){
button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.c));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});}
else if(night.equalsIgnoreCase("button4")){
button = (Button) findViewById(R.id.button4);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.d));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});}
else if(night.equalsIgnoreCase("button5")){
button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.e));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});
}}}
更新:strong文本
public class My_videos extends Activity {
private Button button;
private VideoView vid;
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
initializeVideoViews(); }
private void initializeVideoViews() {
String night = getIntent().getStringExtra("video");
if(night.equalsIgnoreCase("button1")){
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid =(VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.a));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
}
}); }
else if(night.equalsIgnoreCase("button2")){
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.b));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});}
else if(night.equalsIgnoreCase("button3")){
button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.c));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});}
else if(night.equalsIgnoreCase("button4")){
button = (Button) findViewById(R.id.button4);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.d));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});}
else if(night.equalsIgnoreCase("button5")){
button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
vid = (VideoView) findViewById(R.id.videoView1);
vid.setVideoURI(Uri.parse("android.resource://" +
getPackageName() +"/"+R.raw.e));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
}
});
}}}
LOGCAT:
public class MultipleButton extends Activity {
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,My_videos.class);
intent.putExtra("video",night);
startActivity(intent);
}
}
I/Process(25196): Sending signal. PID: 25196 SIG: 9
D/AndroidRuntime(25447): Shutting down VM
W/dalvikvm(25447): threadid=1: thread exiting with uncaught exception (group=0x40015578)
E/AndroidRuntime(25447): FATAL EXCEPTION: main
E/AndroidRuntime(25447): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsn.dr/com.tsn.dr.My_videos}: java.lang.NullPointerException
E/AndroidRuntime(25447):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1651)
E/AndroidRuntime(25447):at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1667)
E/AndroidRuntime(25447):at android.app.ActivityThread.access$1500 (ActivityThread.java:117)
E/AndroidRuntime(25447):at android.app.ActivityThread$H.handleMessage (ActivityThread.java:935)
E/AndroidRuntime(25447):at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25447):at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(25447):at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime(25447):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25447):at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(25447):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:842)
E/AndroidRuntime(25447):at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:600)
E/AndroidRuntime(25447):at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25447): Caused by: java.lang.NullPointerException
E/AndroidRuntime(25447):at com.tsn.dr.My_videos.initializeVideoViews (My_videos.java:31)
E/AndroidRuntime(25447):at com.tsn.dr.My_videos.onCreate(My_videos.java:24)
E/AndroidRuntime(25447):at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047)
E/AndroidRuntime(25447):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1615)
E/AndroidRuntime(25447): ... 11 more
manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tsn.dr"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MultipleButton"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".My_videos"
android:label="@string/app_name">
</activity>
</application>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="handleClick"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="handleClick"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="handleClick"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="handleClick"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="handleClick"
android:text="Button" />
</LinearLayout>
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" >
<VideoView
android:id="@+id/videoView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在按钮单击内部使用这组行将导致错误
setMediaController(new MediaController(this));
因为上面一行中的这个指的是按钮实例,而不是那个按钮实例,请尝试使用这个
setMediaController(new MediaController(My_videos.this));
写下下面的任意一行代码 MultipleButton.java
public class MultipleButton extends Activity {
String night;
Button mBtn1, mBtn2, mBtn3, mBtn4, mBtn5;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtn1=(Button)findViewById(R.id.button1);
mBtn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
night="button1";
Intent intent = new Intent();
intent.setClass(MultipleButton.this, My_videos.class);
intent.putExtra("video", night);
startActivity(intent);
}
});
mBtn2=(Button)findViewById(R.id.button2);
mBtn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
night="button2";
Intent intent = new Intent();
intent.setClass(MultipleButton.this, My_videos.class);
intent.putExtra("video", night);
startActivity(intent);
}
});
mBtn3=(Button)findViewById(R.id.button3);
mBtn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
night="button3";
Intent intent = new Intent();
intent.setClass(MultipleButton.this, My_videos.class);
intent.putExtra("video", night);
startActivity(intent);
}
});
mBtn4=(Button)findViewById(R.id.button4);
mBtn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
night="button4";
Intent intent = new Intent();
intent.setClass(MultipleButton.this, My_videos.class);
intent.putExtra("video", night);
startActivity(intent);
}
});
mBtn5=(Button)findViewById(R.id.button5);
mBtn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
night="button5";
Intent intent = new Intent();
intent.setClass(MultipleButton.this, My_videos.class);
intent.putExtra("video", night);
startActivity(intent);
}
});
}
}
My_videos.java
public class My_videos extends Activity {
private VideoView vid;
String night;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
Bundle bdl = getIntent().getExtras();
night = bdl.getString("video");
vid = (VideoView) findViewById(R.id.videoView1);
if (night.equalsIgnoreCase("button1")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.a));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
} else if (night.equalsIgnoreCase("button2")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.b));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
} else if (night.equalsIgnoreCase("button3")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.c));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
} else if (night.equalsIgnoreCase("button4")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.d));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
} else if (night.equalsIgnoreCase("button5")) {
vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.e));
vid.setMediaController(new MediaController(My_videos.this));
vid.requestFocus();
vid.start();
}
}
}
main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
尝试在清单文件中为My_videos活动添加意图过滤器,而不是My_videos.java类
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
更新----
您正在尝试查找video.xml中没有的视图、按钮1、按钮2等。所以它给出了空指针异常。您的My_videos类是否在同一个包中?我的意思是,在com.tsn.dr?@Raghu Nagaraju是的coruse我亲爱的你的logcat显示了在寻找活动时的问题,但你在MediaController中告诉了问题。。你确定,这是最新的日志吗?