2014-01-06 34 views
0

在這裏我做了5個按鈕的代碼,如Aboutus,開發,運營商,服務,contactus。android應用程序一頁到另一個頁面不工作

在這裏,我點擊了關於我們的按鈕,顯示錯誤,因爲不幸的應用程序關閉。

這裏是我的代碼:

MainActivity.java

public class MainActivity extends Activity { 

    Button aboutus,development,service,carriers,contactus; 


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

     Button aboutus=(Button) findViewById(R.id.aboutus); 
     aboutus.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(v.getContext(),aboutus.class); 
       startActivity(i); 

      } 

     }); 

} 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

activity_main.xml中

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 

     android:orientation="vertical" > 

     <Button 
      android:id="@+id/aboutus" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="About us" 

      /> 

     <Button 
      android:id="@+id/services" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Services" 

      /> 

     <Button 
      android:id="@+id/development" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Development" 
      /> 

     <Button 
      android:id="@+id/carriers" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Carriers" 
      /> 

     <Button 
      android:id="@+id/contactus" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Contact Us" 
      /> 

    </LinearLayout> 

</RelativeLayout> 

aboutus.java

public class aboutus extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.aboutus); 
     } 
} 

aboutus.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" > 

<TextView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Aboutus Page"/> 
</LinearLayout> 

的AndroidManifest.xml這是正確的?

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.globalinfosoft.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= "aboutus"/> 


    </application> 

</manifest> 
+0

意向書I =新意圖(v.getContext(),aboutus.class ); 而不是上面的代碼使用下面的代碼和指令。 Intent i = new Intent(MainActivity.this,aboutus.class); ,你必須聲明關於我們的活動在AndroidManifest.xml中 這樣 <活動機器人:名字=「關於我們」 /> – Manidroid

+0

你有沒有從崩潰日誌? –

+0

我已經在代碼 –

回答

0

MainActivity.java

package com.example.a; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    Button aboutus, development, service, carriers, contactus; 

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

     Button aboutus = (Button) findViewById(R.id.aboutus); 
     aboutus.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       //Starting a new Intent 
       Intent nextScreen = new Intent(getApplicationContext(),aboutus.class); 
       startActivity(nextScreen); 

      } 
     }); 

    } 

    } 

這裏是AndroidManifest.xml中:

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.a.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="com.example.a.aboutus"></activity> 
    </application> 

</manifest> 
+0

感謝工作好@Robi庫馬爾 –

+0

@ user3152655,總是最受歡迎。 –

0

而不是v.getContext()使用'getApplicationContext()'。

Intent i = new Intent(getApplicationContext(),aboutus.class); 
startActivity(i); 
0

下面的代碼和指令試試這個..

Button aboutus=(Button) findViewById(R.id.aboutus); 
     aboutus.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(getApplicationContext(),aboutus.class); 
       startActivity(i); 

      } 

     }); 
0
Intent i = new Intent(v.getContext(),aboutus.class); 
startActivity(i); 

,而不是代碼中使用:

Intent i = new Intent(MainActivity.this,aboutus.class); 
startActivity(i); 

您必須聲明關於我們的活動在AndroidManifest.xml中像這

<Activity android:name= ".aboutus"/> 
0

首先,你應該確保你已經宣佈在AndroidManifest.xml文件的公司簡介活性。

<Activity android:name= "aboutus"/> 

其次,你可以使用

Intent i = new Intent(getApplicationContext(),aboutus.class); 

Intent i = new Intent(MainActivity.this,aboutus.class); 

,而不是

Intent i = new Intent(v.getContext(),aboutus.class); 
相關問題