2016-01-11 6 views
-1

Я застрял в этой строке в течение 5 дней. Вы хотите помочь мне, почему это говорит об исключении Null Pointer, когда я устанавливаю для него адаптер.NullPointerException while setAdapter для listView

Это ListFileController

public class ListFileController extends ArrayAdapter<Files> { 
private Context context; 
private int layoutId; 
ArrayList<Files> array; 

public ListFileController(Context context, int layoutId, ArrayList<Files> array) { 
    super(context, layoutId, array); 

    this.context = context; 
    this.layoutId = layoutId; 
    this.array = array; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater; 
    View rowView = convertView; 
    ViewHolder holder = null; 
    if (rowView == null){ 
     holder = new ViewHolder(); 

     inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 

     rowView = inflater.inflate(R.layout.list_file_layout, parent, false); 
     holder.icon = (ImageView) rowView.findViewById(R.id.icon); 
     holder.fileName = (TextView) rowView.findViewById(R.id.fileName); 
     holder.size = (TextView) rowView.findViewById(R.id.fileSize); 

     rowView.setTag(holder); 
    } 
    else{ 
     holder = (ViewHolder) rowView.getTag(); 
    } 

    Files i = array.get(position); 
    if (array.get(position).isFolder() == 1){ 
     holder.icon.setImageResource(R.drawable.folder); 
    } 
    else if (array.get(position).isFolder() == 0){ 
     if (array.get(position).getFileType().contains("image") || array.get(position).getFileType().contains("picture")) 
      holder.icon.setImageResource(R.drawable.image); 
     else if (array.get(position).getFileType().contains("sound")|| array.get(position).getFileType().contains("music")) 
      holder.icon.setImageResource(R.drawable.audio); 
     else if (array.get(position).getFileType().contains("video")|| array.get(position).getFileType().contains("film")) 
      holder.icon.setImageResource(R.drawable.video); 
     else 
      holder.icon.setImageResource(R.drawable.file); 
    } 
    else 
     holder.icon.setImageResource(R.drawable.back); 

    holder.fileName.setText(array.get(position).getFileName()); 
    holder.size.setText(array.get(position).getFileName()); 

    return rowView; 
} 

public static class ViewHolder{ 
    ImageView icon; 
    TextView fileName; 
    TextView size; 
} 

}

Это мой MainActivity

public class MainActivity extends Activity implements View.OnClickListener { 

private DropboxAPI<AndroidAuthSession> dropbox; 
final static private String ROOT = "/"; 
private final static String APP_NAME = "dropbox_prefs"; 
final static private String APP_KEY = "x7bv6zets19iqt4"; 
final static private String APP_SECRET = "i95y5a09v0wlopu"; 
private static final int REQUEST_PATH = 1; 
private ArrayAdapter<Files> controller = null; 

private AlertDialog.Builder alter; 
private AlertDialog.Builder inputbuilder; 

private String deviceFilePath; 
private String deviceFilename; 

public ArrayList<Files> listOfFiles = new ArrayList<Files>(); 
public ArrayList<Files> fileFromDropbox = new ArrayList<Files>(); 
public ArrayList<String> listOfFolder = new ArrayList<String>(); 

private Files selected; 
private String currentPath = ROOT; 
private String previousPath = ""; 

public boolean isLoggedIn; 
public Button logInBut; 
public Button uploadBut; 
public Button listFilesBut; 
ViewStub listView; 
private ListView list; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    isLoggedIn = false; 

    // Creating button on the screen 
    logInBut = (Button) findViewById(R.id.logInBut); 
    logInBut.setOnClickListener(this); 

    uploadBut = (Button) findViewById(R.id.uploadBut); 
    uploadBut.setOnClickListener(this); 


    listView = (ViewStub) findViewById(R.id.list_stub); 
    listView.inflate(); 
    list = (ListView) findViewById(R.id.list_stub); 


    // Create an array to store item >> Init 1 item to make array not null 
    fileFromDropbox.add(new Files("1st", "test", 0)); 
    listOfFiles.add(new Files ("1st", "test", 0)); 

    controller = new ListFileController(MainActivity.this, android.R.layout.simple_list_item_1, 
      listOfFiles); 
    list.setAdapter(controller); 

    list.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, 
             int position, long id) { 
      // TODO Auto-generated method stub 
      selected = controller.getItem(position); 
      if (selected.isFolder() == 1) { 
       folderOptionDialog(); 
      } 
      return true; 
     } 

    }); 

    list.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      // TODO Auto-generated method stub 
      selected = controller.getItem(position); 
      if (selected.isFolder() == 1) { 
       currentPath = selected.getPath(); 
       updateList(selected.getPath()); 

      } else if (selected.isFolder() == 2) { 
       updateList(selected.getParentPath()); 

      } else { 
       currentPath = selected.getParentPath(); 
       fileOptions(); 

      } 
      controller.notifyDataSetChanged(); 
     } 
    }); 
    // Authentication dropbox API account 
    checkLogIn(false); 
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); 
    AndroidAuthSession session = new AndroidAuthSession(appKeys); 
    dropbox = new DropboxAPI<AndroidAuthSession>(session); 
    dropbox.getSession().startOAuth2Authentication(MainActivity.this); 


    ConnectivityManager connectivityChecker = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 

    //For 3G check 
    boolean is3G = connectivityChecker.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) 
      .isConnectedOrConnecting(); 
    //For WiFi Check 
    boolean isWifi = connectivityChecker.getNetworkInfo(ConnectivityManager.TYPE_WIFI) 
      .isConnectedOrConnecting(); 

    if (!is3G && !isWifi) 
     Toast.makeText(MainActivity.this, "Please connect to the internet", Toast.LENGTH_LONG).show(); 
    if (!isWifi) 
     Toast.makeText(MainActivity.this, "3G service is currently not available, please connect via WIFI", Toast.LENGTH_LONG).show(); 


} 


private void setAdapter() { 

     controller = new ListFileController(this,android.R.layout.simple_list_item_1, listOfFiles); 
     list.setAdapter(controller); 
     list.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       // TODO Auto-generated method stub 
       selected = controller.getItem(position); 

       if (selected.isFolder() == 1) { 
        currentPath = selected.getPath(); 
        updateList(selected.getPath()); 

       } else if (selected.isFolder() == 2) { 
        updateList(selected.getParentPath()); 

       } else { 
        currentPath = selected.getParentPath(); 
        fileOptions(); 

       } 
       controller.notifyDataSetChanged(); 
      } 
     }); 

     list.setOnItemLongClickListener(new OnItemLongClickListener(){ 

      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, 
              int position, long id) { 
       // TODO Auto-generated method stub 
       selected = controller.getItem(position); 
       if (selected.isFolder() == 1) { 
        folderOptionDialog(); 
       } 
       return true; 
      } 

     }); 
} 

Это мой файл XML, который содержит список.

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/logInBut" 
    android:layout_marginLeft = "15dp" 
    android:layout_marginRight="15dp" 
    android:layout_width="373dp" 
    android:layout_height="243dp" 
    android:textColor="#43b8e3" 
    android:text="LOG IN" 
    android:textSize="100dp" 
    android:layout_gravity="center_horizontal" 
    android:enabled="false" /> 

<Button 
    android:id="@+id/uploadBut" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="UPLOAD" 
    android:textColor="#43b8e3" 
    android:textSize="80dp" 
    android:layout_weight="0.01" 
    android:layout_gravity="center_horizontal" /> 


<ViewStub 
    android:id="@+id/list_stub" 
    android:inflatedId="@+id/showlayout" 
    android:layout="@layout/activity_list_file" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp"/> 


</LinearLayout> 
</merge> 

И последняя является расположение

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ListView android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 
+0

отправьте свой стек. –

ответ

3

Вот ваш ListView XML:

<ListView android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

А вот ваш Java, создавая ссылку на него:

list = (ListView) findViewById(R.id.list_stub); 

Ты используешь неправильный идентификатор ресурса э. Измените линию на:

list = (ListView) findViewById(android.R.id.list); 
+0

вы тоже используете неверный идентификатор. это 'android.R.id.list' – Blackbelt

+0

@Blackbelt хороший улов! Это было трудно увидеть быстро. – AdamMc331

+0

Большое спасибо. Вы просто спасете мою жизнь –