-1

helloworld アクティビティの開始中に NullPointerException が発生します。このクラスは、FileEvent.java クラスから開始します。このコードはここに記載されています。

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 は単純なテキストビューで構成されています。

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>

logcat には次のメッセージが表示されます -

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)

ノート :-

1) UI を使用しないため、FileEvent クラスで setContentView() を宣言していません。

2) アプリケーションの別のクラスから insert() を呼び出しているため、ここで onCreate() は実行されていません。

この質問は他の多くの質問と重複しているように見えるかもしれませんが、それらの質問から適切な解決策を見つけることができませんでした.

前もって感謝します。

4

2 に答える 2

1

FileEventクラス コンテキストで別のアクティビティを呼び出そうとしているため、エラーが発生しています。ありえないと思います。

insert()別のクラスからメソッドを呼び出す場合は、そのクラスのコンテキストを渡す必要があります。

そのクラスの Context を渡してみてください。

あなたの方法は

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());
     } 
}

それがあなたを助けることを願っています。

于 2013-09-28T11:00:28.373 に答える