Reputation: 135
I am learning android, I am trying to develop game, I have two classes "Starter" and "Board". Starter class contains menu(http://postimg.org/image/dnyvoey2l/). Its Exit and Help buttons are working properly, but when I press "Two Player" option instead of showing board it shows an error (Unfortunately, (Application Name) has stopped). I am sharing code snippet , please suggest solution.
twop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent tow= new Intent(Starter.this, Selector.class);
startActivity(tow);
}
});
Upvotes: 0
Views: 501
Reputation: 1135
You can't show View
using startActivity()
method
Intent tow= new Intent(Starter.this, Board.class);
startActivity(tow);
Board
should be extended from Activity
not from View
class. Create an BoardActivity.java
and extend it from Activity
.
You should then add Board
View either from XML
or programmatically using setContentView();
in your onCreate()
method.
Edit
Don't forget to add new Activity
in your Manifest.xml
file. Like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.application.package.name">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Starter" android:label="@string/app_name"></activity>
<activity android:name=".Selector"></activity>
</application>
</manifest>
Upvotes: 2
Reputation: 585
Below is my idea. The code of OnClick means starting a new activity.
Intent tow= new Intent(Starter.this, Board.class);
startActivity(tow);
However, the Board is a View, not an activity. So, you should make Board inherit from Activity (or create other activity to hold the Board). When creating any activity, make sure to register in manifest.
Upvotes: 2