complete newbie here .. im creating my first android quiz game.
i would like to start another activity if button is clicked. (from mainactivity to quizactivity)
i used intent for this but when i try to run it on device and i click on the button, the game crashes . a little help please
here is my main activity
package com.example.clicktothink;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn= (Button)findViewById(R.id.play_btn);
btn.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, QuizActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
});
}
}
and here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clicktothink"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.clicktothink.QuizActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.clicktothink.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.clicktothink.MainActivity" />
</activity>
<activity
android:name="com.example.clicktothink.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>
</application>
</manifest>
please check your mainfest and change that to this .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clicktothink"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.clicktothink.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:name="com.example.clicktothink.QuizActivity"/>
</application>
</manifest>
btn.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, QuizActivity.class);
startActivity(intent);
}
});
instead of this
Intent intent = new Intent(MainActivity.this, QuizActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
change to this
Intent intent = new Intent(MainActivity.this, QuizActivity.class);
startActivity(intent);
Related
When I try to run the code I keep getting this error "The activity must be exported or contain an intent-filter".
I have set the small drop down beside the green run button to "app" and I have also put an intent-filter in my activity in the manifest. But this doesn't help. Hopefully someone can help me.
The code below is of my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brian.project">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginSignupActivity.LoginActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
</activity>
<activity android:name=".HomeActivity"></activity>
<activity android:name=".LoginSignupActivity.SignupActivity"></activity>
<!--<activity android:name="WeightActivity"></activity>-->
</application>
</manifest>
My MainActivity code:
package com.example.brian.project;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.brian.project.LoginSignupActivity.LoginActivity;
import com.example.brian.project.LoginSignupActivity.SignupActivity;
public class MainActivity extends AppCompatActivity {
Button Login, Create_Account;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Login=(Button) findViewById(R.id.btn_login);
Button Create_Account=(Button) findViewById(R.id.btn_create_account);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
openLoginActivity();
}
});
Create_Account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
openSignupActivity();
}
});
}
public void openLoginActivity() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
public void openSignupActivity() {
Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);
}
}
Try to use android:exported="true" in the <activity> tag:
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have problem with my manifest. When I try to open the second Activity, I get the error android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.xxxxx.monapplication.APPLICATION.OWNERREGISTRATION }
Find bellow my Manifest .xml contain
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxxxx.monapplication" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" />
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:supportsRtl="true" android:theme="#style/AppTheme" >
<activity android:name="com.xxxxx.monapplication.application.Accueil" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.xxxxx.monapplication.application.OwnerRegistration" >
<intent-filter>
<action android:name="android.intent.action.APPLICATION.OWNERREGISTRATION" />
<category android:name="android.intent.category.APPLICATION.DEFAULT" />
</intent-filter>
</activity>
An the part of my code to open second activity
btnMel.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent("com.xxxxx.monapplication.APPLICATION.OWNERREGISTRATION");
startActivity(i);
}
}
Please help me to find the error.
You can call activity by its class name, then put your custom intent to setAction.
private String YOUR_ACTION = "com.xxxxx.monapplication.APPLICATION.OWNERREGISTRATION"
//uou can use this instead.
//Intent i = new Intent(this /* caller context */, OwnerRegistration.class);
Intent i = new Intent();
i.setAction(YOUR_ACTION);
startActivity(i);
try this code..
Intent myIntent = new Intent(YourActivity.this, OWNERREGISTRATION.class); startActivity(myIntent);
I am having errors when attempting to add an onclick listener button to my current code. The original post where I found the code is listed below. Your assistance is greatly appreciated. I am comfortable with Java but still have trouble navigating with the Eclipse application. If anyone has any documentation that could help better prepare me that would be helpful.
android eclipse button OnClick event
MainActivity.java
Button btn = (Button) findViewById(R.id.btnPlay);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
});
public void myClick(View v) {
Intent intent = new Intent(**this, Swipe.class**);
startActivity(intent);// for calling the activity
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.apptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.apptest.MainActivityAppTest"
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:name=".Swipe"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
I think you are getting a compilation error right?
In this line Intent intent = new Intent(**this, Swipe.class**);
You should type Intent intent = new Intent(MainActivity.this, Swipe.class);
that should work
I am trying to open a splash activity followed by main activity. But i am getting the following error in logcat.
01-20 16:46:45.568: E/AndroidRuntime(29579): FATAL EXCEPTION: main
01-20 16:46:45.568: E/AndroidRuntime(29579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cyoa.necroprelude/com.cyoa.necroprelude.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.cyoa.necroprelude.CHARACTER }
Activity code - Main Activity
package com.cyoa.necroprelude;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
Intent openStartingPoint;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
run();
}
private void run() {
Intent openStartingPoint = new Intent("com.cyoa.necroprelude.CHARACTER");
startActivity(openStartingPoint);
}
}
Activity code - Character Activity
package com.cyoa.necroprelude;
import android.app.Activity;
import android.os.Bundle;
public class Character extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.character);
}
}
here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cyoa.necroprelude"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cyoa.necroprelude.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:name="com.cyoa.necroprelude.Character"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.CHARACTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Change
Intent openStartingPoint = new Intent("com.cyoa.necroprelude.CHARACTER");
to
Intent openStartingPoint = new Intent(this, CHARACTER.class);
String CUSTOM_ACTION = "android.intent.action.CHARACTER";
openStartingPoint.setAction(CUSTOM_ACTION);
When creating an intent that opens another screen make sure it looks something like the following
Intent intent = new Intent(thisScreen.this, newScreen.class);
startActivity(intent);
I'm very new in android and i am working on a app.
I meet problem in linking the 2nd page to the 3rd page while clicking the button. I had tried to solve the problem but it do not work. Below is my AndroidManifest.xml
`
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fyp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BelajarActivity"
android:label="#string/title_activity_main">
</activity>
<activity
android:name=".KnamaActivity"
android:label="#string/title_activity_main">
</activity>
</application>
</manifest>
below is BelajarActivity.java
package com.example.fyp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class BelajarActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.belajar);
Button bnama = (Button) findViewById(R.id.knama);
bnama.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent namaIntent = new Intent(BelajarActivity.this,KnamaActivity.class);
startActivity(namaIntent);
}
});
}
}
This
bnama.setOnClickListener(new Button.OnClickListener() { //this is wrong
...
});
should be
bnama.setOnClickListener(new View.OnClickListener() {
..
});
OnClickListener should be of type View
Just change this line
bnama.setOnClickListener(new View.OnClickListener() {