Android: How to get started with the button?

Noob Android developer here I have more graphics than code, but I thought I would start doing more coding. Anyway, I have several buttons on my main activity page, and I want a different class / activity to open when I click the button. I tried all the methods that I was looking for and something still doesn’t work, when I press the button in the emulator, it just doesn’t do anything, is there ancestors or something is just nothing, someone is pointing me in the right direction you are welcome.

The code from the main page on which the button is located:

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

 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 
+4
source share
5 answers

Try the following:

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

Are you getting any errors? You need to add all the actions that you create in the manifest. If you have two actions and only the main one in the manifest, this can be a problem.

On the other hand, I think your manifest is wrong. Check this. With Main (Your starting point) and Menu, which is second:

 <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 do not need to add a filter for each action. I realized that you only have one Activity entry, and this is a splash screen. However, you may need to modify the manifest to change the ".Splash" and any other action to the full path, including the package.

+4
source

// your package name for your playlist

as your cats show magazines

 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 the full name of the package in your activity in manifest.xml

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

Your activity does not start because it has a NullPointerException. (Line 10). When you do findViewById on what's not in your layout, most likely.

When you encounter such problems, reading the red lines is usually helpful. Basically he says: "Hey, you have a null object on line 10 of SongList when you try to run it!"

Edit

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

It will never work. This is done during the initialization of the object, much before onCreate is called, so long before you call setContentView.

You must initialize your widgets after setting the view.

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

and in type Manifest this

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

For code, try the following:

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

And for the manifest:

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

Source: https://habr.com/ru/post/1388988/


All Articles