我想在我的Android应用程序中使用Volley
库和VolleySignleton
,但是当startActivity时,显示FC错误。
活动代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private DataAdapter dataAdapter;
private SlidingTabLayout mTab;
private ViewPager mPage;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mRecyclerView = (RecyclerView) findViewById(R.id.main_RecyclerView);
mRecyclerView.setHasFixedSize(true);
dataAdapter = new DataAdapter(getApplicationContext(), getData());
mRecyclerView.setAdapter(dataAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mPage = (ViewPager) findViewById(R.id.pager);
mPage.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
mTab = (SlidingTabLayout) findViewById(R.id.tabs);
mTab.setDistributeEvenly(true);
mTab.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
mTab.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mTab.setViewPager(mPage);
}
public static List<Information> getData() {
List<Information> data = new ArrayList<>();
int[] icons = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12,
R.drawable.image13, R.drawable.image14,};
String[] titles = {"Mohammad", "Nooshin", "Peyman", "Shaqayeq", "Nastaran", "Baran",
"Aria", "BABA", "MAMAN", "Farzad", "Maryam", "Negar", "Omid", "Vahid",};
for (int i = 0; i < titles.length && i < icons.length; i++) {
Information current = new Information();
current.title = titles[i];
current.iconId = icons[i];
///----- fill to for (1000)
/*current.title = titles[i%titles.length];
current.iconId = icons[i%icons.length];*/
data.add(current);
}
return data;
}
class MyPageAdapter extends FragmentPagerAdapter {
int icon[] = {R.drawable.ic_menu_bookmark, R.drawable.ic_menu_toolbar, R.drawable.ic_cellphone90};
String tabs[] = getResources().getStringArray(R.array.tabs);
public MyPageAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
Drawable drawable = getResources().getDrawable(icon[position]);
drawable.setBounds(0, 0, 80, 80);
ImageSpan imageSpan = new ImageSpan(drawable);
SpannableString spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
@Override
public int getCount() {
return 3;
}
}
public static class MyFragment extends Fragment {
TextView txt_view;
public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle saveInstanceState) {
View layout = inflater.inflate(R.layout.fragment_my, container, false);
txt_view = (TextView) layout.findViewById(R.id.fragment_text);
Bundle bundle = getArguments();
if (bundle != null) {
txt_view.setText("Fragment is : " + bundle.getInt("position"));
}
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
StringRequest request = new StringRequest(Request.Method.GET, "http://php.net/", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getActivity(), "Response" + response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
return layout;
}
}
}
VolleySignleton代码:
public class VolleySingleton {
private static VolleySingleton sInstance = null;
private RequestQueue mRequestQueue;
private VolleySingleton() {
mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
}
public static VolleySingleton getInstance() {
if (sInstance == null) {
sInstance = new VolleySingleton();
}
return sInstance;
}
public RequestQueue getRequestQueue(){
return mRequestQueue;
}
}
MyApplication类代码:
public class MyApplication extends Application {
private static MyApplication sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = null;
}
public static MyApplication getInstance() {
return sInstance;
}
public static Context getAppContext() {
return sInstance.getApplicationContext();
}
}
FC错误日志:
01-24 12:38:53.264 26787-26787/com.tellfa.slidenerdmaterialtutorial E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tellfa.slidenerdmaterialtutorial, PID: 26787
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.tellfa.slidenerdmaterialtutorial.MyApplication.getApplicationContext()' on a null object reference
at com.tellfa.slidenerdmaterialtutorial.MyApplication.getAppContext(MyApplication.java:26)
at com.tellfa.slidenerdmaterialtutorial.Network.VolleySingleton.<init>(VolleySingleton.java:16)
at com.tellfa.slidenerdmaterialtutorial.Network.VolleySingleton.getInstance(VolleySingleton.java:24)
at com.tellfa.slidenerdmaterialtutorial.MainActivity$MyFragment.onCreateView(MainActivity.java:157)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17496)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17496)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17496)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17496)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17496)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17496)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5466)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2636)
at android.view.View.measure(View.java:17496)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1193)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
我该如何解决它并使用它?我真的需要这个。 tnx all&lt; 3
答案 0 :(得分:1)
添加到Manifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:name="<package name>.MyApplication">
并改变:
public class MyApplication extends Application {
private static MyApplication sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = MyApplication.this;
}
public static MyApplication getInstance() {
return sInstance;
}
}
和
mRequestQueue = Volley.newRequestQueue(MyApplication.getInstance());