我一直在尝试调试这个东西几个小时,我真的看不到这个问题。
这是我的MainActivity。这里要寻找的主要内容是ArrayList<Servico>
,即Servico
自定义对象。我创建了一个简单的“事件”类 - ServicoActual
- 只有Servico
对象引用(和构造函数/ getter):
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import net.pedromoreira.billper.events.ServicoActual;
import java.util.ArrayList;
import java.util.Locale;
import de.greenrobot.event.EventBus;
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ArrayList<Servico> servicos;
//private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
servicos = new ArrayList<Servico>();
servicos.add(new Servico("Luz"));
servicos.add(new Servico("Água"));
servicos.add(new Servico("Gás"));
//Log.i("onCreate", "Servicos: " + servicos.size());
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
//mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<Servico>(this,
R.layout.drawer_list_item, servicos));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
Log.i("DrawerItemClickListener", "Position: " + position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new ServicoFragment();
//Bundle args = new Bundle();
//args.putInt(ServicoFragment.ARG_PLANET_NUMBER, position);
//fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
Log.i("selectItem", "Servico: " + servicos.get(position).getNome());
EventBus.getDefault().postSticky(new ServicoActual(servicos.get(position)));
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(servicos.get(position).getNome());
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame"
*/
public static class ServicoFragment extends Fragment {
//public static final String ARG_PLANET_NUMBER = "planet_number";
private TextView mTestText;
public ServicoFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().registerSticky(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_servico, container, false);
//int i = getArguments().getInt(ARG_PLANET_NUMBER);
//String planet = getResources().getStringArray(R.array.planets_array)[i];
//int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
// "drawable", getActivity().getPackageName());
//((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
//getActivity().setTitle(planet);
mTestText = ((TextView) rootView.findViewById(R.id.test_text));
Log.i("onCreateView", "mTestText: " + mTestText.toString());
mTestText.setText("xpto");
return rootView;
}
public void onEvent(ServicoActual e){
Servico servico = e.getServico();
Log.i("onEvent", "Servico: " + servico.getNome());
getActivity().setTitle(servico.getNome());
mTestText.setText(servico.getNome());
}
}
}
因此,当点击抽屉列表项时,我正在尝试将相应的Servico
(在ServicoActual
“事件内”)传递给ServicoFragment
,Servico
应写入TextView
的{{1}}名称。
只需单击第一个项目(0)即可发生这种情况:
07-13 00:50:35.388 26244-26244/net.pedromoreira.billper I/ViewRootImpl﹕ ViewRoot's Touch Event : Touch Down
07-13 00:50:35.628 26244-26244/net.pedromoreira.billper I/ViewRootImpl﹕ ViewRoot's Touch Event : Touch UP
07-13 00:50:36.308 26244-26244/net.pedromoreira.billper I/ViewRootImpl﹕ ViewRoot's Touch Event : Touch Down
07-13 00:50:36.358 26244-26244/net.pedromoreira.billper I/ViewRootImpl﹕ ViewRoot's Touch Event : Touch UP
07-13 00:50:36.428 26244-26244/net.pedromoreira.billper I/selectItem﹕ Servico: Luz
07-13 00:50:36.428 26244-26244/net.pedromoreira.billper I/onEvent﹕ Servico: Luz
07-13 00:50:36.458 26244-26244/net.pedromoreira.billper E/Event﹕ Could not dispatch event: class net.pedromoreira.billper.events.ServicoActual to subscribing class class net.pedromoreira.billper.MainActivity$ServicoFragment
java.lang.NullPointerException
at net.pedromoreira.billper.MainActivity$ServicoFragment.onEvent(MainActivity.java:235)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:569)
at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:500)
at de.greenrobot.event.EventBus.postSingleEvent(EventBus.java:475)
at de.greenrobot.event.EventBus.post(EventBus.java:365)
at de.greenrobot.event.EventBus.postSticky(EventBus.java:406)
at net.pedromoreira.billper.MainActivity.selectItem(MainActivity.java:161)
at net.pedromoreira.billper.MainActivity.access$300(MainActivity.java:35)
at net.pedromoreira.billper.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:146)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2957)
at android.widget.AbsListView$3.run(AbsListView.java:3849)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
07-13 00:50:36.458 26244-26244/net.pedromoreira.billper I/onEvent﹕ Servico: Luz
07-13 00:50:36.468 26244-26244/net.pedromoreira.billper D/Event﹕ No subscribers registered for event class de.greenrobot.event.SubscriberExceptionEvent
07-13 00:50:36.468 26244-26244/net.pedromoreira.billper I/DrawerItemClickListener﹕ Position: 0
07-13 00:50:36.498 26244-26244/net.pedromoreira.billper I/onEvent﹕ Servico: Luz
07-13 00:50:36.508 26244-26244/net.pedromoreira.billper E/Event﹕ Could not dispatch event: class net.pedromoreira.billper.events.ServicoActual to subscribing class class net.pedromoreira.billper.MainActivity$ServicoFragment
java.lang.NullPointerException
at net.pedromoreira.billper.MainActivity$ServicoFragment.onEvent(MainActivity.java:236)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:569)
at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:500)
at de.greenrobot.event.EventBus.subscribe(EventBus.java:288)
at de.greenrobot.event.EventBus.register(EventBus.java:189)
at de.greenrobot.event.EventBus.registerSticky(EventBus.java:166)
at net.pedromoreira.billper.MainActivity$ServicoFragment.onCreate(MainActivity.java:209)
at android.app.Fragment.performCreate(Fragment.java:1688)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:860)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1450)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:444)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
07-13 00:50:36.508 26244-26244/net.pedromoreira.billper D/Event﹕ No subscribers registered for event class de.greenrobot.event.SubscriberExceptionEvent
07-13 00:50:36.508 26244-26244/net.pedromoreira.billper I/onCreateView﹕ mTestText: android.widget.TextView{429216d0 V.ED.... ......ID 0,0-0,0 #7f090003 app:id/test_text}
我可以做错什么?
答案 0 :(得分:7)
我错了。警告是关于在发布时没有人注册的。实际的异常可能与事件运行时未附加的Activity有关。您可能需要onAttach()
。
http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)
仍然感觉像是一种奇怪的架构,但是说我的应用程序都没有奇怪的架构是错误的。
<强>更新强>
让我重新开始。我的原始答案说 EventBus 崩溃,因为没有人为您的活动注册。但是,该日志声明并非崩溃。它只是一个调试。我发现 EventBus 对这种事情有点咄咄逼人,但总的来说它可能会有所帮助。
NullPointerException是不同的。当您的片段调用onCreate()
时,它会注册到您的粘性事件。这立即召唤您的活动。它就在堆栈中。此时,您的片段可能已附加到您的活动中,但onCreateView()
(可能)未被调用,因此您的TextView不存在。我无法确切地看到哪一行正在抛出异常。查看生命周期:
如果您想使用粘性事件,则需要稍后在Fragment生命周期中注册。我猜你想要onCreateView()
/ onDestroyView()
。我永远不会看到那种注册/取消注册配对,但它应该有用。
一般来说,我认为我们可以通过片段的参数或者直接从活动中获取数据来做到这一点,但所有方法都相当笨拙和/或冗长,这就是为什么我仍然是&#34;仅在需要时#34;那种有碎片的人。
另一种方法是在UI线程中创建一个Handler并发布一个执行注册的Runnable,应在挂起的生命周期事件之后安排,但如果你不这样做需要的是,不要这样做。
此外,您可能只是发布/注册Servico
而不是ServicoActual
,但这不会对您的问题产生任何影响。