我无法弄清楚这个问题,我没有任何信息会发生什么!我从谷歌应用程序面板中收到此错误。请勿参考此内容。
这是google发布中的错误:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{es.project/es.project.activity.ChatActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at es.project.activity.ChatActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more
在我的模拟器中,此代码有效,但我认为有些人有错误。这是我的OnCreate:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
serverId = getIntent().getExtras().getInt("serverId");
server = Chatsi.getInstance().getServerById(serverId);
Settings settings = new Settings(this);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
setTitle("");
setContentView(R.layout.chat);
EditText input = (EditText) findViewById(R.id.input);
input.setOnKeyListener(inputKeyListener);
//INDICATOR
pager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ConversationPagerAdapter(this, server);
pager.setAdapter(pagerAdapter);
final float density = getResources().getDisplayMetrics().density;
indicator = (ConversationIndicator) findViewById(R.id.titleIndicator);
indicator.setServer(server);
indicator.setTypeface(Typeface.DEFAULT);
indicator.setViewPager(pager);
indicator.setFooterColor(0xFFFF8800);
indicator.setFooterLineHeight(2 * density);
indicator.setFooterIndicatorHeight(4 * density);
indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
indicator.setSelectedColor(0xFF000000);
indicator.setSelectedBold(true);
indicator.setBackgroundColor(0xFFF2F2F2);
indicator.setTextColor(0xFF222222);
//Botones
Button boton1 = (Button) this.findViewById(R.id.enviar);
boton1.setOnClickListener(mandartexto);
Button boton2 = (Button) this.findViewById(R.id.emoticonos);
boton2.setOnClickListener(mandaremoticon);
Button boton3 = (Button) this.findViewById(R.id.lupa);
boton3.setOnClickListener(mandarlupa);
//MENUS
menuview = (ListView) findViewById(R.id.menuview);
MenuAdapter menuAdapter = new MenuAdapter(this, BOTONES);
menuview.setAdapter(menuAdapter);
historySize = settings.getHistorySize();
if (server.getStatus() == Status.PRE_CONNECTING) {
server.clearConversations();
pagerAdapter.clearConversations();
server.getConversation(ServerInfo.DEFAULT_NAME).setHistorySize(historySize);
}
float fontSize = settings.getFontSize();
indicator.setTextSize(fontSize * density);
input.setTextSize(settings.getFontSize());
input.setTypeface(Typeface.DEFAULT);
// Optimization : cache field lookups
Collection<Conversation> mConversations = server.getConversations();
for (Conversation conversation : mConversations) {
// Only scroll to new conversation if it was selected before
if (conversation.getStatus() == Conversation.STATUS_SELECTED) {
onNewConversation(conversation.getName());
} else {
createNewConversation(conversation.getName());
}
}
scrollback = new Scrollback();
this.overridePendingTransition(R.anim.appear,R.anim.disappear);
}
答案 0 :(得分:0)
server = Chatsi.getInstance().getServerById(serverId);
然后
if (server.getStatus() == Status.PRE_CONNECTING) {
形成一个潜在的很多例子的地方,其中一个无法获得对象的微不足道的事情(由于暂时信号强度暂时不良而导致)有可能导致未被捕获的空指针异常。
每当您从一个可能失败的机制获得一个对象时,您应该在使用它之前检查该对象是否为非空,或者保护您尝试通过try / catch使用它块或更高级别的处理程序,可以从异常中恢复。
问题不一定与您的服务器对象有关 - 它可能是您的视图或其他任何通过可能失败的机制获取,或在代码路径中使用的尚未初始化。