请教大家一下,我想在onMenuOpened()方法里通过获取一个Item从而修改它的title 但总是抛出NullPointerException

Konvin_Zhi 2014-02-14 05:39:43
如题,我是一个新入Android阵型的小白,多谢各位大大了

menu=>main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_prefs"
android:title="@string/menu_prefs"
android:icon="@android:drawable/ic_menu_preferences"></item>
<item
android:id="@+id/server8"
android:title="开始"></item>
<item
android:id="@+id/OpenShowView"
android:title="显示更新"/>
</menu>


MainActivity.java

package com.example.countinput;

import android.os.Binder;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;

public class MainActivity extends Activity implements TextWatcher,OnSharedPreferenceChangeListener {

private SharedPreferences prefs;
private TextView text_count;
private EditText input;
private Count_Application ca;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater Inflater = this.getMenuInflater();
Inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.menu_prefs:
this.startActivity(new Intent(this,PrefsActivity.class));
break;
case R.id.server8:
if(ca.GetServer()){
this.startService(new Intent(this,Update_Server.class));
}else{
this.stopService(new Intent(this,Update_Server.class));
}
break;
case R.id.OpenShowView:
this.startActivity(new Intent(this,ShowActivity.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT));
break;
}
return true;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
menu.findItem(R.id.server8);
return super.onMenuOpened(featureId, menu);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_count = (TextView)this.findViewById(R.id.counts);
input = (EditText)this.findViewById(R.id.eidts);
input.addTextChangedListener(this);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
ca = (Count_Application)this.getApplication();
//DBHelper dbs = new DBHelper(this);
//SQLiteDatabase db = dbs.getReadableDatabase();
//input.setText(prefs.getString("UserName", ""));
}
;
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

int text_lenght = 140 -s.length();
//this.text_count.setText(String.valueOf(text_lenght));
this.text_count.setTextColor(Color.GREEN);
if(text_lenght<10){
this.text_count.setTextColor(Color.YELLOW);
}
if(text_lenght<0){
this.text_count.setTextColor(Color.RED);
}

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
//input.setText(prefs.getString("UserName", ""));

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}

@Override
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
// TODO Auto-generated method stub
//input.setText(prefs.getString("UserName", ""));
Count_Application apps = (Count_Application)this.getApplication();
input.setText(apps.getCount());
}

}
...全文
158 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
依然绿茶 2014-02-16
  • 打赏
  • 举报
回复
不要在onMenuOpened方法里面修改title,就在onCreateOptionsMenu里面修改。所以这里可以不用重写onMenuOpened方法

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧