Android-屏幕链接不起作用

时间:2012-07-16 19:21:31

标签: android button imagebutton

我正在创建的应用程序在主屏幕上有两个按钮,然后链接到另一个屏幕。但是,当我链接到另一个screeen时,我确实显示了xml文件,但与该文件关联的类似乎不起作用。就像我的其他屏幕上的按钮没有做他们应该做的那样(我有一个不返回主页的后退按钮,以及一个没有链接到video.xml屏幕的图像按钮)。 / p>

有谁知道这个问题?

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;
}


}

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>

我也得到了这个logcat错误(在实现注释中所做的更改之后)

07-16 15:46:01.259:E / AndroidRuntime(15421):致命异常:主要 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):在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):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):引起: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):在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 more

2 个答案:

答案 0 :(得分:2)

而不是代码

videoNext.setOnClickListener(new OnClickListener() {

使用videoNext.setOnClickListener(new View.OnClickListener() {

它需要View.onclickListener。

而不是打电话

setContentView(R.layout.screen2xml);

你应该通过startActivity(intent)开始一个新的Activity;方法,并在此活动中显示此布局。 建议不要多次使用setContentView。

答案 1 :(得分:2)

当您单击videoNext时,您只需将screen2的xml加载到MainActivity中。

setContentView(R.layout.screen2xml);

如果要加载Screen2活动,则需要按照您已注释掉的意图启动它。这样就会显示第二个活动,并且实际上正确使用了screen2的xml。像这样:

Intent myIntent = new Intent(MainActivity.this, Screen2.class);
MainActivity.this.startActivity(myIntent);