2011-05-29 69 views
5

幫助!我真的很欣賞Stack Overflow,它是過去幾個月的貢獻者。我有很多問題,我在這裏找到答案......但這個我似乎無法找到任何地方...我是Java和Android的noob,我一直在想這個幾天。出於某種原因,我有一個名爲fileList的ListView對象,它返回null ...一切都很好編譯,但是當我嘗試使用fileList的時候我得到了一個N​​ullPointerException異常...我已經能夠將它隔離到它的聲明中:爲什麼(ListView)findViewById返回null?

ListView fileList = (ListView)findViewById(R.id.open_ListView); 

但我不能爲我的生活理解這條線有什麼問題!我在下面包含了很多代碼,理論上它應該有任何和所有可能與此錯誤有關的代碼。

請任何幫助,這將非常感謝!謝謝!


這是違規的代碼部分。它只是開關塊的OPEN_DIALOG部分,其他所有開關都能很好地顯示它們的新Dialog。我已加星出錯的行...

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    Dialog newDialog = new Dialog(Minervalia.this); 
    switch(id) 
    { 
    case DIALOG_FILE_NEW: 
     newDialog.setContentView(R.layout.new_file); 
     newDialog.setTitle("New File"); 
     newDialog.setCancelable(true); 

     Button okBtn = (Button) newDialog.findViewById(R.id.ok_btn); 
     Button cancelBtn = (Button) newDialog.findViewById(R.id.cancel_btn); 
     final EditText widthEt = (EditText) newDialog.findViewById(R.id.width_edit); 
     final EditText heightEt = (EditText) newDialog.findViewById(R.id.height_edit); 

     okBtn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       file_width = Integer.parseInt(widthEt.getText().toString()); 
       file_height = Integer.parseInt(heightEt.getText().toString()); 
       onCreate(null); 
       dismissDialog(DIALOG_FILE_NEW); 
      } 
     }); 

     cancelBtn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       dismissDialog(DIALOG_FILE_NEW); 
      } 
     }); 
     return newDialog; 
    case DIALOG_OPEN: 
     newDialog.setContentView(R.layout.open_file); 
     newDialog.setTitle("Open File"); 
     newDialog.setCancelable(true); 

// ********** This next line returns null! Why? 
     ListView fileList = (ListView)findViewById(R.id.open_ListView); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, loadFileList()); 
     fileList.setAdapter(adapter); 
     fileList.setTextFilterEnabled(true); 

     fileList.setOnItemClickListener(new OnItemClickListener() 
     { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       // When clicked, show a toast with the TextView text 
       Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     return newDialog; 
    case DIALOG_SAVE: 
     newDialog.setContentView(R.layout.save_file); 
     newDialog.setTitle("Save File"); 
     newDialog.setCancelable(true); 

//--==[[ Define the important TextViews for our Save Dialog ]]==--\\    
     TextView pathTxt = (TextView) newDialog.findViewById(R.id.save_path_info); 
     EditText fnTxt = (EditText) newDialog.findViewById(R.id.save_filename_edit); 

//--==[[ Define the Radio Buttons for our Save Dialog ]]==--\\   
     RadioButton JPEGoption = (RadioButton) newDialog.findViewById(R.id.save_JPEGoption); 
     RadioButton PNGoption = (RadioButton) newDialog.findViewById(R.id.save_PNGoption); 

     file_type = TYPE_JPEG; // Set this as the default option 

     JPEGoption.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       file_type = TYPE_JPEG; 
      } 
     }); 

     PNGoption.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       file_type = TYPE_PNG; 
      } 
     }); 

     Button save_okBtn = (Button) newDialog.findViewById(R.id.save_ok_btn); 
     Button save_cancelBtn = (Button) newDialog.findViewById(R.id.save_cancel_btn); 

     path = pathTxt.getText().toString(); 

     fnTxt.addTextChangedListener(new TextWatcher() 
     { 
      public void afterTextChanged(Editable s) 
      { 
      } 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) 
      { 
      } 
      public void onTextChanged(CharSequence s, int start, int before, int count) 
      { 
       filename = s.toString(); 
      } 
     }); 
     Toast.makeText(this, path, Toast.LENGTH_SHORT).show(); 
     Toast.makeText(this, filename, Toast.LENGTH_SHORT).show(); 
     save_okBtn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       try 
       { 
        String fullName = path + filename; 
        Bitmap.CompressFormat compForm = Bitmap.CompressFormat.JPEG; // make sure our format is initialized 
        if(file_type == TYPE_JPEG) 
        { 
         fullName = fullName + ".jpg"; 
         compForm = Bitmap.CompressFormat.JPEG; 
        } 
        if(file_type == TYPE_PNG) 
        { 
         fullName = fullName + ".png"; 
         compForm = Bitmap.CompressFormat.PNG; 
        } 
        File thisImage = new File(fullName); 
        FileOutputStream out = new FileOutputStream(thisImage); 
        mBitmap.compress(compForm, 90, out); 

        new SingleMediaScanner(mContext, thisImage); 
        out.flush(); 
        out.close(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 

       dismissDialog(DIALOG_SAVE); 
      } 
     }); 

     save_cancelBtn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       dismissDialog(DIALOG_SAVE); 
      } 
     }); 
     return newDialog; 
    } 
    return null; 
} 

private String[] loadFileList() 
{ 
    String[] mFileList = new String[0]; // generate empty array to avoid NullPointerException 
    try 
    { 
     filePath.canWrite(); 
    } 
    catch(SecurityException e) 
    { 
// Why isn't TAG recognized?... 
//   Log.e(TAG, "unable to write on the sd card " + e.toString()); 
    } 
    if(filePath.exists()) 
    { 
     FilenameFilter filter = new FilenameFilter() 
     { 
      public boolean accept(File dir, String filename) 
      { 
       File sel = new File(dir, filename); 
       return filename.contains(".jpg") || filename.contains(".png") || sel.isDirectory(); 
      } 
     }; 
     mFileList = filePath.list(filter); 
    } 
    else 
    { 
     mFileList = new String[0]; 
    } 
    return mFileList; 
} 

這是我open_file.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView 
     android:id="@+id/open_ListView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </ListView> 
    <LinearLayout 
     android:id="@+id/open_ButtonLinearLayout" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 
     <Button 
      android:id="@+id/open_ok_btn" 
      android:text="Open" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content"> 
     </Button>  
     <Button 
      android:id="@+id/open_cancel_btn" 
      android:text="Cancel" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content"> 
     </Button>  
    </LinearLayout> 
</LinearLayout> 

這是我list_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp" > 
</TextView> 

回答

8

試試這個,因爲它是在對話框的佈局裏面,而不是活動的:

ListView fileList = (ListView)newDialog.findViewById(R.id.open_ListView); 
+0

YAY!這工作!非常感謝! ...然後,當你發佈這個,並且我嘗試了它之後,我意識到我一直在爲其他所有事情做這些......並且從未注意到......呃!謝謝你,謝謝你,謝謝你! – 2011-05-30 00:51:19

4

試着做通過導航到:

'Project' - >'Clean ...'然後選擇您的項目。

+0

奇怪的是,這個爲我工作。 – mahdaeng 2011-08-28 03:16:16

+0

也爲我工作。謝謝毛刺! – conciliator 2011-10-18 19:12:01

2

你應該寫上你的code..before ListView fileList = (ListView)findViewById(R.id.open_ListView); ..sorry的頂部setcontenetview,我的英語....!