How To Add Second Activity In Android

2020-07-02 08:02发布

Hey everyone! Im trying to add a second activity to my android project, but im not sure how to add the activity files exactly?

Ive added a class here "Name/Scr/PackageName/MyClassHere" but Im not sure if that the correct thing to do or place to put it, because there isnt a XML file either.

Im using this code to open a new screen, http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html And I get 2 error's. 1 at runtimeon this line: Intent i = new Intent(Coinparison.this, ResultsScreen.class); it says it cant find my activity.

And the other error here setContentView(R.layout.ResultsScreen); which says "ResultsScreen" cannot be resolved.

Not sure whats wrong, but any help is great! :)

5条回答
Rolldiameter
2楼-- · 2020-07-02 08:43

There is a wizard in eclipse now for adding activities, just right click on your project, go to new -> other -> android -> Android Activity

This will create the class, layout and manifest entry.

查看更多
爷的心禁止访问
3楼-- · 2020-07-02 08:46

Here is a wizard in Android Studio now for adding New Activity, just right click on your project, go to new -> Application Name ->Select Phone and Tablet -> Select Minimum API Level->Next->Blank Activity->Next->Activity Name->Next-> Finish

This will create the class, layout and manifest entry.

查看更多
该账号已被封号
4楼-- · 2020-07-02 08:51

i think u need add this activity in manifest file like this

 <application  balblabla>
        <activity android:name=".firstActivity">
                  android:label="@string/app_name"
             //bla bla bla
        </activity>
    <activity android:name=".secondActivity"/>

    </application>

see link again and find this : //AndroidManifest.xml there u can see text which writed in bold mode

<activity class=".Screen2" android:label="Screen 2">
      </activity>
查看更多
神经病院院长
5楼-- · 2020-07-02 08:52

Answer for Error in """ setContentView(R.layout.ResultsScreen); which says "ResultsScreen" cannot be resolved """

Go to second.java (Related to second activity) file and import missing classes.

In Eclipse, press Ctrl + Shift + O to import missing classes (Cmd + Shift + O on Mac).

查看更多
再贱就再见
6楼-- · 2020-07-02 08:56

Add your activity to AndroidManifest.xml

<activity android:name="ResultsScreen"
   android:label="@string/app_name">
   <intent-filter>
       <action android:name="android.intent.action.VIEW" />
   </intent-filter>
</activity>
查看更多
登录 后发表回答