按照标题,尝试在getDrawablesList()
中调用setupUI()
和ImageSectionFragment()
时出现此错误。getDrawablesList()
和setupUI()
在名为onCreate()
的单独项目中进行了测试,它们工作得很好。
我怀疑这与我在FragmentActivity
中如何称呼它们有关。
任何帮助将不胜感激。
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public String SERVERIP;
private TCPClient mTcpClient;
private ArrayList<String> arrayList;
private ImageView selectedImageView;
private ImageView leftArrowImageView;
private ImageView rightArrowImageView;
private Gallery gallery;
private int selectedImagePosition = 0;
private List<Drawable> drawables;
private GalleryImageAdapter galImageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
arrayList = new ArrayList<String>();
Bundle extras = getIntent().getExtras();
SERVERIP = extras.getString("key");
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
if (SERVERIP == "null") {
Log.d("MainActivity.java", "Continue without connection selected");
}
else {
new connectTask().execute("");
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
/*
* Fragment fragment = new DummySectionFragment(); Bundle args = new
* Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
* position + 1); fragment.setArguments(args); return fragment;
*/
Fragment fragment = new Fragment();
if (position == 0) {
fragment = new DummySectionFragment();
}
else if (position == 1) {
fragment = new ImageSectionFragment();
}
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_textpage,
container, false);
return rootView;
}
}
public static class ImageSectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
MainActivity ma = new MainActivity();
public ImageSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_imagepage,container, false);
ma.getDrawablesList();
ma.setupUI();
return rootView;
}
}
private void setupUI() {
selectedImageView = (ImageView) findViewById(R.id.selected_imageview);
leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
gallery = (Gallery) findViewById(R.id.gallery);
leftArrowImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (selectedImagePosition > 0) {
--selectedImagePosition;
}
gallery.setSelection(selectedImagePosition, false);
}
});
rightArrowImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (selectedImagePosition < drawables.size() - 1) {
++selectedImagePosition;
}
gallery.setSelection(selectedImagePosition, false);
}
});
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selectedImagePosition = pos;
if (selectedImagePosition > 0 && selectedImagePosition < drawables.size() - 1) {
leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
} else if (selectedImagePosition == 0) {
leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
}
changeBorderForSelectedImage(selectedImagePosition);
setSelectedImage(selectedImagePosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
galImageAdapter = new GalleryImageAdapter(this, drawables);
gallery.setAdapter(galImageAdapter);
if (drawables.size() > 0) {
gallery.setSelection(selectedImagePosition, false);
}
if (drawables.size() == 1) {
rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
}
}
private void changeBorderForSelectedImage(int selectedItemPos) {
int count = gallery.getChildCount();
for (int i = 0; i < count; i++) {
ImageView imageView = (ImageView) gallery.getChildAt(i);
imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_border));
imageView.setPadding(3, 3, 3, 3);
}
ImageView imageView = (ImageView) gallery.getSelectedView();
imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.selected_image_border));
imageView.setPadding(3, 3, 3, 3);
}
private void getDrawablesList() {
drawables = new ArrayList<Drawable>();
drawables.add(getResources().getDrawable(R.drawable.natureimage1));
drawables.add(getResources().getDrawable(R.drawable.natureimage2));
drawables.add(getResources().getDrawable(R.drawable.natureimage3));
drawables.add(getResources().getDrawable(R.drawable.natureimage4));
drawables.add(getResources().getDrawable(R.drawable.natureimage5));
drawables.add(getResources().getDrawable(R.drawable.natureimage6));
drawables.add(getResources().getDrawable(R.drawable.natureimage7));
drawables.add(getResources().getDrawable(R.drawable.natureimage8));
drawables.add(getResources().getDrawable(R.drawable.natureimage9));
drawables.add(getResources().getDrawable(R.drawable.natureimage10));
drawables.add(getResources().getDrawable(R.drawable.natureimage11));
drawables.add(getResources().getDrawable(R.drawable.natureimage12));
drawables.add(getResources().getDrawable(R.drawable.natureimage13));
drawables.add(getResources().getDrawable(R.drawable.natureimage14));
drawables.add(getResources().getDrawable(R.drawable.natureimage15));
}
private void setSelectedImage(int selectedImagePosition) {
BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
selectedImageView.setImageBitmap(b);
selectedImageView.setScaleType(ScaleType.FIT_XY);
}
public class connectTask extends AsyncTask<String, String, TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
// we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
// here the messageReceived method is implemented
public void messageReceived(String message) {
// this method calls the onProgressUpdate
publishProgress(message);
}
});
mTcpClient.run(SERVERIP);
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// in the arrayList we add the messaged received from server
arrayList.add(values[0]);
// notify the adapter that the data set has changed. This means that
// new message received
// from server was added to the list
}
}
}
这是logcat
07-16 14:21:48.664: E/AndroidRuntime(26188): FATAL EXCEPTION: main
07-16 14:21:48.664: E/AndroidRuntime(26188): java.lang.NullPointerException
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
07-16 14:21:48.664: E/AndroidRuntime(26188): at com.fypj.mdm.MainActivity.getDrawablesList(MainActivity.java:273)
07-16 14:21:48.664: E/AndroidRuntime(26188): at com.fypj.mdm.MainActivity.access$0(MainActivity.java:270)
07-16 14:21:48.664: E/AndroidRuntime(26188): at com.fypj.mdm.MainActivity$ImageSectionFragment.onCreateView(MainActivity.java:160)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.View.measure(View.java:15481)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.View.measure(View.java:15481)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.View.measure(View.java:15481)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-16 14:21:48.664: E/AndroidRuntime(26188): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2361)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.View.measure(View.java:15481)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1999)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1238)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1413)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4611)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.os.Handler.handleCallback(Handler.java:615)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.os.Looper.loop(Looper.java:137)
07-16 14:21:48.664: E/AndroidRuntime(26188): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-16 14:21:48.664: E/AndroidRuntime(26188): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 14:21:48.664: E/AndroidRuntime(26188): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 14:21:48.664: E/AndroidRuntime(26188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-16 14:21:48.664: E/AndroidRuntime(26188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-16 14:21:48.664: E/AndroidRuntime(26188): at dalvik.system.NativeStart.main(Native Method)
最佳答案
您不能像这样实例化一个活动
MainActivity ma = new MainActivity();
您需要做的是在片段中覆盖onAttach(Activity)并将活动分配给ma
Activity ma;
public void onAttach (Activity activity){
ma = activity;
}
或使用
getActivity()
方法在需要时获取Activity