2012-07-06 81 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. 單擊名稱鏈接,並輸入您的活動名稱。

這會爲您的項目和清單添加一項新的活動。