如何获得在Android的用户名 [英] How to get the username in android
问题描述
喜在下面的code如何获得imservice.getusername(),但是,它给下面error.I想要的用户名,我写这篇code.imservice.getusername()这一个同样是这样,我访问它在这个类中它不工作working.But的用户名。
任何一个可以帮助我在这里,我没有错。
**好友列表**
公共类好友列表扩展ListActivity
{
私有静态最终诠释ADD_NEW_FRIEND_ID = Menu.FIRST;
私有静态最终诠释CREATE_GROUP_ID = Menu.FIRST + 1;
私有静态最终诠释EXIT_APP_ID = Menu.FIRST + 2;
私人IAppManager imService = NULL;
私人FriendListAdapter friendAdapter;
公共字符串ownusername =新的String();
私有类FriendListAdapter扩展了BaseAdapter
{
类ViewHolder {
TextView的文本,文本1;
ImageView的图标;
}
私人LayoutInflater mInflater;
私人位图mOnlineIcon;
私人位图mOfflineIcon;
私人FriendInfo []的朋友= NULL;
公共FriendListAdapter(上下文的背景下){
超();
mInflater = LayoutInflater.from(上下文);
mOnlineIcon = BitmapFactory.de codeResource(context.getResources(),R.drawable.greenstar);
mOfflineIcon = BitmapFactory.de codeResource(context.getResources(),R.drawable.redstar);
}
公共无效setFriendList(FriendInfo []的朋友)
{
this.friends =朋友;
}
公众诠释getCount将(){
返回friends.length;
}
公共FriendInfo的getItem(INT位置){
回到朋友[位置]
}
众长getItemId(INT位置){
返回0;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL)
{
convertView = mInflater.inflate(R.layout.friend_list_screen,NULL);
持有人=新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(保持器);
}
其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.text.setText(朋友[位置] .userName);
holder.text1.setText(朋友[位置] .groupName);
holder.icon.setImageBitmap(?朋友[位置] .STATUS == STATUS.ONLINE mOnlineIcon:mOfflineIcon);
返回convertView;
}
}
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.list_screen);
friendAdapter =新FriendListAdapter(本);
尝试 {
字符串RESULT1 = imService.DispalyGroupDetails(imService.getUsername());
的System.out.println(RESULT1);
}赶上(UnsupportedEncodingException E){
e.printStackTrace();
}
}
的logcat
5月2号至7日:17:54.526:E / AndroidRuntime(3081):致命异常:主要
5月2日至7号:17:54.526:E / AndroidRuntime(3081):工艺:at.vcity.androidim,PID:3081
5月2日至7号:17:54.526:E / AndroidRuntime(3081):java.lang.RuntimeException的:无法启动的活动ComponentInfo {at.vcity.androidim / at.vcity.androidim.FriendList}:显示java.lang.NullPointerException
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.os.Handler.dispatchMessage(Handler.java:102)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.os.Looper.loop(Looper.java:136)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.ActivityThread.main(ActivityThread.java:5017)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在java.lang.reflect.Method.invokeNative(本机方法)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在java.lang.reflect.Method.invoke(Method.java:515)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在dalvik.system.NativeStart.main(本机方法)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):由:显示java.lang.NullPointerException
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在at.vcity.androidim.FriendList.onCreate(FriendList.java:178)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.Activity.performCreate(Activity.java:5231)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
5月2日至7号:17:54.526:E / AndroidRuntime(3081):11 ...更多
**群组列表**
公共类群组列表扩展ListActivity
{
布尔[] checkBoxState;
布尔器isChecked;
字符串检查;
ListView的用户;
INT位置;
私人IAppManager imService = NULL;
ArrayList的< FriendInfo>结果=新的ArrayList< FriendInfo>();
私人FriendListAdapter friendAdapter;
公共字符串ownusername =新的String();
私有类FriendListAdapter扩展了BaseAdapter
{
@燮pressWarnings(未使用)
类ViewHolder {
TextView的文字;
ImageView的图标;
复选框CHECK1;
}
私人LayoutInflater mInflater;
私人位图mOnlineIcon;
私人位图mOfflineIcon;
私人FriendInfo []的朋友= NULL;
公共FriendListAdapter(上下文的背景下){
超();
mInflater = LayoutInflater.from(上下文);
mOnlineIcon = BitmapFactory.de codeResource(context.getResources(),R.drawable.greenstar);
mOfflineIcon = BitmapFactory.de codeResource(context.getResources(),R.drawable.redstar);
}
公共无效setFriendList(FriendInfo []的朋友)
{
this.friend =朋友;
}
公众诠释getCount将(){
返回friend.length;
}
公共FriendInfo的getItem(INT位置){
回到朋友[位置]
}
众长getItemId(INT位置){
返回0;
}
@燮pressWarnings(未使用)
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
最后ViewHolder持有人;
如果(convertView == NULL)
{
convertView = mInflater.inflate(R.layout.grouplist,NULL);
持有人=新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(保持器);
}
其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.text.setText(好友[位置] .userName);
holder.icon.setImageBitmap(好友[位置] .STATUS == STATUS.ONLINE mOnlineIcon:mOfflineIcon);
最后的ArrayList<字符串> checkedFriends =新的ArrayList<字符串>();
checkBoxState =新的布尔[friend.length]
holder.check1.setChecked(checkBoxState [位置]);
holder.check1.setOnCheckedChangeListener(新OnCheckedChangeListener(){
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
checkBoxState [位置] =器isChecked;
如果(器isChecked){
检查=朋友[位置] .userName;
}
}
});
返回convertView;
}
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
如果(android.os.Build.VERSION.SDK_INT> 9){
StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()
.permitAll()建立()。
StrictMode.setThreadPolicy(政策);
}
的setContentView(R.layout.group_list_screen);
按钮创建=(按钮)findViewById(R.id.create);
friendAdapter =新FriendListAdapter(本);
friendAdapter.getCheckedItems();
create.setOnClickListener(新OnClickListener(){
@燮pressWarnings({未使用,未})
@覆盖
公共无效的onClick(视图v){
串组名= getIntent()getStringExtra(缺口)。
尝试 {
字符串RESULT1 = imService.CreateGroup(组名,imService.getUsername(),friendAdapter.getCheckedItems());
}赶上(UnsupportedEncodingException E){
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),集团成功地创造,Toast.LENGTH_LONG).show();
}
});
}
它看起来并不像你实例 imService
,所以你会得到一个空指针除了每次调用一个getter方法时间 imService
。
Hi in the below code how to get the imservice.getusername().But, it's giving below error.I want the username for that I am writing this code.imservice.getusername() this one same like this I am accessing the username it working.But in this class it's not working.
Can any one help me where i did mistake.
**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.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
**GroupList **
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();
}
});
}
It doesn't look like you instantiate imService
, so you're going to get a null pointer exception every time you call a getter method on imService
.
这篇关于如何获得在Android的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!