这是我的第一个问题,所以请耐心等待。
所以我尝试完成Swipe View,直到我尝试在Fragment中创建了这样的功能。我通过调用Fragment函数成为onClick方法中的NPE。 所以这是我的代码
我的主要方法叫做PyramidActivity
import java.util.ArrayList;
import java.util.Locale;
import com.example.testsawa.R;
import com.example.testsawa.R.id;
import com.example.testsawa.R.layout;
import com.example.testsawa.R.menu;
import com.example.testsawa.R.string;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
public class PyramidActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
ActionBar actionBar;
PiramidFragmentA pirFraA;
PiramidFragmentB pirFraB;
PiramidFragmentC pirFraC;
ImageButton ibOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pyramid);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new MyPageChangeListener());
ibOne = (ImageButton) findViewById(R.id.imageButton1);
ImageButton i = (ImageButton) findViewById(R.id.imageButton2);
}
public void imageClick(View view) {
switch (view.getId()) {
case R.id.imageButton1:
pirFraA.setIngredient("Test");//this coses a NPE
break;
case R.id.imageButton2:
break;
case R.id.imageButton3:
break;
case R.id.imageButton4:
break;
case R.id.imageButton5:
break;
case R.id.imageButton6:
break;
case R.id.imageButton7:
break;
case R.id.imageButton8:
break;
case R.id.imageButton9:
break;
case R.id.imageButton10:
break;
case R.id.imageButton11:
break;
case R.id.imageButton12:
break;
default:
break;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
if (position == 0) {
return pirFraA = new PiramidFragmentA();
}
if (position == 1) {
return pirFraB = new PiramidFragmentB();
}
if (position == 2) {
return pirFraC = new PiramidFragmentC();
}
return null;
}
@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;
}
}
public class MyPageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
}
}
所以我的Fragment类就像这样
import java.util.ArrayList;
import com.example.testsawa.R;
import com.example.testsawa.R.layout;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class PiramidFragmentA extends Fragment {
private ArrayList<String> ingredientsList;
private ImageButton ibOne;
View view;
boolean isPressed = true;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_piramid_fragment_a, container,false);
ibOne =(ImageButton) view.findViewById(R.id.imageButton3);
return view;
}
public void setIngredient(String ing){
ingredientsList.add(ing);
}
public ArrayList<String> getIngredient(){
return ingredientsList;
}
}
我的Logcat看起来像这样
08-23 14:51:29.950: E/AndroidRuntime(1794): FATAL EXCEPTION: main
08-23 14:51:29.950: E/AndroidRuntime(1794): java.lang.IllegalStateException: Could not execute method of the activity
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.view.View$1.onClick(View.java:3633)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.view.View.performClick(View.java:4240)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.view.View$PerformClick.run(View.java:17721)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.os.Handler.handleCallback(Handler.java:730)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.os.Looper.loop(Looper.java:137)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 14:51:29.950: E/AndroidRuntime(1794): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 14:51:29.950: E/AndroidRuntime(1794): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 14:51:29.950: E/AndroidRuntime(1794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 14:51:29.950: E/AndroidRuntime(1794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 14:51:29.950: E/AndroidRuntime(1794): at dalvik.system.NativeStart.main(Native Method)
08-23 14:51:29.950: E/AndroidRuntime(1794): Caused by: java.lang.reflect.InvocationTargetException
08-23 14:51:29.950: E/AndroidRuntime(1794): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 14:51:29.950: E/AndroidRuntime(1794): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 14:51:29.950: E/AndroidRuntime(1794): at android.view.View$1.onClick(View.java:3628)
08-23 14:51:29.950: E/AndroidRuntime(1794): ... 11 more
08-23 14:51:29.950: E/AndroidRuntime(1794): Caused by: java.lang.NullPointerException
08-23 14:51:29.950: E/AndroidRuntime(1794): at de.piramid.testsawa.PiramidFragmentA.setIngredient(PiramidFragmentA.java:39)
08-23 14:51:29.950: E/AndroidRuntime(1794): at de.piramid.testsawa.PyramidActivity.imageClick(PyramidActivity.java:61)
08-23 14:51:29.950: E/AndroidRuntime(1794): ... 14 more
我在Fragment类中尝试过onCLik方法,即使它没有用。我希望我能很好地编辑我的问题,你可以帮助我。所以在那之前。
答案 0 :(得分:1)
你的ingredientsList
变量为null,因此你得到了这个NullPointerException。
08-23 14:51:29.950: E/AndroidRuntime(1794): Caused by: java.lang.NullPointerException 08-23 14:51:29.950: E/AndroidRuntime(1794): at de.piramid.testsawa.PiramidFragmentA.setIngredient(PiramidFragmentA.java:39)
你应该初始化这个变量(这只是一个例子):
public class PiramidFragmentA extends Fragment {
private ArrayList<String> ingredientsList;
/** ... */
public void setIngredient(String ing){
if(ingredientsList == null) {
ingredientsList = new ArrayList<String>();
}
ingredientsList.add(ing);
}
}