I just started android development in 2 days and this is the error that I cannot solve "Unfortunately, [App] has stopped".
This is what I'm planning to do with the APP.
show the UI that will tell the user that by pressing ok the app will start making a log file.
when the OK button is pressed, it will run in the background (don't need UI) and will wait for the notification from other app and will write the text from the notification to the log file.
This is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationnotifier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
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.notificationnotifier.GetNotification"
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.notificationnotifier.MonitorNotification"
android:label="#string/app_name" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
This is GetNotification.java
public class GetNotification extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_notification);
Button okButton = (Button) findViewById(R.id.OKButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");
startActivity(getNotification);
}
}) ;
Button cancel = (Button) findViewById(R.id.Cancel);
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}) ;
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
This is MonitorNotification.java
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;
public class MonitorNotification extends AccessibilityService{
#Override
public void onAccessibilityEvent(AccessibilityEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void onInterrupt() {
// TODO Auto-generated method stub
}
#Override
protected void onServiceConnected() {
// TODO Auto-generated method stub
// super.onServiceConnected();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.feedbackType = 1;
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.notificationTimeout = 100;
setServiceInfo(info);
}
}
This is the logcat
[updated]
02-04 10:31:17.947: E/Trace(1108): error opening trace file: No such file or directory (2)
02-04 10:31:18.648: D/gralloc_goldfish(1108): Emulator without GPU emulation detected.
02-04 10:31:21.128: D/AndroidRuntime(1108): Shutting down VM
02-04 10:31:21.148: W/dalvikvm(1108): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-04 10:31:21.169: E/AndroidRuntime(1108): FATAL EXCEPTION: main
02-04 10:31:21.169: E/AndroidRuntime(1108): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.notificationnotifier/com.example.notificationnotifier.MonitorNotification}: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.os.Looper.loop(Looper.java:137)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-04 10:31:21.169: E/AndroidRuntime(1108): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 10:31:21.169: E/AndroidRuntime(1108): at java.lang.reflect.Method.invoke(Method.java:511)
02-04 10:31:21.169: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-04 10:31:21.169: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 10:31:21.169: E/AndroidRuntime(1108): at dalvik.system.NativeStart.main(Native Method)
02-04 10:31:21.169: E/AndroidRuntime(1108): Caused by: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-04 10:31:21.169: E/AndroidRuntime(1108): ... 11 more
02-04 10:31:23.268: I/Process(1108): Sending signal. PID: 1108 SIG: 9
Replace this line:
Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");
by this one:
Intent getNotification = new Intent(GetNotification.this,
MonitorNotification.class);
Hope it helps.
Add the below code in your manifest file:
<service android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
. . .
</service>
Declaration
An accessibility is declared as any other service in an AndroidManifest.xml but it must also specify that it handles the "android.accessibilityservice.AccessibilityService" Intent. Failure to declare this intent will cause the system to ignore the accessibility service. Additionally an accessibility service must request the BIND_ACCESSIBILITY_SERVICE permission to ensure that only the system can bind to it. Failure to declare this intent will cause the system to ignore the accessibility service. Following is an example declaration:
<service android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
. . .
</service>
I know now what the problem was, my friend told me that it should be startService not startActivity in the getNotification.java
the correct way to call it is
startService(getNotification);
not
startActivity(getNotification);
I'm extending AccesibilityService in the class in MonitorNotification.java. :D
Related
I'm trying unzip some files in background, so I use IntentService like in google's tutorial. My service class declared in AndroidManifest like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.osmdroid">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:configChanges="orientation|screenSize|keyboardHidden"
android:hardwareAccelerated="true"
android:icon="#drawable/ecn_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MapActivity"
android:icon="#drawable/ecn_icon"
android:label="test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings">
</activity>
<service
android:name=".UnZipService"
android:exported="false"/>
</application>
In activity, I have
IntentFilter intentFilter = new IntentFilter(DownloadManager
.ACTION_DOWNLOAD_COMPLETE);
receiverDownloadComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long reference = intent.getLongExtra(DownloadManager
.EXTRA_DOWNLOAD_ID, -1);
if (myDownloadReference == reference) {
...
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startActivity(mServiceIntent);
break;
...
}
cursor.close();
}
}
};
registerReceiver(receiverDownloadComplete, intentFilter);
And service here:
public class UnZipService extends IntentService {
public UnZipService() {
super("UnZipService");
}
#Override
protected void onHandleIntent(Intent workIntent) {
String dataString = workIntent.getDataString();
Log.v("IntentURI", dataString);
Toast.makeText(this, "Installing....", Toast.LENGTH_SHORT).show();
}
It should show toast just for test, but I always get an error:
01-29 19:08:25.740 15521-15521/org.osmdroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=org.osmdroid (has extras) } in org.osmdroid.SettingsActivity$1#21292650
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5147)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.osmdroid/org.osmdroid.UnZipService}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
at android.app.Activity.startActivityForResult(Activity.java:3404)
at android.app.Activity.startActivityForResult(Activity.java:3365)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:3600)
at android.app.Activity.startActivity(Activity.java:3568)
at org.osmdroid.SettingsActivity$1.onReceive(SettingsActivity.java:148)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5147)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Both (class and activity) in same folder (org.osmdroid). Seems like paths is ok, but the problem appears and I have no more ideas...
You are starting a service as an activity
Change
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startActivity(mServiceIntent);
to
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startService(mServiceIntent); // Only this line is changed
Please declared this activity in your AndroidManifest.xml
UPDATE
Like an idiot I did not pay attention to the logCat.
The problem was coming from the page I was navigating to, not the intent itself, I was declaring some code before the setContentView code has a chance to run within onContentLoad().
Thanks for all the help!
I know this question has been asked before, but I have followed numerous posts about this topic; but can't seem to find the right answer.
I am trying to navigate from my MainActivity page, to my ViewFeeds page; but it is throwing the above error.
The funny thing is, I have already got the code to navigate between pages, and it works. But when I come to duplicate this code for another class, it throws the above error.
MainActivity Code (The one I am navigating from):
public void btnFeedsClick(View v)
{
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ViewFeeds.class);
MainActivity.this.startActivity(intent);
}
});
}
The ViewFeeds class (The one I am navigating to):
package com.example.rssapplication;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewFeeds extends Activity{
//The array which holds all of the data...
ArrayList<String> items = new ArrayList<String>();
ListView lv = (ListView) findViewById(R.id.listView1);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewfeeds);
ActionBar actionBar = getActionBar();
actionBar.hide();
items = new ArrayList<String>();
String ret = "";
try {
InputStream inputStream = openFileInput("Teams.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
// Toast.makeText(MainActivity.this, receiveString, Toast.LENGTH_LONG).show();
try {
while ( (receiveString = bufferedReader.readLine()) != null ) {
items.add(receiveString);
stringBuilder.append(receiveString + "\r\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inputStream.close();
//ret holds each line of text from the file...
ret = stringBuilder.toString();
TextView txt = (TextView) findViewById(R.id.textView6);
txt.setText(ret);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
items);
lv.setAdapter(arrayAdapter);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The android Manifest (I have put a comment above the section in which I am declaring the reference):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rssapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application
android:allowBackup="true"
android:icon="#drawable/sportsoccericon"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:screenOrientation="portrait"
>
<activity
android:name="com.example.rssapplication.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.rssapplication.SelectTeams" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- This is where I am creating the intent to navigate from main, to viewfeeds -->
<activity android:name="com.example.rssapplication.ViewFeeds" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.rssapplication.ShowArticle" android:label="#string/app_name"
android:parentActivityName="com.example.rssapplication.MyAdapter"
>
<!-- <meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.rssapplication.MyAdapter" />-->
<!-- <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> -->
</activity>
</application>
</manifest>
This is he logcat:
08-07 16:27:02.512: E/AndroidRuntime(2321): FATAL EXCEPTION: main
08-07 16:27:02.512: E/AndroidRuntime(2321): Process: com.example.rssapplication, PID: 2321
08-07 16:27:02.512: E/AndroidRuntime(2321): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.rssapplication/com.example.rssapplication.ViewFeeds}: java.lang.NullPointerException
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.os.Looper.loop(Looper.java:157)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.reflect.Method.invoke(Method.java:515)
08-07 16:27:02.512: E/AndroidRuntime(2321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-07 16:27:02.512: E/AndroidRuntime(2321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-07 16:27:02.512: E/AndroidRuntime(2321): at dalvik.system.NativeStart.main(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): Caused by: java.lang.NullPointerException
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.Activity.findViewById(Activity.java:1965)
08-07 16:27:02.512: E/AndroidRuntime(2321): at com.example.rssapplication.ViewFeeds.<init>(ViewFeeds.java:20)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.Class.newInstanceImpl(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.Class.newInstance(Class.java:1208)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
08-07 16:27:02.512: E/AndroidRuntime(2321): ... 11 more
Any help will be greatly appreciated!
Thanks,
Callum
The problem is this:
ListView lv = (ListView) findViewById(R.id.listView1);
You are calling findViewById() too early! You always have to be aware of the Activity Lifecycle! findViewById() can only be called after you set a layout with setContentView().
You should look for Views in onCreate() like this:
public class ViewFeeds extends Activity {
private ArrayList<String> items = new ArrayList<String>();
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewfeeds);
lv = (ListView) findViewById(R.id.listView1);
...
}
...
}
1st problem declare this inside onCreate(...) after setContentView(R.layout.ur_layout)
ListView lv = (ListView) findViewById(R.id.listView1);
2nd problem as your application's android:minSdkVersion="8" you should use
ActionBar actionBar = getSupportActionBar();
instead of Activity you have to extends ActionBarActivity of appcompat v7 support library
ActionbarActivity
How to import appcompat into your project
I get this error when I run my program. It starts up the first page, but when it's supposed to go to .Menu it crashes:
11-04 06:01:06.039: W/dalvikvm(949): threadid=11: thread exiting with uncaught exception (group=0x414c4700)
11-04 06:01:06.057: E/AndroidRuntime(949): FATAL EXCEPTION: Thread-87
11-04 06:01:06.057: E/AndroidRuntime(949): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.secondapp.MENU }
11-04 06:01:06.057: E/AndroidRuntime(949): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
11-04 06:01:06.057: E/AndroidRuntime(949): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
11-04 06:01:06.057: E/AndroidRuntime(949): at android.app.Activity.startActivityForResult(Activity.java:3390)
11-04 06:01:06.057: E/AndroidRuntime(949): at android.app.Activity.startActivityForResult(Activity.java:3351)
11-04 06:01:06.057: E/AndroidRuntime(949): at android.app.Activity.startActivity(Activity.java:3587)
11-04 06:01:06.057: E/AndroidRuntime(949): at android.app.Activity.startActivity(Activity.java:3555)
11-04 06:01:06.057: E/AndroidRuntime(949): at com.example.secondapp.Splash$1.run(Splash.java:26)
Here's my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.secondapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<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=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And here are the .java files of my .Splash activity and my .Menu activity:
Splash
package com.example.secondapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.happyman);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent startMain = new Intent("com.example.secondapp.MENU");
startActivity(startMain);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Menu
package com.example.secondapp;
public class Menu extends ListActivity {
String classes[] = {"MainActivity", "example1", "example2", "example3", "example4", "example5"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
String Cheese = classes[position];
super.onListItemClick(l, v, position, id);
try{
Class ourClass = Class.forName("com.example.secondapp." + Cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
in your manifest the action should match. Change it with com.example.secondapp.MENU
It is in caps MENU should be Menu
Change to
Intent startMain = new Intent("com.example.secondapp.Menu");
coz your activity is public class Menu extends ListActivity { and in manifest you have
<activity
android:name=".Menu"
Edit:
Explicit intents designate the target component by its name (the
component name field, mentioned earlier, has a value set). Since
component names would generally not be known to developers of other
applications, explicit intents are typically used for
application-internal messages — such as an activity starting a
subordinate service or launching a sister activity.
Implicit intents do not name a target (the field for the component
name is blank). Implicit intents are often used to activate
components in other applications.
So Change to explicit intent
Intent startMain = new Intent(Splash.this,Menu.class); // in Splash.java
and in manifest
<activity
android:name="com.example.secondapp.Menu"
android:label="#string/app_name" >
</activity>
For more info check the docs
http://developer.android.com/guide/components/intents-filters.html
You need to set activity class in your manifest and you misspelled MENU
replace
<activity
android:name=".Menu"
android:label="#string/app_name" >
with
<activity
android:name=".MENU"
android:label="#string/app_name" >
I created splash screen to my android project, if i run it splash screen appears for a while and displays force close message, what should i do to navigate to the next page? any suggestions?
public class LoadingScreen extends Activity implements LoadingTaskFinishedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the splash screen
setContentView(R.layout.activity_loading_screen);
// Find the progress bar
ProgressBar progressBar = (ProgressBar) findViewById(R.id.Progressbar);
// Start your loading
new LoadingTask(progressBar, null).execute("www.google.co.uk"); // Pass in whatever you need a url is just an example we don't use it in this tutorial
}
// This is the callback for when your async task has finished
public void onTaskFinished() {
completeSplash();
}
private void completeSplash(){
startApp();
finish(); // Don't forget to finish this Splash Activity so the user can't return to it!
}
private void startApp() {
Intent intent = new Intent(LoadingScreen.this, Rebuix.class);
startActivity(intent);
}
}
My manifest file
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/rebuix"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".LoadingScreen"
android:label="#string/title_activity_loading_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Rebuix"
android:label="#string/title_activity_rebuix" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.rebuix.com.Rebuix" />
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.rebuix.com.Rebuix" />
</activity>
Logcat
11-23 13:13:03.798: I/Tutorial(459): Starting task with url: www.google.co.uk
11-23 13:13:14.156: D/AndroidRuntime(459): Shutting down VM
11-23 13:13:14.156: W/dalvikvm(459): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-23 13:13:14.166: E/AndroidRuntime(459): FATAL EXCEPTION: main
11-23 13:13:14.166: E/AndroidRuntime(459): java.lang.NullPointerException
11-23 13:13:14.166: E/AndroidRuntime(459): at com.rebuix.com.LoadingTask.onPostExecute(LoadingTask.java:68)
11-23 13:13:14.166: E/AndroidRuntime(459): at com.rebuix.com.LoadingTask.onPostExecute(LoadingTask.java:1)
11-23 13:13:14.166: E/AndroidRuntime(459): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-23 13:13:14.166: E/AndroidRuntime(459): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-23 13:13:14.166: E/AndroidRuntime(459): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-23 13:13:14.166: E/AndroidRuntime(459): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 13:13:14.166: E/AndroidRuntime(459): at android.os.Looper.loop(Looper.java:123)
11-23 13:13:14.166: E/AndroidRuntime(459): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 13:13:14.166: E/AndroidRuntime(459): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 13:13:14.166: E/AndroidRuntime(459): at java.lang.reflect.Method.invoke(Method.java:521)
11-23 13:13:14.166: E/AndroidRuntime(459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 13:13:14.166: E/AndroidRuntime(459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 13:13:14.166: E/AndroidRuntime(459): at dalvik.system.NativeStart.main(Native Method)
LoadingTask class
public class LoadingTask extends AsyncTask<String, Integer, Integer> {
public interface LoadingTaskFinishedListener {
void onTaskFinished(); // If you want to pass something back to the listener add a param to this method
}
// This is the progress bar you want to update while the task is in progress
private final ProgressBar progressBar;
// This is the listener that will be told when this task is finished
private final LoadingTaskFinishedListener finishedListener;
/**
* A Loading task that will load some resources that are necessary for the app to start
* #param progressBar - the progress bar you want to update while the task is in progress
* #param finishedListener - the listener that will be told when this task is finished
*/
public LoadingTask(ProgressBar progressBar, LoadingTaskFinishedListener finishedListener) {
this.progressBar = progressBar;
this.finishedListener = finishedListener;
}
#Override
protected Integer doInBackground(String... params) {
Log.i("Tutorial", "Starting task with url: "+params[0]);
if(resourcesDontAlreadyExist()){
downloadResources();
}
// Perhaps you want to return something to your post execute
return 1234;
}
private boolean resourcesDontAlreadyExist() {
// Here you would query your app's internal state to see if this download had been performed before
// Perhaps once checked save this in a shared preference for speed of access next time
return true; // returning true so we show the splash every time
}
private void downloadResources() {
// We are just imitating some process thats takes a bit of time (loading of resources / downloading)
int count = 10;
for (int i = 0; i < count; i++) {
// Update the progress bar after every step
int progress = (int) ((i / (float) count) * 100);
publishProgress(progress);
// Do some long loading things
try { Thread.sleep(1000); } catch (InterruptedException ignore) {}
}
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]); // This is ran on the UI thread so it is ok to update our progress bar ( a UI view ) here
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
finishedListener.onTaskFinished(); // Tell whoever was listening we have finished
}
}
change
new LoadingTask(progressBar, null).execute("www.google.co.uk");
to
new LoadingTask(progressBar, this).execute("www.google.co.uk");
I think the second param should be a LoadingTaskFinishedListener.
I have viewed most of the other threads regarding this error but have not found an answer.
I started a new project a couple of weeks ago using a plugin and the example project with the plugin. Added various of my own features and designs and no problems running the project.
Then updated to ADT 17 2 days ago and this seriously messed things up for me. Started getting class path errors to name a few. I then reverted back to adt 16 which fixed the errors and my project compiles fine but as soon as i run it it crashes on the test device.
I have checked that my compliance level is correct, checked library paths, api versions, manifest xml, basically everything. I do not get how something that use to work perfectly can now just not work.
I proceeded to unistall everything and did a reinstall on the sdk's ADT and java, but to no avail, even just trying to run the example project just crashes.
I have aslo uninstalled the app from the device and rbooted the device and cleared the cache. I am at the end of my rope. Like i say, i have checked libraries and everything, its just this runtime error.
I also increased the connection time out, and added "android:installLocation="preferExternal" to my manifest, no change.
Please help, there cant be an issue with the code as it worked perfectly.
Please see the code for the starting activity:
package com.yourcompany.junaioplugin.template;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.yourcompany.junaioplugin.template.R;
import com.metaio.junaio.plugin.JunaioPlugin;
public class SplashActivity extends Activity
{
static
{
JunaioPlugin.loadNativeLibs();
}
/**
* standard tag used for all the debug messages
*/
public static final String TAG = "junaioPluginTemplate";
/**
* Display log messages with debug priority
*
* #param msg Message to display
* #see Log#d(String, String)
*/
public static void log(String msg)
{
if (msg != null)
Log.d(TAG, msg);
}
/**
* Progress dialog
*/
private ProgressDialog progressDialog;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.main );
JunaioStarterTask junaioStarter = new JunaioStarterTask();
junaioStarter.execute(1);
}
private class JunaioStarterTask extends AsyncTask<Integer, Integer, Integer>
{
#Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(SplashActivity.this, "junaio", "Starting up...");
}
#Override
protected Integer doInBackground(Integer... params)
{
// Set authentication if a private channel is used
// JunaioPlugin.setAuthentication("username", "password");
// Start junaio, this will initialize everything the plugin need
int result = JunaioPlugin.startJunaio(this, getApplicationContext());
return result;
}
#Override
protected void onProgressUpdate(Integer... progress)
{
}
#Override
protected void onPostExecute(Integer result)
{
if (progressDialog != null)
{
progressDialog.cancel();
progressDialog = null;
}
switch (result)
{
case JunaioPlugin.ERROR_EXSTORAGE:
SplashActivity.log("External storage is not available, closing...");
finish();
break;
case JunaioPlugin.ERROR_INSTORAGE:
SplashActivity.log("Internal storage is not available, closing...");
finish();
break;
case JunaioPlugin.CANCELLED:
SplashActivity.log("Starting junaio cancelled");
break;
case JunaioPlugin.SUCCESS:
JunaioPlugin.setAuthentication("junaioTester", "test123");
launchLiveView();
break;
}
}
}
/**
* Launch junaio live view
*/
private void launchLiveView()
{
startActivity(new Intent(this, JunaioARViewTestActivity.class));
finish();
}
#Override
protected void onResume()
{
super.onResume();
}
#Override
protected void onPause()
{
super.onPause();
}
#Override
protected void onStop()
{
super.onStop();
if (progressDialog != null)
{
progressDialog.cancel();
progressDialog = null;
}
}
}
Here is the logcat:
03-27 10:47:47.543: I/dalvikvm(10641): Could not find method com.metaio.junaio.plugin.JunaioPlugin.loadNativeLibs, referenced from method com.yourcompany.junaioplugin.template.SplashActivity.<clinit>
03-27 10:47:47.543: W/dalvikvm(10641): VFY: unable to resolve static method 65: Lcom/metaio/junaio/plugin/JunaioPlugin;.loadNativeLibs ()V
03-27 10:47:47.543: D/dalvikvm(10641): VFY: replacing opcode 0x71 at 0x0000
03-27 10:47:47.543: D/dalvikvm(10641): VFY: dead code 0x0003-0003 in Lcom/yourcompany/junaioplugin/template/SplashActivity;.<clinit> ()V
03-27 10:47:47.543: W/dalvikvm(10641): Unable to resolve superclass of Lcom/yourcompany/junaioplugin/template/JunaioARViewTestActivity; (48)
03-27 10:47:47.543: W/dalvikvm(10641): Link of class 'Lcom/yourcompany/junaioplugin/template/JunaioARViewTestActivity;' failed
03-27 10:47:47.547: E/dalvikvm(10641): Could not find class 'com.yourcompany.junaioplugin.template.JunaioARViewTestActivity', referenced from method com.yourcompany.junaioplugin.template.SplashActivity.launchLiveView
03-27 10:47:47.547: W/dalvikvm(10641): VFY: unable to resolve const-class 78 (Lcom/yourcompany/junaioplugin/template/JunaioARViewTestActivity;) in Lcom/yourcompany/junaioplugin/template/SplashActivity;
03-27 10:47:47.547: D/dalvikvm(10641): VFY: replacing opcode 0x1c at 0x0002
03-27 10:47:47.547: D/dalvikvm(10641): VFY: dead code 0x0004-000d in Lcom/yourcompany/junaioplugin/template/SplashActivity;.launchLiveView ()V
03-27 10:47:47.547: W/dalvikvm(10641): Exception Ljava/lang/NoClassDefFoundError; thrown during Lcom/yourcompany/junaioplugin/template/SplashActivity;.<clinit>
03-27 10:47:47.547: W/dalvikvm(10641): Class init failed in newInstance call (Lcom/yourcompany/junaioplugin/template/SplashActivity;)
03-27 10:47:47.547: D/AndroidRuntime(10641): Shutting down VM
03-27 10:47:47.547: W/dalvikvm(10641): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
03-27 10:47:47.555: E/AndroidRuntime(10641): FATAL EXCEPTION: main
03-27 10:47:47.555: E/AndroidRuntime(10641): java.lang.ExceptionInInitializerError
03-27 10:47:47.555: E/AndroidRuntime(10641): at java.lang.Class.newInstanceImpl(Native Method)
03-27 10:47:47.555: E/AndroidRuntime(10641): at java.lang.Class.newInstance(Class.java:1429)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.os.Looper.loop(Looper.java:123)
03-27 10:47:47.555: E/AndroidRuntime(10641): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-27 10:47:47.555: E/AndroidRuntime(10641): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:47:47.555: E/AndroidRuntime(10641): at java.lang.reflect.Method.invoke(Method.java:521)
03-27 10:47:47.555: E/AndroidRuntime(10641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-27 10:47:47.555: E/AndroidRuntime(10641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-27 10:47:47.555: E/AndroidRuntime(10641): at dalvik.system.NativeStart.main(Native Method)
03-27 10:47:47.555: E/AndroidRuntime(10641): Caused by: java.lang.NoClassDefFoundError: com.metaio.junaio.plugin.JunaioPlugin
03-27 10:47:47.555: E/AndroidRuntime(10641): at com.yourcompany.junaioplugin.template.SplashActivity.<clinit>(SplashActivity.java:19)
03-27 10:47:47.555: E/AndroidRuntime(10641): ... 15 more
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="3"
android:versionName="3.5.1" package="com.yourcompany.junaioplugin.template"
android:installLocation="preferExternal">
<!-- The application must be compiled using Google APIs (Android 3.0) -->
<!-- However, target and min SDK can be 8 (Android 2.2) -->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.location.gps" android:required="false"/>
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="false"/>
<uses-feature android:name="android.hardware.sensor.compass" android:required="false"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:label="#string/app_name"
android:icon="#drawable/icon"
android:debuggable="true">
<uses-library android:name="com.google.android.maps" />
<!-- Start screen -->
<activity android:name=".SplashActivity"
android:theme="#style/Theme.Fullscreen"
android:screenOrientation="portrait"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- junaio AR view activity -->
<activity
android:name=".JunaioARViewTestActivity"
android:theme="#style/Theme.Fullscreen"
android:configChanges="orientation"
android:screenOrientation="landscape">
</activity>
<activity
android:name="com.metaio.junaio.plugin.view.POIDetailDialog"
android:theme="#style/Theme.POIDialog"
android:screenOrientation="landscape">
</activity>
<activity
android:name="com.metaio.junaio.plugin.view.WebViewActivity"
android:theme="#style/Theme.Fullscreen"
android:configChanges="orientation">"
</activity>
<activity
android:name="com.metaio.junaio.plugin.view.ImageViewActivity"
android:theme="#style/Theme.Fullscreen"
android:configChanges="orientation">
</activity>
</application>
</manifest>
For me, when I installed ADT 17 I have problems using 3rd party libraries (It kept telling me there were duplications). It turns out that they no longer need to be added to the build path; just kept in a folder in the root of your project called "libs". Could this be the same problem?