2013-12-08 4 views
0

Я пытаюсь написать приложение с 4 кнопками меню на главном экране. При нажатии этой кнопки меню появляется новый экран. На данный момент у меня есть только код, который предназначен для первой нажатой кнопки, но это приложение сбой/остановки. Я думаю, проблема в том, что у меня нет активности TheCompany, объявленной в файле манифеста Android, но я не знаю, как это сделать. Я пишу приложение в Eclipse, и до сих пор у меня есть следующие файлы/наборы кода: MainActivity.Java:Android приложение останавливается при нажатии кнопки

public class MainActivity extends Activity { 
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 

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

     View button = findViewById(R.id.TheCompany); 
     button.setOnClickListener(new OnClickListener(){ 
      public void onClick (View v){ 
       startIntent(); 
      } 
     }); 

     //getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     // If your minSdkVersion is 11 or higher, instead use: 
     // getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main_activity_actions, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    /** Called when the user clicks the Company button */ 
    private void startIntent() { 
     // Do something in response to button 
     Intent intent = new Intent(this, TheCompany.class); 
     startActivity(intent); 
    } 
} 

Activity_main.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" 
android:padding="10dp" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="The Company" 
     android:id="@+id/TheCompany"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Services"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Contacts" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Structure" /> 
</LinearLayout> 
</LinearLayout> 

Тогда у меня есть Thecompany Java файл:

public class TheCompany extends Activity 
{ 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.company); 
    } 
} 

Android.Manifest выглядит следующим образом:

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

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

<application 
    android:allowBackup="true"   
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:icon="@drawable/logo" 
     android:name="com.example.myfirstapp.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.myfirstapp.DisplayMessageActivity" 
     android:label="@string/title_activity_display_message" 
     android:parentActivityName="com.example.myfirstapp.MainActivity" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.myfirstapp.MainActivity" /> 
    </activity> 
</application> 
</manifest> 
+3

«Я думаю, проблема в том, что у меня нет активности TheCompany, объявленной в файле манифеста Android, но я не знаю, как бы я это сделал». Сделайте это так же, как и ваши другие «Деятельности». Вы еще пытались добавить его? – codeMagic

+0

Как насчет только чтения документации? это очень ясно объяснено. – Simon

+0

Саймон, спасибо, что Документация? Я бы это прочитал, если бы смог найти его. – user3079872

ответ

1

Просто добавьте это в Manifest «приложения» метки:

<activity 
    android:name="com.example.myfirstapp.TheCompany" 
    android:label="@string/app_name" > 
</activity> 

Вы должны объявить каждое действие в файле манифеста.

Результат таков:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.myfirstapp" 
android:versionCode="1" 
android:versionName="1.0" > 

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

<application 
    android:allowBackup="true"   
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:icon="@drawable/logo" 
     android:name="com.example.myfirstapp.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.myfirstapp.TheCompany" 
     android:label="@string/app_name" > 
    </activity> 
    <activity 
     android:name="com.example.myfirstapp.DisplayMessageActivity" 
     android:label="@string/title_activity_display_message" 
     android:parentActivityName="com.example.myfirstapp.MainActivity" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.myfirstapp.MainActivity" /> 
    </activity> 
</application> 
</manifest> 
+0

Спасибо Квентин! Теперь я это понимаю! Огромное спасибо! Оно работает. – user3079872

0

вы did`t создать любой идентификатор, как «Thecompany» в activity_main.xml, вы должны объявить идентификатор для кнопки, а затем получить идентификатор с помощью findViewById (...);

View button = findViewById(R.id.button1); 
Смежные вопросы