NoClassDefFoundError (unknown source) when starting new Activity - java

I'm trying to start an activity
`package com.kapzlock.mytestproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity{
#Override
protected void onCreate(Bundle SomeVar) {
// TODO Auto-generated method stub
super.onCreate(SomeVar);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
}
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent openStartingPoint = new Intent("com.kapzlock.mytestproject.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
`
The Manifest xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kapzlock.mytestproject"
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=".Splash"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.kapzlock.mytestproject.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
It compiles without problems, but when I run the Splash.class, I'm getting an Exception in thread "main" java.lang.NoClassDefFoundError: android/app/Activity Error. The package name is com.kapzlock.mytestproject and the class that I'm referencing to is MainActivity.class. Has anybody an idea where the mistake could be in?

Try this:
Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.Mark all checkboxes.Click on Apply and clean the project.
This worked for me.Hope this helps.

Replace your code with following
finally
{
Intent openStartingPoint = new Intent(this,MAINACTIVITY.class);
startActivity(openStartingPoint);
}
Remove following from your manifest.xml in mainActivity tag
<intent-filter>
<action android:name="com.kapzlock.mytestproject.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Related

Keep connectivity broadcast receiver running after application is closed

When I need to use Internet connection and it is not posible I want to set a broadcast receiver which keeps working if the app is closed.
I am using a service for that, but that it is not working. It works fine when the app is on screen or paused but it stops working if I close it. I am not pretty sure if I should use other thing or if I am doing something wrong.
This is my code:
package com.example.ana.exampleapp;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
public class TestsService extends Service {
private static BroadcastReceiver networkChangeReceiver;
private static String TAG = "Service";
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
Log.v(TAG, "Service created");
boolean keep = needTokeepWaiting()
if(!keep)
stopSelf();
}
#Override
public void onDestroy() {
if (networkChangeReceiver != null)
unregisterReceiver(networkChangeReceiver);
networkChangeReceiver = null;
Log.v(TAG, "Service destroyed");
}
private void registerNetworkChangeReceiver() {
networkChangeReceiver = new BroadcastReceiver() {
private String TAG = "NetworkReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Connection changed received");
boolean keep = needTokeepWaiting()
if(!keep)
stopSelf();
}
};
IntentFilter filter = new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkChangeReceiver, filter);
}
}
And my manisfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ana.exampleapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
<activity android:name=".FinishRegisterActivity"></activity>
<activity android:name=".TestActivity"></activity>
<activity android:name=".InformationActivity"></activity>
<service android:name=".TestsService"></service>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
And in the TestActivity when it is necessary I do:
Intent service = new Intent(this, TestsService.class);
startService(service);
What I am doing is similar to what it is suggested here, but it doesn't work:
Keep broadcast receiver running after application is closed
I have also tried to do it without a service and registering in the manifest, but It didn't work neither:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ana.exampleapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity">
</activity>
<activity android:name=".FinishRegisterActivity" >
</activity>
<activity android:name=".TestActivity">
</activity>
<activity android:name=".InformationActivity" >
</activity>
<receiver android:name=".NetworkChangeReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
I have just found the problem. There is a bug in Android 4.4.x which kills background services when closing the app. I was testing my app in Android 4.4.2. Here there is a detailed explanation:
http://www.androidpolice.com/2014/03/07/bug-watch-stopping-apps-on-android-4-4-2-can-silently-kill-related-background-services-a-fix-is-on-the-way/
So my code was right as I have tried in Android 4.2.2 and it works (I have tried overriding onStartCommand() so maybe that was needed).
If you don't want your service to stop when the app is closed, you should override onStartCommand() method doing something like this:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_REDELIVER_INTENT;
}
You can return START_STICKY or START_REDELIVER_INTENT, whatever works better for you

starting point cannot be resolved into a type

So I'm building my very first android app. Trying to get my splash screen to work. But Splash.java is showing an error " StartingPoint cannot be resolved to a type.
Here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arjun.add"
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=".Splash"
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=".StartingPoint"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.arjun.add.StartingPoint" />
<category android:name="android.intent.category.DEDAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And here is my Splash.Java
package com.arjun.add;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle ArjunSharma) {
// TODO Auto-generated method stub
super.onCreate(ArjunSharma);
setContentView(R.layout.splash);
Thread timer = new Thread (){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent(Splash.this, StartingPoint.class );
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
Hope you guys can help me out
try this one:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arjun.add"
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.arjun.add.Splash"
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.arjun.add.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
EDIT: note that put MainActivity as your starting point, that is first activity after splash screen
hope it helps
In your manifest defining StartingPoint you have the category defined as android.intent.category.DEDAULT, should be android.intent.category.DEFAULT.

Intent using thread is not working

I know I already asked this, but I didn't get a sufficient answer. Im trying to start an activity, but the emulator stays on the first activity. Ive tried all ways to do it but it never works. The youtube videos show that it should work but it never does. Is there something missing or is there anything wrong with the following code?
//First Activity:
package com.mtprogramming.magicsquaresgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
public class Opening extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
Intent open = new Intent("com.mtprogramming.magicsquaresgame.MENU");
startActivity(open);
}
}
};
timer.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.opening, menu);
return true;
}
}
//Second Activity:
package com.mtprogramming.magicsquaresgame;
import android.app.Activity;
import android.os.Bundle;
//Created by suprav on 7/11/13.
public class Menu extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
}
//Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mtprogramming.magicsquaresgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
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.mtprogramming.magicsquaresgame.Opening"
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.mtprogramming.magicsquaresgame.Menu"
android:label="#string/title_activity_menu"
android:parentActivityName="Opening" >
<intent-filter>
<action android:name="com.mtprogramming.magicsquaresgame.MENU"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="Opening" />
</activity>
</application>
</manifest>
Try this:
Intent open = new Intent(Opening.this, Menu.class);
startActivity(open);
I think i found the issue, seems like metadata property is not being properly used, hences activity its not being started, this is the proper way to use the property:
<activity android:name=".TestActivity" >
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".TestParentActivity">
</meta-data>
</activity>
So, seems like you are missing a dot in "android:value="Opening"
Regards!

Reading the incoming phone number

i am a new android developer . i am writing a application that get the incoming caling number and do some stuff.
i have one Activity " Main_Activity "
the code in Main_Activity is this :
package com.example.callchecker;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class Main_Activity extends BroadcastReceiver{public Main_Activity() {
// TODO Auto-generated constructor stub
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
Bundle extras=intent.getExtras();
if (extras !=null){
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG",state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
String phonenumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("MY_DEBUG_TAG", phonenumber);
}
}
} catch (Exception e) {
// TODO: handle exception
Log.w("MY_DEBUG_TAG", e);
}
}
}
and the AndroidManifist.xml is this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callchecker"
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.example.callchecker.Main_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="Main_Activity" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
I just want to get the number and write it to LogCat
But it is not working , i can not see any line in LogCat
when i click on app icon it say
" Unfortunately call checker has been stopped "
Make sure the following permission is used in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>

why BroadcastReceiver don't catch the Action?

Hi I am working on BroadCastReciver. There are two way to define BroadCastReciver.First one is using Java code and Second one is define in AndroidManifest.xml using . In my code second one is don't work properly.please tell where am i goes wrong.
public class HotelReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String dActionName = intent.getAction();
Log.i("My Rceiver ", intent.getAction());
if (dActionName.equals(Intent.ACTION_SCREEN_ON)) {
Toast.makeText(context, "SCREEN ON", Toast.LENGTH_SHORT).show();
} else if (dActionName.equals(Intent.ACTION_SCREEN_OFF)) {
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hotelsecurity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10"
android:maxSdkVersion="15" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<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>
<receiver
android:name=".HotelReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
</application>
</manifest>
I think your reciever should read
<receiver
android:name="HotelReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
no dot "."
Just use this code inside of onCreate() for your activity,
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
HotelReceiver mReceiver = new HotelReceiver(this);
registerReceiver(mReceiver, filter);

Categories