I am getting NullPointerException while starting a helloworld activity. I am starting this class from FileEvent.java class, which code I have put down here.
public class FileEvent extends Activity implements ObserverActivity{
public static String path2;
public String filename;
public String path;
public adapter info ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.info = new adapter(this);
}
public void insert(String path) {
// TODO Auto-generated method stub
// try{
this.info = new adapter(this);
this.filename = path;
System.out.println("Starting intent in fileevent");
try{
startActivity(new Intent(FileEvent.this,hello.class)); // In this line I am getting nullpointerexception was caught.
}
catch(Exception e)
{
Log.v("Caught in insert() of FileEvent : ",e.toString());
}
}
hello.class consists of a simple textview.
AndroidManifest.xml :-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample_fileobserver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sample_fileobserver.hello"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.hello" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.sample_fileobserver.FileEvent"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.FIleEvent" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
The logcat shows the following message -
09-28 05:51:55.307: I/System.out(13542): Starting intent in fileevent
09-28 05:51:55.307: A/FileObserver(13542): Unhandled exception in FileObserver com.example.sample_fileobserver.MyFileObserver@b11bec18
09-28 05:51:55.307: A/FileObserver(13542): java.lang.NullPointerException
09-28 05:51:55.307: A/FileObserver(13542): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
09-28 05:51:55.307: A/FileObserver(13542): at android.content.ComponentName.<init>(ComponentName.java:75)
09-28 05:51:55.307: A/FileObserver(13542): at android.content.Intent.<init>(Intent.java:3662)
09-28 05:51:55.307: A/FileObserver(13542): at com.example.sample_fileobserver.FileEvent.insert(FileEvent.java:42)
09-28 05:51:55.307: A/FileObserver(13542): at com.example.sample_fileobserver.MyFileObserver.onEvent(MyFileObserver.java:70)
09-28 05:51:55.307: A/FileObserver(13542): at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
09-28 05:51:55.307: A/FileObserver(13542): at android.os.FileObserver$ObserverThread.observe(Native Method)
09-28 05:51:55.307: A/FileObserver(13542): at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
Note :-
1) I have not declared setContentView() in FileEvent class, since it does not going to use UI.
2) Here onCreate() is not running, as I am calling insert() from another class of my application.
It might seem that, this question might be duplicate of many other questions, but I did not find the right solution from those questions.
Thanks in advance.
You are getting error because you are trying to call another activity with FileEvent
class context. I think it is not possible.
If you are calling insert()
method from another class, have to pass context of that class.
Try to pass Context of that class.
Your Method should be
public void insert(String path, Context context)
{
info = new adapter(context);
filename = path;
try
{
startActivity(new Intent(context,hello.class));
}
catch(Exception e)
{
Log.v("Caught in insert() of FileEvent : ",e.toString());
}
}
Hope it will help you.