类文件中的空指针异常

时间:2015-02-07 12:13:13

标签: android

嗨,在下面的代码我得到空指针异常。在这里我显示所有的朋友列表。但它没有显示朋友列表它给出空指针异常。 任何人都可以帮助我解决这个问题。 ** FriendList **

public class FriendList extends ListActivity 
{
    private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
    private static final int CREATE_GROUP_ID = Menu.FIRST+1;
    private static final int EXIT_APP_ID = Menu.FIRST + 2;

    private IAppManager imService = null;
    private FriendListAdapter friendAdapter;

    public String ownusername = new String();

    private class FriendListAdapter extends BaseAdapter 
    {       
        class ViewHolder {
            TextView text,text1;

            ImageView icon;
        }
        private LayoutInflater mInflater;
        private Bitmap mOnlineIcon;
        private Bitmap mOfflineIcon;        

        private FriendInfo[] friends = null;


        public FriendListAdapter(Context context) {
            super();            

            mInflater = LayoutInflater.from(context);

            mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
            mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

        }

        public void setFriendList(FriendInfo[] friends)
        {
            this.friends = friends;
            }


        public int getCount() {     

            return friends.length;
        }


        public FriendInfo getItem(int position) {           

            return friends[position];
        }

        public long getItemId(int position) {

            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;


            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.friend_list_screen, null);


                holder = new ViewHolder();
                holder.text1=(TextView)convertView.findViewById(R.id.text1);
                holder.text = (TextView) convertView.findViewById(R.id.text);

                holder.icon = (ImageView) convertView.findViewById(R.id.icon);                                       

                convertView.setTag(holder);
            }   
            else {

                holder = (ViewHolder) convertView.getTag();
            }


            holder.text.setText(friends[position].userName);

            holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);

            return convertView;
        }

    }

    public class MessageReceiver extends  BroadcastReceiver  {

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("Broadcast receiver ", "received a message");
            Bundle extra = intent.getExtras();
            if (extra != null)
            {
                String action = intent.getAction();
                if (action.equals(IMService.FRIEND_LIST_UPDATED))
                {

                    FriendList.this.updateData(FriendController.getFriendsInfo(), 
                                                FriendController.getUnapprovedFriendsInfo());

                }
            }
        }

    };
    public MessageReceiver messageReceiver = new MessageReceiver();

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {          
            imService = ((IMService.IMBinder)service).getService();      

            FriendInfo[] friends = FriendController.getFriendsInfo(); 
            if (friends != null) {              
                FriendList.this.updateData(friends, null); 
            }    

            setTitle(imService.getUsername() + "'s friend list");
            ownusername = imService.getUsername();

        }
        public void onServiceDisconnected(ComponentName className) {          
            imService = null;
            Toast.makeText(FriendList.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };



    protected void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_screen);

        friendAdapter = new FriendListAdapter(this);
        try {
            String result1 = imService.DispalyGroupDetails(imService.getUsername());
            System.out.println(result1);
        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();
        }



    }
    public void updateData(FriendInfo[] friends, FriendInfo[] unApprovedFriends)
    {
        if (friends != null) {
            friendAdapter.setFriendList(friends);   
            setListAdapter(friendAdapter);              
        }               

        if (unApprovedFriends != null) 
        {
            NotificationManager NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            if (unApprovedFriends.length > 0)
            {                   
                String tmp = new String();
                for (int j = 0; j < unApprovedFriends.length; j++) {
                    tmp = tmp.concat(unApprovedFriends[j].userName).concat(",");            
                }
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.stat_sample)
                .setContentTitle(getText(R.string.new_friend_request_exist));
                /*Notification notification = new Notification(R.drawable.stat_sample, 
                        getText(R.string.new_friend_request_exist),
                        System.currentTimeMillis());*/

                Intent i = new Intent(this, UnApprovedFriendList.class);
                i.putExtra(FriendInfo.FRIEND_LIST, tmp);                

                PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                        i, 0);

                mBuilder.setContentText("You have new friend request(s)");


                mBuilder.setContentIntent(contentIntent);


                NM.notify(R.string.new_friend_request_exist, mBuilder.build());         
            }
            else
            {

                NM.cancel(R.string.new_friend_request_exist);           
            }
        }

    }


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

        super.onListItemClick(l, v, position, id);      

        Intent i = new Intent(this, Messaging.class);
        FriendInfo friend = friendAdapter.getItem(position);
        i.putExtra(FriendInfo.USERNAME, friend.userName);

        i.putExtra(FriendInfo.PORT, friend.port);
        i.putExtra(FriendInfo.IP, friend.ip);       
        startActivity(i);
    }




    @Override
    protected void onPause() 
    {
        unregisterReceiver(messageReceiver);        
        unbindService(mConnection);
        super.onPause();
    }

    @Override
    protected void onResume() 
    {

        super.onResume();
        bindService(new Intent(FriendList.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);

        IntentFilter i = new IntentFilter();

        i.addAction(IMService.FRIEND_LIST_UPDATED);

        registerReceiver(messageReceiver, i);               
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        boolean result = super.onCreateOptionsMenu(menu);       

        menu.add(0, ADD_NEW_FRIEND_ID, 0, R.string.add_new_friend);
        menu.add(0, CREATE_GROUP_ID, 0, R.string.create_group);
        menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);     

        return result;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
    {       

        switch(item.getItemId()) 
        {     
            case ADD_NEW_FRIEND_ID:
            {
                Intent i = new Intent(FriendList.this, AddFriend.class);
                startActivity(i);
                return true;
            }
            case CREATE_GROUP_ID:
            {
                Intent i = new Intent(FriendList.this, CreateGroup.class);
                startActivity(i);
                return true;
            }   
            case EXIT_APP_ID:
            {
                imService.exit();
                finish();
                return true;
            }           
        }

        return super.onMenuItemSelected(featureId, item);       
    }   

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);




    }
}

logcat的

02-07 05:17:54.526: E/AndroidRuntime(3081): FATAL EXCEPTION: main
02-07 05:17:54.526: E/AndroidRuntime(3081): Process: at.vcity.androidim, PID: 3081
02-07 05:17:54.526: E/AndroidRuntime(3081): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.vcity.androidim/at.vcity.androidim.FriendList}: java.lang.NullPointerException
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.os.Looper.loop(Looper.java:136)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at java.lang.reflect.Method.invoke(Method.java:515)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at dalvik.system.NativeStart.main(Native Method)
02-07 05:17:54.526: E/AndroidRuntime(3081): Caused by: java.lang.NullPointerException
02-07 05:17:54.526: E/AndroidRuntime(3081):     at at.vcity.androidim.FriendList.onCreate(FriendList.java:178)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.Activity.performCreate(Activity.java:5231)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-07 05:17:54.526: E/AndroidRuntime(3081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-07 05:17:54.526: E/AndroidRuntime(3081):     ... 11 more

0 个答案:

没有答案