0

在這裏不用我mainactivity.java當我在mainactivity點擊一個按鈕,我想顯示一個XML文件

public class MainActivity extends Activity { 
Button iol; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    iol=(Button)findViewById(R.id.iolCalculation); 
    try{ 
     iol.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent iolIntent=new Intent(MainActivity.this,Selection.class); 
       startActivity(iolIntent); 
      } 
     }); 
    }finally{ 

    } 

} 

這裏是我的主XML其中id爲Button1的按鈕被點擊

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/black" 
    tools:context=".FullscreenActivity" 
    android:id="@+id/frame_layout"> 

     <LinearLayout 
      android:id="@+id/fullscreen_content_controls" 
      style="@style/ButtonBar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|center_horizontal" 
      android:background="@color/black_overlay" 
      android:orientation="horizontal" 
      tools:ignore="UselessParent" > 
     </LinearLayout> 

     <Button 
      style="@style/ButtonBarButton" 
      android:layout_width="222dp" 
      android:layout_height="62dp" 
      android:layout_marginLeft="45dp" 
      android:layout_marginTop="75dp" 
      android:background="@color/black" 
      android:paddingLeft="20dp" 
      android:id="@+id/iolCalculation" 
      android:text="@string/Button" 
      android:textColor="@color/WhiteSmoke" 
      android:textStyle="bold" /> 

     <Button 
      style="@style/ButtonBarButton" 
      android:layout_width="222dp" 
      android:layout_height="62dp" 
      android:layout_marginLeft="45dp" 
      android:id="@+id/sHelp" 
      android:layout_marginTop="139dp" 
      android:background="@color/Black" 
      android:gravity="center" 
      android:paddingLeft="10dp" 
      android:text="@string/Button1" 
      android:textColor="@color/WhiteSmoke" 
      android:textStyle="bold" /> 

     <Button 
      style="@style/ButtonBarButton" 
      android:layout_width="222dp" 
      android:layout_height="62dp" 
      android:id="@+id/sAbout" 
      android:layout_marginLeft="45dp" 
      android:layout_marginTop="203dp" 
      android:background="@color/Black" 
      android:paddingLeft="10dp" 
      android:text="@string/Button2" 
      android:textColor="@color/WhiteSmoke" 
      android:textStyle="bold" > 

     </Button> 
</FrameLayout> 

在這裏不用我的第二個XML佈局這與它的內容時按鈕一起是diplayed 1被點擊

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/black" 
    android:orientation="vertical" 
    android:id="@+id/form_layout"> 

    <Spinner 
     android:id="@+id/formulae" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="50dp" 
     android:layout_marginRight="50dp" 
     android:background="@color/LightSkyBlue" 
     android:prompt="@string/prompt" 
     android:entries="@array/formulas" /> 

    <TextView 
     android:id="@+id/k2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/k1" 
     android:layout_below="@+id/k1" 
     android:layout_marginTop="38dp" 
     android:text="@string/K2" 
     android:textColor="@color/White" 
     android:textSize="25sp" /> 

    <TextView 
     android:id="@+id/al_const" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/al" 
     android:layout_below="@+id/al" 
     android:layout_marginTop="44dp" 
     android:text="@string/Rx" 
     android:textColor="@color/White" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/al" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/k2" 
     android:layout_below="@+id/k2" 
     android:layout_marginTop="38dp" 
     android:text="@string/AL" 
     android:textColor="@color/White" 
     android:textSize="25sp" /> 

    <EditText 
     android:id="@+id/k2_editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/al" 
     android:layout_alignRight="@+id/al_editText" 
     android:layout_alignTop="@+id/k2" 
     android:layout_marginLeft="120dp" 
     android:background="@color/black" 
     android:ems="10" 
     android:inputType="numberDecimal" 
     android:textColorLink="@color/white" /> 

    <EditText 
     android:id="@+id/al_editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/formulae" 
     android:layout_alignTop="@+id/al" 
     android:layout_marginLeft="120dp" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

    <EditText 
     android:id="@+id/al_const_editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/al_const" 
     android:layout_alignRight="@+id/al_editText" 
     android:layout_marginLeft="120dp" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

    <Button 
     android:id="@+id/result" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="@color/LightSkyBlue" 
     android:text="@string/Result" /> 

    <EditText 
     android:id="@+id/k1_editText" 
     android:layout_width="wrap_content" 
     android:layout_height="37dp" 
     android:layout_alignLeft="@+id/k2_editText" 
     android:layout_alignRight="@+id/formulae" 
     android:layout_below="@+id/formulae" 
     android:layout_marginTop="30dp" 
     android:background="@color/black" 
     android:ems="10" 
     android:inputType="numberDecimal" 
     android:textColorLink="@color/White" /> 

    <TextView 
     android:id="@+id/k1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/k1_editText" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="27dp" 
     android:text="@string/K1" 
     android:textColor="@color/white" 
     android:textSize="25sp" /> 

</RelativeLayout> 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.iolcalci" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.iolcalci.MainActivity" 
      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="Selection" 
      android:exported="false" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:label="@string/app_name"> 
       <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

logcat的用於選擇類使用的setContentView

12-24 08:13:52.456: E/AndroidRuntime(766): FATAL EXCEPTION: main 
12-24 08:13:52.456: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.Selection}: android.content.res.Resources$NotFoundException: Resource ID #0x7f090005 type #0x12 is not valid 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:5039) 
12-24 08:13:52.456: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method) 
12-24 08:13:52.456: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511) 
12-24 08:13:52.456: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
12-24 08:13:52.456: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
12-24 08:13:52.456: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method) 
12-24 08:13:52.456: E/AndroidRuntime(766): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f090005 type #0x12 is not valid 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.content.res.Resources.getLayout(Resources.java:853) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
12-24 08:13:52.456: E/AndroidRuntime(766): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.Activity.setContentView(Activity.java:1881) 
12-24 08:13:52.456: E/AndroidRuntime(766): at com.example.iolcalci.Selection.onCreate(Selection.java:12) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.Activity.performCreate(Activity.java:5104) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
12-24 08:13:52.456: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
12-24 08:13:52.456: E/AndroidRuntime(766): ... 11 more 

執行上面的代碼後,我可以看到顯示的空白布局。如何 我應該在點擊button1後顯示第二個 xml的spinners,textview,editbox等。

+0

通過清單文件創建Selection類,並將第二個xml文件添加到Selection類的setContentView()中。 – User

+0

@Madhavi我已經更新了我的清單,並嘗試setContentview選擇,但沒有奏效。我已經更新logcat的那個條件也 – human

+0

檢查你的按鈕id其錯誤.......你在java文件中提到的id沒有在xml文件中找到 – KMI

回答

0

您正在嘗試用兩種不同的屏幕

here is simple tutorial to Achieve this

,所以你需要創建小的應用..

兩個活動在您的項目

  1. MainActivity.java
  2. Selection.java

兩種設計文件

  1. activity_main.xml中
  2. activity_second。XML

看到你Selection.java(活動)的文件應該是這樣的,來處理微調

public class Selection extends Activity implements OnItemSelectedListener { 

    private Spinner spinner = null; 

    // to show the list in spinner we need to create a list of items 
    private String[] androidBooks = { "Hello, Android", "Professional Android", 
      "Unlocking Android", "Android App Development", "Pro Android", 
      "Beginning Android ", "Android Programming Tutorials", 
      "Android Wireless App Development", "Pro Android Games", }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, androidBooks); 

     // get the spinner id what we decleared in xml layout file 
     // (activity_second.xml) 
     spinner = (Spinner) findViewById(R.id.formulae); 

     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(this); 

      //.... same way you can get id of button etc. and work on them 

    } 

    // implement the item selected listener for spinner 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, 
      long id) { 
     int item = spinner.getSelectedItemPosition(); 
     Toast.makeText(getBaseContext(), 
       "You have selected the book: " + androidBooks[item], 
       Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 

} 

和PL使條目清單文件的應用程序標記活動都這樣

AndroidManifest.xml中

<application 
     android:icon="@drawable/icon" 
     android:label="@string/app_name"> 
     <activity 
      android:name=".MainActivity " 
      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=".Selection" 
      android:label="@string/app_name"> 
      <intent-filter>    
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

如果您有任何問題,請告訴我!樂意幫助

+0

我應該如何顯示微調框和它的elements.i是一個初學者 – human

+0

你在xml文件中有一個微調框,所以你需要在相關的活動文件中得到它的id(就像你已經做了Button的in主要活動)。讓我這樣做,我會盡快更新答案。 – swiftBoy

+0

非常感謝你:) – human

0

如果您正在嘗試移動到另一個屏幕,並使用新視圖和XML,則需要參考創建一個擴展Activity類的新Java類。其中將有setContentView()方法將採取參數爲R.layout.somexml將放置在您的佈局目錄中水庫。瞭解更多從下面的鏈接中讀取。

  1. Android Document

  2. Example

+0

,但截至目前,我應該實施上述活動沒有下一個階層。 – human

+0

當我在新班級中使用setcontenview我有錯誤,如上面logcat – human

+0

所示它說資源未找到。在onCreate()方法中提供您在資源中添加的xml文件。 Selection類也需要是一個Activity。所以它將在選擇活動的清單中刪除IntentFilter標籤。所以它將被選擇擴展活動和公共無效onCreate(Bundle SavedInstance){setContentView(R.layout.somexml);} –

0

發生此錯誤是因爲您忘記將您的Selection類註冊到清單文件中。註冊它在那裏,我相信你的應用程序將工作。

1
in androidmainfest.xml just write <activity 
      android:name="Selection"></activity> 
instead of 
<activity 
      android:name="Selection" 
      android:exported="false" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:label="@string/app_name"> 
       <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
相關問題