简体   繁体   English

Android:如何通过按钮启动活动?

[英]Android: How to start an Activity via button?

Noob Android developer here I'm more graphics than code but thought I'd start doing more coding. Noob Android开发人员在这里我比图片更多的图形,但我想我会开始做更多的编码。 Anyway, I have some buttons on my main activity page and I want it so when the button is clicked it opens another class/activity. 无论如何,我的主活动页面上有一些按钮,我想要它,所以当单击按钮时,它会打开另一个类/活动。 I've tried all the methods I've looked up and something is still not working, when I click the button in the emulator it just doesn't do anything, does't forcestop or anything just nothing, someone point me in the right direction please. 我已经尝试了所有我查找过的方法但仍然无法正常工作,当我点击模拟器中的按钮时它什么也没做,不会强制停止或什么都没有,有人指出我在右边方向请。

Code from the main page where the button lives: 按钮所在主页面中的代码:

public class StartingPoint extends Activity {
protected void onCreate(Bundle aim) {
    super.onCreate(aim);

    setContentView(R.layout.main);

    final Button bSL = (Button) findViewById(R.id.bSongList);
    bSL.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent SongList = new Intent(StartingPoint.this, SongList.class);
            StartingPoint.this.startActivity(SongList);
        }
    });
}
}

Manifest 表现

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="myname.appname.SPLASH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".StartingPoint"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="myname.appname.STARTINGPOINT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SongList"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="myname.appname.SONGLIST" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

-L -L

Last 3 lines of logcat just after clicking the button in question.

01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms
01-02 21:59:58.565: D/dalvikvm(348): GC_EXPLICIT freed 8K, 55% free 2591K/5703K,     external 1625K/2137K, paused 520ms

Try this: 试试这个:

Intent songList = new Intent(StartingPoint.this, SongList.class);
startActivity(songList);

Are you getting any errors? 你有任何错误吗? You need to add every Activity you create in the manifest. 您需要在清单中添加您创建的每个活动。 If you have two activities and only the Main in the manifest, that could be you problem. 如果你有两个活动而且清单中只有Main,那可能就是你的问题。

On second thought, I believe your manifest is wrong. 再想一想,我相信你的表现是错的。 Check this. 检查一下。 With the Main (Your starting point) and the Menu which is the second one: 使用Main(您的起点)和Menu是第二个:

<activity android:name="com.activities.Main">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>  
    </activity>
    <activity android:name="com.activities.Menu"></activity>

Try this manifest: 试试这个清单:

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />               
            </intent-filter>
        </activity>
        <activity
            android:name=".StartingPoint"
            android:label="@string/app_name" >           
        </activity>
        <activity
            android:name=".SongList"
            android:label="@string/app_name" >            
        </activity>
    </application>

</manifest>

You don't need to add the filter on each activity. 您无需在每个活动上添加过滤器。 I understood that you have only one entry Activity and that is the splash screen. 我知道你只有一个条目活动,那就是启动画面。 However, you might need to change the manifest to change the ".Splash" and every other Activity to the full path including the package. 但是,您可能需要更改清单以将“.Splash”和其他每个Activity更改为包括包的完整路径。

// whats your package name for your SongList //你的SongList的包名是什么

as your log cats shows 正如你的日志猫所示

01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms

//use full package name in your activity in manifest.xml //在manifest.xml中的活动中使用完整包名称

<activity
        android:name="myname.appname.SongList"
        android:label="@string/app_name" >       
    </activity>

Your activity does not start because it has a NullPointerException in it. 您的活动无法启动,因为它中包含NullPointerException。 (Line 10). (第10行)。 When you do a findViewById on something that is not in your layout, most likely. 当您对布局中不存在的内容执行findViewById时,很可能。

When facing this kind of problems, reading the red lines is usually helpful. 面对这类问题时,阅读红线通常很有帮助。 It basically says: "Hey! you have a null object in SongList line 10 when you try to start it!" 它基本上说:“嘿!当你试图启动它时,你在SongList第10行中有一个空对象!”

Edit 编辑

ImageView ivlogo = (ImageView) findViewById(R.id.ivsonglogo);
ExpandableListView elv1 = (ExpandableListView) findViewById(R.id.elv1);

This can never work. 这永远不会奏效。 This is done during the object initialization, much before onCreate is called, therefore much before you have called setContentView. 这是在对象初始化期间完成的,远在调用onCreate之前,因此在调用setContentView之前很久。

You must initialize your widgets after having set the view. 您必须在设置视图后初始化窗口小部件。

Intent SongList = new Intent(StartingPoint.this, SongList.class);
            startActivity(SongList);

and in Manifest type this 并在Manifest中输入此内容

 <application .....>
<activity android:name=".CustomSurfaceView"></activity>
</application>

For the code try this: 对于代码尝试这个:

Intent songListIntent = new Intent(this, SongList.class);
StartingPoint.this.startActivity(songListIntent);

And for manifest: 并表示:

<activity android:name=".SongList"></activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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