Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中启动活动时出现NullPointerException_Java_Android_Xml_Fileobserver - Fatal编程技术网

Java 在android中启动活动时出现NullPointerException

Java 在android中启动活动时出现NullPointerException,java,android,xml,fileobserver,Java,Android,Xml,Fileobserver,我在启动helloworld活动时收到NullPointerException。我从FileEvent.java类开始这个类,我在这里写下了这些代码 public class FileEvent extends Activity implements ObserverActivity{ public static String path2; public String filename; public String path; public adapter info ; @Overr

我在启动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());
            }

    }
类由一个简单的文本视图组成

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)
09-28 05:51:55.307:I/System.out(13542):文件事件中的启动意图
09-28 05:51:55.307:A/FileObserver(13542):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):位于android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
09-28 05:51:55.307:A/FileObserver(13542):位于android.content.ComponentName.(ComponentName.java:75)
09-28 05:51:55.307:A/FileObserver(13542):在android.content.Intent.(Intent.java:3662)
09-28 05:51:55.307:A/FileObserver(13542):在com.example.sample_FileObserver.FileEvent.insert(FileEvent.java:42)
09-28 05:51:55.307:A/FileObserver(13542):在com.example.sample_FileObserver.MyFileObserver.onEvent(MyFileObserver.java:70)
09-28 05:51:55.307:A/FileObserver(13542):在android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
09-28 05:51:55.307:A/FileObserver(13542):在android.os.FileObserver$obserThread.observe(本机方法)
09-28 05:51:55.307:A/FileObserver(13542):在android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
注:-

1) 我没有在FileEvent类中声明setContentView(),因为它不会使用UI

2) 这里onCreate()没有运行,因为我正在从应用程序的另一个类调用insert()

看起来,这个问题可能与许多其他问题重复,但我没有从这些问题中找到正确的解决方案

提前感谢。

更改您的舱单:

<activity
  android:name="com.example.sample_fileobserver.FileEvent"
  android:label="@string/app_name" > 
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

您遇到错误,因为您试图使用
FileEvent
类上下文调用另一个活动。我认为这是不可能的

如果从另一个类调用
insert()
方法,则必须传递该类的上下文

尝试传递该类的上下文

你的方法应该是

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

希望它能对您有所帮助。

请发布整个堆栈跟踪。如果您不想拥有/设置UI,为什么要使用活动?您的清单中有一个输入错误-
FIleEvent
@fasteque,如果我没有使用它,我就无法正确编写意图代码?我已经发布了完整的日志。谢谢。事实上,我还有另一个活动作为
LAUNCHER
。在那个活动中,我调用了这个
insert()
方法。感谢您的回答@Andrain,但我之前尝试过在insert()方法中打印一些日志消息。他们工作了。因此,我认为从另一个类调用insert()可能没有问题。我的应用程序在FileEvent方法中的startActivity()处挂起。