单击按钮时Android App停止

时间:2013-12-08 17:35:07

标签: android

我正在尝试编写一个在主屏幕上有4个菜单按钮的应用程序。单击此每个菜单按钮,将出现一个新屏幕。目前我只编写代码来迎合第一个点击按钮,但应用程序崩溃/停止。 我认为问题是我没有在Android Manifest文件中声明的TheCompany的活动,但我不知道我会怎么做。 我在Eclipse中编写应用程序,到目前为止,我有以下文件/代码集: MainActivity.Java:

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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

你没有在activity_main.xml中创建像“TheCompany”这样的id,你必须为按钮声明id,然后使用findViewById(...)获取id;

View button = findViewById(R.id.button1);