Android: Application is crashing On Startup

2019-09-21 19:42发布

问题:

I am a novice and I'm just creating a simple Android app in which there are 2 buttons that take you to different pages when clicked, which have different functions.

However, my app crashes on start-up, simply saying

Unfortunately [app name] has stopped

and I don't understand

As I have barely made any changes to my MainActivity.java, the code seems to be well written, and eclipse displays no errors... Any help, please? I have searched around Stack, but I can't find anything specific to this...

Here is my MainActivity.java:

package com.example.helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

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

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**Called upon when the user clicks the 'Abbreviations' button*/
public void listDefinitions (View view){
    Intent intent = new Intent(this, ListDefinitions.class);
    startActivity(intent);
}

/**Called upon when the user clicks the 'Assistant' button*/
public void openAssistant (View view){
    Intent intent = new Intent(this, Assistant.class);
    startActivity(intent);
}

}

Here is my 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="com.example.legittest.MainActivity" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="@string/intro_title"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="21.5sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="@string/module_selection"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/definitions_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="111dp"
    android:text="@string/definitions"
    android:onClick="listDefinitions" />

<Button
    android:id="@+id/assistant_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/definitions_button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="56dp"
    android:text="@string/assistant"
    android:onClick="openAssistant" />

<TextView
    android:id="@+id/definitions_explainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/definitions_button"
    android:layout_centerHorizontal="true"
    android:text="@string/definitions_info"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/greycolor" />

<TextView
    android:id="@+id/pa_explainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/definitions_explainer"
    android:layout_below="@+id/assistant_button"
    android:text="@string/assistant_info"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/greycolor" />

</RelativeLayout>

My ListDefinitions.java & Assistant.java are untouched at this point.

My Manifest:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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=".ListDefinitions"
        android:label="@string/title_activity_list_definitions"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.theocratichelper.MainActivity" />
    </activity>
    <activity
        android:name=".PreachAssistant"
        android:label="@string/title_activity_preach_assistant"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.theocratichelper.MainActivity" />
    </activity>
</application>

</manifest>

回答1:

It's your manifest, replace the "last" activity in your manifest by this :

<activity android:name=".Assistant" android:label="@string/title_activity_preach_assistant" android:parentActivityName=".MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.theocratichelper.MainActivity" /> </activity>

Your class name is Assistant and not PreachAssistant i presume.



回答2:

Just replace the last activity in Android Manifest by this.

<activity android:name=".Assistant" android:label="@string/title_activity_preach_assistant" </activity>