Intent To Start Activity Not Working - java

I am trying to start an android activity name 'TheGame' from the activity 'SelectionFragment' with a button who's ID is 'startGame'
The SelectionFragment file's public class is :
public class SelectionFragment extends Fragment {
This is the code that contains the Intent:
public void startTheGame (View view) {
Intent intent = new Intent(this, TheGame.class);
startActivity(intent);
}
And this is what my Manifest file looks like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexlamond.higherorlower"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/highlowlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.alexlamond.higherorlower.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>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<activity android:name="com.facebook.LoginActivity" >
</activity>
<activity
android:name="com.alexlamond.higherorlower.TheGame"
android:label="#string/title_activity_the_game" >
</activity>
</application>
My issue is that the Intent will not work, when I got to Eclipse's suggestion of how to fix it, it says:
The constructor Intent(SelectionFragment, Class) is undefined

Your onClick attribute must match by exact naming to a public method that has the same name. You have attribute android:onClick="TheGame" and the method signature is public void startTheGame (View view). Change the onClick attribute value to "startTheGame".
However, I would rather use View.OnClickListener instead of setting the onClick attribute because:
On the long run you may reuse/move/change this layout and you will end-up with the same error.
You might do some refactoring and you might change this method by accident or not, and the compiler will not complain.
Setting this type of linkage between UI and model seems to me a violation of MVC.

In case you are starting activity from your fragment, try this:
public void startTheGame (View view) {
Intent intent = new Intent(getActivity(), TheGame.class);
startActivity(intent);
}

Related

Insert NewChidldActivity in-Between MainActivity and FirstChildAcvitity And Change Default ChildActivity to the NewChildActivity

Fellow Friends,
thank you all for your usual support.
I have developed an an examination app with login functionality.
Previously, the app had 3 Activities (MainActivity, WelcomeScreenActivity and QuestionsAcvitity). This app is working perfectly.
However, when the user logs in from the MainActivity, the WelcomeScreenActivity shows.
But I need to INSERT another activity (PROFILE_ACTIVITY) between the MainActivity and the WelcomeScreenActivity so that, when the user logs in successfully, the app will display the user's PROFILEACTIVITY.
Pictures below the page
Previous Code before the new ProfileActivity
MainActivity.java code
#Override
protected void onPostExecute(Void result){
if (!responseData.equals( "User Not Found" )) {
message = "Welcome";
data = responseData;//store the user id from the server in data
//Bundle bundle = new Bundle();//bundle the message and parse it to the next activity
//bundle.putString("dispMsg", message);//bundle the message using the variable dispMsg
//intent.putExtras(bundle);
startActivity(intent);
statusBar.setText(message);
} else {
message = responseData;//"Incorrect Username or Password. Try again!";
statusBar.setText(message);
}
super.onPostExecute(result);
}
DisplayWelcomeScreenActivity.java code
This activity displays after a successful login.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_welcome_screen);
// Get the Intent that started this activity and extract the string
intent = getIntent();
}
To pass from this WelcomeActivity to the next QuestionsActivity, the code below does that on buttonclick() event
public void courseClicked(View v) {
String course = "";
Intent intent = new Intent(this, CSS_342_Questions.class);
int qNum = 0;
switch (v.getId()){
case R.id.buttonCSS342:
course = "CSS 342";
qNum=1;
break;
case R.id.buttonCSS352:
course = "CSS 352";
qNum=1;
break;
case R.id.buttonCSS354:
course = "CSS 354";
qNum=1;
break;
case R.id.buttonCSS356:
course = "CSS 356";
qNum=1;
break;
case R.id.buttonCSS381:
course = "CSS 381";
qNum=1;
break;
case R.id.buttonPCR362:
course = "PCR 362";
qNum=1;
break;
}
Bundle bundle = new Bundle();
bundle.putString("dispCode", course);
bundle.putInt("qNum", qNum);
//bundle.putString("dispMsg", "Welcome");
intent.putExtras(bundle);
startActivity(intent);
}
The QuestionsActivity gets displayed using the code below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_css_342__questions);
// Get the Intent that started this activity and extract the string
intent = getIntent();
bundle = getIntent().getExtras();
showCode = bundle.getString("dispCode");
qNum = bundle.getInt("qNum");
}
This code below is the Manifest xml that links the 3 activities together for proper navigation.
AndroidManifest.XML code
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.examinationportal">
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
<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/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayWelcomeScreen"
android:label="#string/welcome_screen_title"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".CSS_342_Questions"
android:allowTaskReparenting="true"
android:label="#string/question_screen"
android:parentActivityName=".DisplayWelcomeScreen">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".DisplayWelcomeScreen" />
</activity>
<activity
android:name=".RegisterUserActivity"
android:label="#string/new_user_form"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
Now, I want to insert a new activity (ProfileActivity) between the MainActivity and the DisplayScreenActivity.
I have changed the AndroidManifest xml code below.
The app is crashing. The LogCat shows the line where the app is crashing **StartActivity(intent)** in MainActivity.java; line 153
The Code After the new Activity
The MainActivity code still remains the same.
The new AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.examinationportal">
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
<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/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProfileAcvitity"
android:label="#string/student_profile"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".RegisterUserActivity"
android:label="#string/new_user_form"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
New ProfileActivity.java code
public class ProfileAcvitity extends AppCompatActivity {
public Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_acvitity);
intent = getIntent();
}
LOGCAT INFORMATION
04-25 13:53:33.680 14789-14789/com.example.examinationportal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.examinationportal, PID: 14789
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.examinationportal/com.example.examinationportal.DisplayWelcomeScreen}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4034)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3986)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4325)
at android.app.Activity.startActivity(Activity.java:4293)
at com.example.examinationportal.MainActivity$GetText.onPostExecute(MainActivity.java:153)
at com.example.examinationportal.MainActivity$GetText.onPostExecute(MainActivity.java:83)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5763)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Please help me solve this problem.
Thank you.
<activity android:name=".DisplayWelcomeScreen"
android:label="#string/student_welcome"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Adding those lines in your AndroidManifest.xml file will solve your problem.

How to call an Activity class from non-Activity in Android

I am trying to build library for android, in my library i am trying to call a Activity class from non-Activity class.
This is my non-Activity class
public class Library1 {
Activity activity;
public Library1(Activity activity){
this.activity = activity;
}
public void captureImage() {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
Intent i1 = new Intent ( activity, Aa.class);
context.startActivity(i1);
}
}
This my Activity class which i want to call
public class Aa extends Activity {
public void someMethod()
{
}
}
But when i call Activity class from non-Activty it is showing following error and application is crashing
android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.example.androidcamera3/com.example.library1.Aa}; have you declared this
activity in your AndroidManifest.xml?
This is my AndroidManifesto.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.library1"
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.library1.Library1"
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.library1.Aa" android:label="#string/app_name" />
</application>
There is a spell mistake. [ acivity => activity ]
Change
<acivity android:name="com.example.library1.Aa" android:label="#string/app_name" />
to
<activity android:name="com.example.library1.Aa" android:label="#string/app_name" />
Activity is defined in package com.example.library1, however it is launched as com.example.androidcamera3/com.example.library1.Aa.

Apk android ActivityNotFoundException

I want to run an implicitly tried with the following code:
MenuActivity.java
public class MenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button btn = (Button) findViewById(R.id.btnVideo);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri intentUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pp);
intent.setDataAndType(intentUri,"audio/mp3");
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.powerpump.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity" />
</application>
You need to specify more parameters in AndroidManifest.xml?
Thanks.
I guess you simply have to register your MenuActivity in you AndroidManifest.xml as demonstrated here.
Looking at your code, it looks like you created a MenuActivity class after creating an android project with default settings and MenuActivity is your main activity(Launcher) class.
Just replace the MainActivity with MenuActivity in your AndroidManifest.xml file and delete
<activity android:name=".MenuActivity" />
OR
Replace the class name to MainActivity from MenuActivity in your java code as follows
public class MainActivity extends Activity {
......
OR
use this as your android manifest.xml file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.powerpump.MenuActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Android unexcpected error

I am a beginner of Android and i need a bit hints and help. I am using following code
public class WifiHotSpotActivity extends Activity {
private Button adnew = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
adnew = (Button) findViewById(R.id.addNewBtn);
adnew.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(v.getContext(), addNew.class);
startActivityForResult(myIntent, 0);
}
});
}
The error i am receiving is
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kahaf.wifiHotSpot/com.kahaf.wifiHotSpot.addNew}; have you declared this activity in your AndroidManifest.xml?
if anyone can tell me what is the problem.
You should define your activity in the manifest file. Here's an example how your manifest should look after the addition of that activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:label="#string/app_name"
android:name=".FooActivity"
android:configChanges="keyboardHidden">"
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YourActivityNameHere"
android:label="#string/app_name"
>
</activity>
<activity
android:name=".AnotherActivity"
android:label="#string/app_name"
>
</activity>
</application>
</manifest>
put this to AndroidManifest.xml
<activity android:name=".WifiHotSpotActivity"/>
It looks like you've not defined the Activity Element in the manifest xml for WifiHotSpotActivity. Without this you will be unable to launch this activity.
go to manifest the application tag.. add activity "addNew" which you are calling in button click..

Repaint Activity through Service

I use my Main Activity to paint Text. Now Ive a Service in Background that listens to a bluetooth device. If a button on the device is pressed I insert values in the local database and want to repaint the correct value in my Main Activity. Everything works fine - just the repaint does not work, because i cant call the invalidate() method from my Service.
I tried with a BroadCastReceiver but i think i misunderstood something.
This is my Method in my Service "ControllerService"
private void insertToDB(int iWert, char c) {
if(zt.getJoystickMoved().x == iWert) {
sendOrderedBroadcast(i,null);
Log.i("Broadcast","send!");
//in this block i want to notify the Activity
}
}
Here is my Main Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(resetReceiver, new IntentFilter("my.intent.INTENT_NAME"));
}
public BroadcastReceiver resetReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast", "got Broadcast!");
MainActivity.this.cc.invalidate();
//cc.invalidate() repaints my Activity
//cc is a View with the paint Logic inside
this.setResultCode(Activity.RESULT_OK);
}
}
My Broadcast Receiver in my Main Activity does not receive anything ...
I hope someone could help me with my Problem.
In my AndroidManifest is this code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.countV1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission
android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name="com.test.data.ListHelper">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="my.intent.INTENT_NAME"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="PreferencesActivity" android:name="PreferencesActivity"></activity>
<activity android:name="ZtPreferencesActivity" android:label="ZtPreferencesActivity"></activity>
<activity android:name="customizePreferences"></activity>
<activity android:name="ControllerActivity"></activity>
<service android:name="com.test.services.ControllerService"></service>
<activity android:name="BatteryActivity"></activity>
</application>
</manifest>

Categories