Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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
如何在android中获取用户名_Android - Fatal编程技术网

如何在android中获取用户名

如何在android中获取用户名,android,Android,在下面的代码中,大家好。如何获取imservice.getusername()。但是,它给出了以下错误。我想要我正在编写此代码的用户名。imservice.getusername()这一个与此相同,我正在访问它工作的用户名。但是在这个类中它不工作 谁能帮我一下我哪里做错了 **友人名单** public class FriendList extends ListActivity { private static final int ADD_NEW_FRIEND_ID = Menu.FI

在下面的代码中,大家好。如何获取imservice.getusername()。但是,它给出了以下错误。我想要我正在编写此代码的用户名。imservice.getusername()这一个与此相同,我正在访问它工作的用户名。但是在这个类中它不工作

谁能帮我一下我哪里做错了

**友人名单**

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.text1.setText(friends[position].groupName);
            holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);

            return convertView;
        }

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



    }
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
**群组列表**

public class GroupList extends ListActivity 
{

    boolean[] checkBoxState;
    boolean isChecked;
    String check;
    ListView users;
    int position;
    private IAppManager imService = null;
    ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();
    private FriendListAdapter friendAdapter;

    public String ownusername = new String();

    private class FriendListAdapter extends BaseAdapter 
    {   
        @SuppressWarnings("unused")

        class ViewHolder {
            TextView text;
            ImageView icon;
            CheckBox check1;



        }

        private LayoutInflater mInflater;
        private Bitmap mOnlineIcon;
        private Bitmap mOfflineIcon;        

        private FriendInfo[] friend = 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.friend = friends;

        }


        public int getCount() {     

            return friend.length;
        }


        public FriendInfo getItem(int position) {           

            return friend[position];
        }

        public long getItemId(int position) {

            return 0;
        }

        @SuppressWarnings("unused")
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;


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


                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);

                convertView.setTag(holder);

            }           

            else {

                holder = (ViewHolder) convertView.getTag();

            }


            holder.text.setText(friend[position].userName);
            holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);

            final ArrayList<String> checkedFriends = new ArrayList<String>();
            checkBoxState = new boolean[friend.length];
            holder.check1.setChecked(checkBoxState[position]);
            holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkBoxState[position]=isChecked;

                    if(isChecked){

                       check=friend[position].userName;

                    } 

                }
            });


            return convertView;
        }
protected void onCreate(Bundle savedInstanceState){       
        super.onCreate(savedInstanceState);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        setContentView(R.layout.group_list_screen);

        Button create=(Button)findViewById(R.id.create);


        friendAdapter = new FriendListAdapter(this); 


        friendAdapter.getCheckedItems();
        create.setOnClickListener(new OnClickListener() {

            @SuppressWarnings({ "unused", "unchecked" })
            @Override
            public void onClick(View v) {
                String groupname = getIntent().getStringExtra("nick");

                            try {



                                String result1 = imService.CreateGroup(groupname,imService.getUsername(),friendAdapter.getCheckedItems());
                            } catch (UnsupportedEncodingException e) {

                                e.printStackTrace();
                            }



                Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();

            }
        });    

    }
公共类组列表扩展了ListActivity
{
布尔[]checkBoxState;
布尔值被检查;
字符串检查;
ListView用户;
内部位置;
私有IAppManager imService=null;
ArrayList结果=新建ArrayList();
私有FriendListAdapter FriendListAdapter;
公共字符串ownusername=新字符串();
私有类FriendListAdapter扩展了BaseAdapter
{   
@抑制警告(“未使用”)
类视图持有者{
文本查看文本;
图像视图图标;
复选框1;
}
私人停车场;
私有位图mOnlineIcon;
私人电视台;
private FriendInfo[]friend=null;
公共FriendListAdapter(上下文){
超级();
mInflater=LayoutInflater.from(上下文);
mOnlineIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.greenstar);
mOfflineIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.redstar);
}
public void setFriendList(FriendInfo[]朋友)
{
这个。朋友=朋友;
}
public int getCount(){
返回friend.length;
}
public FriendInfo getItem(内部位置){
归还朋友[职位];
}
公共长getItemId(int位置){
返回0;
}
@抑制警告(“未使用”)
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
最终持票人;
if(convertView==null)
{
convertView=mInflater.flate(R.layout.grouplist,null);
holder=新的ViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.text);
holder.icon=(ImageView)convertView.findViewById(R.id.icon);
holder.check1=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(支架);
}           
否则{
holder=(ViewHolder)convertView.getTag();
}
holder.text.setText(友人[position].userName);
holder.icon.setImageBitmap(friend[position].status==status.ONLINE?mOnlineIcon:mOfflineIcon);
final ArrayList checkedFriends=新ArrayList();
checkBoxState=新布尔值[friend.length];
holder.check1.setChecked(checkBoxState[位置]);
holder.check1.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
检查更改后的公共无效(复合按钮视图,布尔值已检查){
checkBoxState[位置]=已检查;
如果(已检查){
check=朋友[职位]。用户名;
} 
}
});
返回视图;
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
}
setContentView(R.layout.group\u list\u屏幕);
按钮创建=(按钮)findViewById(R.id.create);
friendAdapter=新的FriendListAdapter(此);
getCheckedItems();
create.setOnClickListener(新的OnClickListener(){
@SuppressWarnings({“未使用”、“未选中”})
@凌驾
公共void onClick(视图v){
字符串groupname=getIntent().getStringExtra(“尼克”);
试一试{
字符串result1=imService.CreateGroup(groupname,imService.getUsername(),friendAdapter.getCheckedItems());
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
Toast.makeText(getApplicationContext(),“成功创建组”,Toast.LENGTH_LONG.show();
}
});    
}

它看起来不像是实例化了
imService
,所以每次调用
imService
上的getter方法时,都会得到一个空指针异常
imService.getUsername()的声明是什么
line以前工作过?公共字符串getUsername(){在正常工作之前返回this.username;}@ρцσѕρєK