Вопрос

The aim of this app is to have the EditText create a new TextView(s) in a list above it in a linear layout. Whenever the "Send" button is pressed, it just crashes the app.

Here is my code:

public class NotesFragment extends Fragment {

    LinearLayout llNotes;
    Button butNotes;
    EditText etNotes;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.notes, container, false);
        llNotes = (LinearLayout) rootView.findViewById(R.id.llNotes);

        return rootView;

    }

    public void sendText(View view)
    {
        etNotes = (EditText)llNotes.findViewById(R.id.etNotes);
        String message = etNotes.getText().toString();

        TextView text = new TextView(getActivity());
        text.setText(message);
        llNotes.addView(text);
    }
}

In the xml file the button has the attribute:

android:onClick="sendText"


EDIT: Here is the LOGCAT

05-11 15:23:35.713: D/ActivityThread(3750): setTargetHeapUtilization:0.75
05-11 15:23:35.713: D/ActivityThread(3750): setTargetHeapIdealFree:2097152
05-11 15:23:35.933: E/SensorManager(3750): thread start
05-11 15:23:35.933: D/SensorManager(3750): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fd..:8
05-11 15:23:35.953: D/SensorManager(3750): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fdc,f(
05-11 15:23:35.953: D/SensorManager(3750): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fd..:8
05-11 15:23:35.953: I/SensorManager(3750): Set min delay = 200000
05-11 15:23:35.953: I/SensorManager(3750): Before sending... delay = 200000
05-11 15:23:35.953: D/Sensors(3750): Remain listener = android.view.OrientationEventListener$SensorEventListenerImpl@41edc4e0
05-11 15:23:35.953: I/Sensors(3750): sendDelay --- 200000000
05-11 15:23:35.953: D/SensorManager(3750): JNI - sendDelay
05-11 15:23:35.993: D/libEGL(3750): loaded /system/lib/egl/libEGL_adreno200.so
05-11 15:23:36.013: D/libEGL(3750): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-11 15:23:36.013: D/libEGL(3750): loaded /system/lib/egl/libGLESv2_adreno200.so
05-11 15:23:36.083: I/Adreno200-EGLSUB(3750): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-11 15:23:36.103: E/(3750): <s3dReadConfigFile:75>: Can't open file for reading
05-11 15:23:36.103: E/(3750): <s3dReadConfigFile:75>: Can't open file for reading
05-11 15:23:36.103: D/OpenGLRenderer(3750): Enabling debug mode 0
05-11 15:23:36.233: D/SensorManager(3750): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fdc,f(
05-11 15:23:36.233: D/Sensors(3750): Remain listener = Sending .. normal delay 200ms
05-11 15:23:36.233: I/Sensors(3750): sendDelay --- 200000000
05-11 15:23:36.243: D/SensorManager(3750): JNI - sendDelay
05-11 15:23:36.243: I/SensorManager(3750): Set normal delay = true
05-11 15:23:36.243: D/SensorManager(3750): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fd..:8
05-11 15:23:36.293: W/InputMethodManager(3750): Ignoring onBind: cur seq=1819, given seq=1818
05-11 15:23:36.313: W/IInputConnectionWrapper(3750): getExtractedText on inactive InputConnection
05-11 15:23:36.333: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.333: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:36.484: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.484: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:36.514: W/IInputConnectionWrapper(3750): getExtractedText on inactive InputConnection
05-11 15:23:36.514: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.514: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:36.604: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.604: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:51.078: D/AndroidRuntime(3750): Shutting down VM
05-11 15:23:51.078: W/dalvikvm(3750): threadid=1: thread exiting with uncaught exception (group=0x4145c360)
05-11 15:23:51.088: E/AndroidRuntime(3750): FATAL EXCEPTION: main
05-11 15:23:51.088: E/AndroidRuntime(3750): java.lang.IllegalStateException: Could not find a method sendText(View) in the activity class net.tagyoureit.heykeepup.MainActivity for onClick handler on view class android.widget.Button with id 'butNotes'
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View$1.onClick(View.java:3677)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View.performClick(View.java:4192)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View$PerformClick.run(View.java:17254)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.os.Handler.handleCallback(Handler.java:615)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.os.Looper.loop(Looper.java:137)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.app.ActivityThread.main(ActivityThread.java:4950)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.reflect.Method.invoke(Method.java:511)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at dalvik.system.NativeStart.main(Native Method)
05-11 15:23:51.088: E/AndroidRuntime(3750): Caused by: java.lang.NoSuchMethodException: sendText [class android.view.View]
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.Class.getMethod(Class.java:915)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View$1.onClick(View.java:3670)
05-11 15:23:51.088: E/AndroidRuntime(3750):     ... 11 more

Also, this is inside the NotesFragment class. The MainActivity only has fragment information in it. This is inside the MainActivity class:

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Notes", "Money" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}
Это было полезно?

Решение

your problem is in xml file you set attributes OnClick="sendText" and defining method in fragment Actually you have to defining it in MainActivity not it Fragment

public void sendText(View view){
  // your code ....
}

Actually you this way do not Applied encapsulation object oriented properties – so best way to do it like this : you have to override onCreateView method in NotesFragment class to set onClickListner like this

private Button foo; // as globle var
private OnButtonsClicked boo; // as globle var

public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.boo = (OnButtonsClicked) activity;
}
public interface OnButtonsClicked {

    public void sendText(View v);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.notes,
        container, false);
    foo = view.findViewByid(R.id.foo); // button id
    foo.setOnClickListener(new OnClickListener() {
       public void onClick(View v)
       {
          boo.sendText(v);
       } 
    });
return view;
}

and in your Activity implement OnButtonsClicked interface like this

 public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, NotesFragment.OnButtonsClicked {
     // your code ....

   @override
   public void sendText(View v){
    // button click code here
   }
 }

Другие советы

The app will crash with illegalStateException because there is no sendText method defined in your MainActivity. The android:onClick holds true for Activity only but not for fragments.

You need to make use View.OnClickListener inside your fragments for handling onClick events.

How to handle button clicks using the xml onClick within Fragments may help you with possible solutions for the problem.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top