2012-07-06 76 views
0

我已经搜索并尝试了所有的结果,但无济于事。我正在制作一个从主屏幕开始并通过按钮指向新屏幕的程序。我可以通过setContentView(R.layout.screen2)进入下一个屏幕;但是我的数据来自连接到xml文件的java文件从不转移。我不认为xml文件正在向java文件注册关联(因为我甚至试图创建后退按钮,无论我放什么代码,他们都不会回到前一个屏幕)。意图使得按钮甚至不会进入下一个屏幕。我已经在这一个星期了,我仍然不知道如何解决这个问题。按钮到新屏幕不工作

主要活动 -

package com.wunapp.newsvideoapp; 




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


     Button videoNext = (Button) findViewById(R.id.videoButton) ; 
     videoNext.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       //setContentView(R.layout.screen2); 



        Intent i = new Intent(MainActivity.this, Screen2.class); 
       // i.setClassName("rahul.application.WunApp", "rahul.application.WunApp.screen1"); 
       startActivity(i); 
      } 
     }); 

     Button newsNext = (Button) findViewById(R.id.newsButton); 
     newsNext.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
      //setContentView(R.layout.screen1); 
      Intent intent = new Intent(MainActivity.this, Screen1.class); 
      startActivity(intent); 

      } 
     }); 



} 

} 

Screen2-

package com.wunapp.newsvideoapp; 


//import java.io.IOException; 


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.widget.MediaController; 
import android.widget.VideoView; 

import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnPreparedListener; 

public class Screen2 extends Activity { 


    VideoView videoView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.screen2); 

Button back = (Button)this.findViewById(R.id.backButton); 
     back.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
      setContentView(R.layout.activity_main); 

      } 
     }); 

...[code] 
} 
} 

主要活动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" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:text="@string/welcome" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.25" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" /> 

     <Button 
      android:id="@+id/videoButton" 
      android:layout_width="156dp" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="@string/videoButton" /> 

     <Button 
      android:id="@+id/newsButton" 
      android:layout_width="152dp" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/videoButton" 
      android:layout_below="@+id/textView2" 
      android:layout_marginTop="33dp" 
      android:text="@string/newsButton" /> 

    </RelativeLayout> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </RelativeLayout> 

</LinearLayout> 

画面2 XML-

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_height="fill_parent" 
    android:paddingLeft="2dp" android:paddingRight="2dp" 
    android:paddingTop="2dp" android:paddingBottom="2dp" 
    android:layout_width="fill_parent" android:orientation="vertical"> 


    <VideoView 
     android:id="@+id/VideoView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" /> 

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/backButton" /> 





</RelativeLayout> 

Manifest-

<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> 
    </application> 

</manifest> 

应用程序是一个新的/视频应用程序,在那里你可以在屏幕截图1 RSS源,并在屏幕2视频...我离开了所有这一切使这些工作,因为我的东西知道他们的代码工作正常(尝试他们作为主要文件在不同的项目)。这只是从屏幕到屏幕的链接(并且在后退按钮的情况下向后),我需要帮助。

+0

你是否在清单文件中提及screen2 – 2012-07-06 16:16:27

+0

为了帮助你,你能告诉我们你面临的问题是什么吗?抛出任何异常。 执行以下操作:转至LogCat或控制台并向我们显示此处的跟踪信息,以帮助我们了解问题所在。 也列出你的清单文件。 – Romin 2012-07-06 16:19:11

+0

代码编译得很好,问题是什么也没有显示出来......我只是添加了清单文件 – 2012-07-06 16:21:44

回答

0

改变你的清单文件中像这样

<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=".Screen2" /> 
</application> 
1

您需要将其他活动添加到您的清单

<activity 
    android:name=".Screen1" android:label="Screen1Label" > 
</activity> 
<activity 
    android:name=".Screen2" android:label="Screen2Label" > 
</activity> 
1

AndroidManifest.xml表明,只有一个主要活动。您需要在清单中定义Screen1和Screen2活动。

例如:

<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" 
      android:label="@string/title_activity_screen1" > 
     </activity> 
     <activity 
      android:name=".Screen2" 
      android:label="@string/title_activity_screen2" > 
     </activity> 
    </application> 

</manifest> 
+0

非常感谢大家!正如你所看到的,我是一个新手,但你们都真的帮助过我,从来不知道你必须改变清单。 – 2012-07-06 16:34:26

1

我按照添加新的活动的做法如下:

  1. 点击AndroidManifest.xml
  2. 点击应用节点选项卡( 屏幕的左下角)
  3. 点击添加并选择活动作为选项
  4. 单击名称链接,并输入您的活动名称。

这会为您的项目和清单添加一项新的活动。