简体   繁体   中英

Linking Activities Using Intents (Android Beginner)

I'm beginning to learn Android and have been reading the "Beginning Android 4 Application Development." (As well as downloading the relevant source code)..

However; I have been trying to create a very simple slideshow with a button labelled "Gallery" which will take me to a new Activity that will show a grid like layout for my photos. However, my application does not do this. When the button is pressed it either crashes the app or refuses to do anything at all.

Manifest

<uses-sdk
    android:minSdkVersion="14"
    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:label="Viwer"
        android:name=".Viwer" >
        <intent-filter >
            <action android:name="com.example.viwer.Gallery" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

</manifest>

Any help would be greatly appreciated, I assume it's something simple but after looking at it for three hours I can't see what it is.

You need to add the Gallery activity to the Android Manifest

<activity
    android:name=".Gallery"
    android:label="Gallery"></activity>

In your manifest you register viewer activity in an intent-filter yet is not declare as a broadcast receiver.

Second event if it was a broadcast receiver it will never launch as you do not send any broadcast by calling startActivity

i think that what your trying to achieve is this:

Please try this:

 ebutton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent intent =  new Intent(MainActivity.this,Viwer.calss )
            startActivity(intent);
            }

    });

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