简体   繁体   中英

Android Button Click Crashes App

Intent intent = new Intent();
intent.setClass(MainActivity.this, Line.class);
startActivity(intent);

My cellphone alert "Sorry,The program has stopped working".

Why?

This is the error. http://www.mgiga.com.tw:8080/mo/01.jsp

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

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name="com.sample.activity.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Best if you could share the error you get in your logcat

Intent intent = new Intent(MainActivity.this, Line.class);
startActivity(intent);

Because your program can crash because of a lot of reasons.

May be there is some problem in your second activity("Line.class") not in button click. Make another demo activity with "hello world" & check that still your app crashes or not. Example: if your demo activity named " DemoActivity " Then write code

Intent intent = new Intent(MainActivity.this, DemoActivity.class);
startActivity(intent);

If you give Logcat error then it is better understandable for us.

Edited: add activity in your manifest.xml

<activity
android:name="YourPackageName.Line"
android:label="@string/app_name">

Try this way,hope this will help you to solve your problem.

Intent intent = new Intent();
intent.setClass(MainActivity.this, "YourPackageName.Line");
startActivity(intent);

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Line"/>
    </application>
</manifest>

Line is an Activity you haven't declare it inside manifest . Thats why you get ActivityNotFoundException .

Edit :

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

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name="com.sample.activity.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.dragimagedemo.Line" >
    </activity>
</application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM