Stuck on why my program wont work...? - android

Ive been trying to make an app for my girlfriend and I which will allow us to type messages and then allow th other to read it and click the button below to give a response and keep this going back and forth. I've gotten somewhat far I guess but I'm completely stuck on why it wont work now. Ive been working based on the skeleton of this code.
Originally, the code allowed me to open the application and then once I pressed the button at the bottom, it would give me an error saying that the program is no longer able to run and that I had to "force close" it. I fixed that but then when I did, the MainActivity window would keep reappearing when I would click the button. I attempted to fix that but now the application doesn't open anymore
I tried the debugger but I'm not advanced enough to figure out what it really says....
If you would like project folder or somehow upload here, I have no problem doing that. Just trying to make this as a little present for my girl. Thanks in advance!
[edit]
I should mention that there are no syntax errors
[edit]
Manifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chatfriends"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" tools:ignore="MissingPrefix"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".Activity__alex" />
</application>
</manifest>
[edit]
MainActivity:
package com.chatfriends;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_main, menu);
return true;
}
protected void onStart() {
super.onStart();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
public void startActivity_alex(View v){
Intent intent = new Intent(MainActivity.this, Activity__alex.class);
startActivity(intent);
}
}
Edit: Now the app runs, but when I click on the button below it gives me an error.
Here is the catlog

Change
<activity
android:name=".chatfriends.MainActivity"
to
<activity
android:name=".MainActivity"
inside your manifest because you MainActivity is inside com.chatfriends package and not in com.chatfriends.chatfriends

You should define your activity name in the Manifest file as:
android:name=".MainActivity"
instead of android:name=".chatfriends.MainActivity"
Because the error stating the class com.chatfriends.chatfriends.MainActivity is not found, and you have your MainActivity class directly under com.chatfrieds package.
Edit
The error message tells you "NoSuchMethodException" onClick(View). It seems you defined onClick handler or your button in the layout XML file.
You need to add this function in your activity:
public void onClick(View view){
// Add your code here for button click
}

Related

Activity class MainActivity doesn't exist . I dont know why?

When I run my code in my phone, It is not running. I'm worried about it.Can't figure out what the problem is.I have 5 java files(I don't know if I can call it as activity) MainActivity.java, MainActivity2.java, Person.java, DatabaseHandler.java, GetDetails.java. and activity_main.xml, front_page.xml, refer_mail.xml
Its a lengthy post. sorry for that.
This is my console message.
[2015-08-28 19:23:58 - FormDetails] ActivityManager: Error type 3
[2015-08-28 19:23:58 - FormDetails] ActivityManager: Error: Activity class {com.Prasad.formdetails/com.Prasad.formdetails.MainActivity} does not exist.
This is my MainActivity.java File.
package com.Prasad.formdetails;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button register=(Button)findViewById(R.id.register);
Button details=(Button)findViewById(R.id.get_details);
//Register Functionality
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
//Get Details Functionality
details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this, GetDetails.class);
startActivity(intent);
}
});
}
}
This is my AndroidManifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Prasad.formdetails"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:enabled="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".MainActivity2" />
<activity android:name=".GetDetails" />
</application>
</manifest>
Remove android:enabled="false" from Your Application Tag. Then Clean,Rebuild And Restart Your Project .Try this way .I hope it will helps you
android:enabled
Whether or not the Android system can instantiate components of the application — "true" if it can, and "false" if not. If the value is "true", each component's enabled attribute determines whether that component is enabled or not. If the value is "false", it overrides the component-specific values; all components are disabled.
The default value is "true".
package="com.Prasad.formdetails"
you should not use capital letters in package name . Use package="com.prasad.formdetails"
I'd say it's your android:enabled="false" in the application tag. Your package is disabled, hence your activity is disabled, too.

Unfortunately [yourapp], has stopped

I'm using intent method to make a simple app that takes you from main screen [with an enter button] to another screen which has three options. Code sourced online and seems to be error free, though my app crashes saying "Unfortunately [yourapp], has stopped" immediately after i press the button which is meant to take to the the other screen.
This is my first activity code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.enterBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, HomeActivity.class);
startActivity(intent);
}
});
}
}`
And this is my landing screen's activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
I'm really stuck with this issue and any help would be much appreciated. Many thanks in advance.
Every activity needs to be added in your manifest file under application tag. This seems to be the problem in your case.
Try to post you LogCat so that we might get some more information and if you have not yet added your Activity in the manifest, this is the way of adding it (Activities go under application tag)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.southmp3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".HomeActivity"
android:label="#string/app_name" >
</activity>
</application>
all the Activities should be going under the application tag
I had forgotten to register my new activity on the androidmanifest.xml file. That's what sorted my app crash issue.

Cocos2d Android Lock Screen or Leaving Game Forces Restart

I am in the final stages of a Cocos2d - Android game and I am having a problem that I can't figure out.
When I lock my tablet (Nexus 7) or when I hit the home button to leave the game, it restarts. It goes right back to the splash screen like the game is opening all over again. For your reference, here is my Main Activity and my Android Manifest:
public class MainActivity extends Activity {
//To get a static reference to context for shared preferences
private static Context context;
protected CCGLSurfaceView _glSurfaceView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//To get a static reference to context for shared preferences
MainActivity.context = getApplicationContext();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_glSurfaceView = new CCGLSurfaceView(this);
setContentView(_glSurfaceView);
}
//To get a static reference to context for shared preferences
public static Context getAppContext(){
return MainActivity.context;
}
#Override
public void onStart()
{
super.onStart();
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().setDisplayFPS(true);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
CCScene scene = Splash.scene();
CCDirector.sharedDirector().runWithScene(scene);
}
#Override
public void onPause()
{
super.onPause();
CCDirector.sharedDirector().pause();
SoundEngine.sharedEngine().pauseSound();
}
#Override
public void onResume()
{
super.onResume();
CCDirector.sharedDirector().resume();
SoundEngine.sharedEngine().resumeSound();
}
#Override
public void onStop()
{
super.onStop();
SoundEngine.sharedEngine().pauseSound();
}
and
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bigfiresoftware.alphadefender"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/adicon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="bigfiresoftware.alphadefender.MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:hardwareAccelerated="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:configChanges="keyboard|keyboardHidden|orientation"
</manifest>
Any/all help is very much appreciated. Thanks.
add below line in your manifest file to your activity.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
when you locked the screen the orientation of your screen changes.So that your activity destroys and again it creates.If you ad this line your orientation will not change.
The answers above didn't fix it but I finally found the answer. This was a tough one.
If anyone else finds this problem:
Remove the targetSdkVersion in AdnroidManifest.xml. Or change to some others, I haven't tried others but removal worked.
android:minSdkVersion="9"
android:targetSdkVersion="17" /> //git rid of this guy
add this line in your onstop() method
CCDirector.sharedDirector().end();
and you need to write an onDestroy() method to remove all the instances when you close the game.

Android Application Crashing

I'm trying to make my application start another class.
What im trying to learn is how to get another class to run in the background - like if the user opens the application, the application stays running.
I thought if I could try to open another class by using an intent, it would work. When i run my application on the emulator, it just crashes...
Here is the opening:
package omg.justry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
public void onCreate(Bundle savedInstanceState) {
Intent openStartingPoint = new Intent("omg.justtry.PartF**king2");
startActivity(openStartingPoint);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Here is the "PartF**king2" class:
package omg.justry;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
public class PartF**king2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
The thing is, Eclipse doesn't show any errors. I just exported the app and installed it to the emulator using adb.
I also added the class to the AndroidManifest as you see here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="omg.justry"
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="PartF**king2"></activity>
</application>
I think its the manifest now that I look at it but whatever i do, it gets an error or crashes with Eclipse not explaining anything.
In every class, you onCreate(Bundle savedInstanceState) method MUST contain
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
The super is absolutly mandatory, and the setContentView defines the layout for your activity.
And an Activity cannot "run in the background". Start by reading some Android tutorial, and you'll have some clues about what to do.

New intent, how to link to my class?

I'm beginner in Android (and java), and I'm just playing around and want to create new View after clicking a button. Here is my code so far:
Main class
package myTests.homeSpace;
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 TestsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(TestsActivity.this, screen1.class);
startActivity(myIntent);
}
});
}
}
Class to run after clicking button
package myTests.homeSpace;
import android.app.Activity;
import android.os.Bundle;
public class screen1 extends Activity {
#Override
public void onCreate(Bundle SavedBundleInstance)
{
super.onCreate(SavedBundleInstance);
setContentView(R.layout.screen1);
}
}
The problem is, after hitting button I got "Unforunately, Tests has stopped" error. ("Tests" is application name). I know (or guess) the problem is in this line Intent myIntent = new Intent(TestsActivity.this, screen1.class);
I guess my reference to class screen1 is wrong somehow, but I have no idea why. There are no compilation errors nor warnings, layout .xmls shouldn't be wrong.
Could any of you please advice me any solution?
EDIT
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myTests.homeSpace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestsActivity"
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>
CONSOLE OUTPUT
[2012-06-20 19:56:21 - ddms] null
java.lang.NullPointerException
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
I bet if you read the crash log somewhere in there says did you forget to declare this activity in the Manifest?
I agree, not declaring the activity in your Manifest file (AndroidManifest.xml) is most likely your problem. See the android developer page on activities for more information. Here is the relevant description:
Declares an activity (an Activity subclass) that implements part of
the application's visual user interface. All activities must be
represented by elements in the manifest file. Any that are
not declared there will not be seen by the system and will never be
run.
I just posted this so you would have more space to research on your own.
Declare your second activity in the Manifest.xml as:
<activity
android:name=".screen1">
</activity>
Just below the declaration of your first activity.

Categories

Resources