1

我正在尝试使用 Intent 从 FileEvent.java 类启动 ArrAdapter.java。在 startActivity() 中,我得到空指针异常。FileEvent.java 扩展了一个活动,但没有 setContentView()。因为我没有在这个类的 UI 上显示任何东西。请看下面的代码——

FileEvent.java :-

    public class FileEvent extends Activity implements ObserverActivity{
    public static final String PREFS_NAME = "MyPreferencesFile";
    public static String path2;
    public String filename;
    public String path; 
    MyFileObserver myFileObserver; 
    public adapter info ;
    public ArrayAdapter<String> adapterarr;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
           this.info  = new adapter(this); 

        myFileObserver = new MyFileObserver("/storage/sdcard/DCIM/SAMPLE_IMAGES/");
            myFileObserver.registerObserver(this);
            myFileObserver.startWatching();
    Bundle bundle = getIntent().getExtras();
}

     protected void onPause(){
            myFileObserver.stopWatching();
            myFileObserver.unregisterObserver(this); 
          } 

          protected void onResume(){
            myFileObserver.registerObserver(this);
            myFileObserver.startWatching();
          }
    public void insert(String path) {
        // TODO Auto-generated method stub
//      try{

    this.info = new adapter(this);
    this.filename = path; 

    if(info == null)
    {
        System.out.println("info is null here.");
    }
    else
    {
        System.out.println("info is not null here.");
    //  setpath(path);
        System.out.println("sharedpreferences "+FileEvent.path2);

        startActivity(new Intent("android.intent.action.ArrAdapter")); //Here I am getting the NullPointerException.


       // i.putExtra("path", path);

        /*  }
        }

    private void setpath(String path) {
        // TODO Auto-generated method stub
    FileEvent.path2 = path;

}
    public void delete(String path) {
        Log.v("entered delete() in FileEvent", "entered delete() in FileEvent");

        // TODO Auto-generated method stub
this.info = new adapter(this);
try{
    if(this.info == null)
    {
        System.out.println("this.info is null here");
    }
    else
    {
        System.out.println("this.info is not null here");
          int id_of_deleted_person_photo = info.deletefile(path);
Log.v("_id of FileName to delete : ", ""+id_of_deleted_person_photo+"");
            }
        }
        catch(Exception e)
        {
            Log.v("Error in delete() in FileEvent.java : ",e.toString());
        }
        Log.v("completed delete() in FileEvent", "completed delete() in FileEvent");
    }
    @Override
    public void onFileObserved(int Event, String path) {
        // TODO Auto-generated method stub
        Log.v("path in onFileObserved",path);
    }

ArrAdapter.java :-

public class ArrAdapter extends ListActivity{
public String path;
ListView listView;
public adapter info;
public static final String PREFS_NAME = "MyPreferencesFile";
private ArrayAdapter<String> adapterarr;
private String filename;
public Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
setContentView(R.layout.fileeventlist);
info = new adapter(this);
listView = (ListView) findViewById(android.R.id.list);

    Log.v("value of FileEvent.path2 : ",FileEvent.path2);
        insert(FileEvent.path2);
        }
        private void insert(String var_from_prev_intent1) {
            // TODO Auto-generated method stub
            try{
                int rowcount = info.getrowcountofpersons(); 
                    Log.v("rowcount in new list onCreate: ", 

""+info.getrowcountofpersons()+"");
                String[] values = new String[rowcount];
            for(int i =1;i<=rowcount;i++)
            {
                values[i-1]=info.getPersonList(i); 
                System.out.println("in for loop now"+values[i-1]);
            } 
            this.adapterarr = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, values); 
            }
            catch(Exception e)
            {
                Log.v("Error at ArrayAdapter : ",e.toString());
            }
            if(adapterarr == null)
            {
                System.out.println("2. Here adapterarr is null.");
            }
            else
            {
                System.out.println("2. Here adapterarr is not null.");
            }



            listView.setItemsCanFocus(false);

            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


            // Assign adapter to List
            setListAdapter(adapterarr);  
            //  new Bullet(info).execute((Void)null);


        }


protected void onListItemClick(ListView l, View v, int position, long id) {

//  try{

        super.onListItemClick(l, v, position, id);
          // ListView Clicked item index
           int itemPosition     = position;
          // ListView Clicked item value
           String  itemValue    = (String) l.getItemAtPosition(position);
         //  content.setText("Click : \n  Position :"+itemPosition+"  \n  ListItem : " +itemValue);
           String personname = itemValue;
           try
           {
               System.out.println("paths in FileEvent : "+filename);
               info.insert(filename,personname);
           }
           catch(Exception e)
           {
               e.printStackTrace();
           }

    /*  }
        catch (Exception e)
        {
            Log.v("Error in insert() definition FileEvent.java : ",e.toString());
        }

*/   

} }

    }

我得到 logcat 如下 -

09-28 00:04:40.537: A/FileObserver(31484): Unhandled exception in FileObserver com.example.sample_fileobserver.MyFileObserver@b11cd6b8
09-28 00:04:40.537: A/FileObserver(31484): java.lang.NullPointerException
09-28 00:04:40.537: A/FileObserver(31484):  at android.app.Activity.startActivityForResult(Activity.java:3390)
09-28 00:04:40.537: A/FileObserver(31484):  at android.app.Activity.startActivityForResult(Activity.java:3351)
09-28 00:04:40.537: A/FileObserver(31484):  at android.app.Activity.startActivity(Activity.java:3587)
09-28 00:04:40.537: A/FileObserver(31484):  at android.app.Activity.startActivity(Activity.java:3555)
09-28 00:04:40.537: A/FileObserver(31484):  at com.example.sample_fileobserver.FileEvent.insert(FileEvent.java:95)
09-28 00:04:40.537: A/FileObserver(31484):  at com.example.sample_fileobserver.MyFileObserver.onEvent(MyFileObserver.java:59)
09-28 00:04:40.537: A/FileObserver(31484):  at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
09-28 00:04:40.537: A/FileObserver(31484):  at android.os.FileObserver$ObserverThread.observe(Native Method)
09-28 00:04:40.537: A/FileObserver(31484):  at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)

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.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> 
     <activity
        android:name="com.example.sample_fileobserver.ArrAdapter"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.ArrAdapter" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

        <service 
        android:enabled="true" 
        android:name="com.example.sample_fileobserver.FileModificationService">
    </service>
</application>

我已经在 Manifest 文件中注册了这两个类。如果没有,我可能会遇到 ClassNotFound Exception。所以,我无法理解,为什么要提出这个异常。

提前致谢。

4

3 回答 3

2

尝试使用以下代码开始您的第二个活动:

startActivity(new Intent(this, ArrAdapter.class));
于 2013-09-28T05:28:05.007 回答
0

您的代码有 2 个问题。至今。

第一次开始活动

将您的代码更改为@Nitin 所说的内容

startActivity(new Intent(this, ArrAdapter.class));

第二张SD卡权限

从您的代码中,

myFileObserver = new MyFileObserver("/storage/sdcard/DCIM/SAMPLE_IMAGES/");

我认为您正在从 sdcard 中检索数据。为此,您需要获得外部存储的权限。将以下代码添加到您的 manifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
于 2013-09-28T06:04:18.257 回答
0

使用环境类查找 SD 卡:

File sdCard = Environment.getExternalStorageDirectory();
myFileObserver = new MyFileObserver(sdCard.getAbsolutePath() +"/DCIM/SAMPLE_IMAGES/");
于 2013-09-28T06:28:50.793 回答