简体   繁体   English

单击按钮时应用崩溃(使用xml android:onClick和Fragment类)

[英]App crashes on button click, (using xml android:onClick and Fragment classes)

The aim of this app is to have the EditText create a new TextView(s) in a list above it in a linear layout. 该应用程序的目的是让EditText在其上方的列表中以线性布局创建一个新的TextView。 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: 在xml文件中,按钮具有以下属性:

android:onClick="sendText" android:onClick =“ sendText”


EDIT: Here is the LOGCAT 编辑:这是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. 同样,这在NotesFragment类内部。 The MainActivity only has fragment information in it. MainActivity中仅包含片段信息。 This is inside the MainActivity class: 这是在MainActivity类内部:

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 您的问题是在xml文件中,您设置了属性OnClick =“ sendText”并在片段中定义了方法实际上,您必须在MainActivity中对其进行定义,而不是片段

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 实际上,您这种方式并不应用面向对象的封装属性-这样做的最佳方法是:您必须重写NotesFragment类中的onCreateView方法以像这样设置onClickListner

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 并在您的Activity中实现像这样的OnButtonsClicked接口

 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. 由于MainActivity中未定义sendText方法,因此该应用程序将崩溃并带有invalidStateException。 The android:onClick holds true for Activity only but not for fragments. android:onClick仅对Activity有效,但对片段无效。

You need to make use View.OnClickListener inside your fragments for handling onClick events. 您需要在片段内部使用View.OnClickListener来处理onClick事件。

How to handle button clicks using the xml onClick within Fragments may help you with possible solutions for the problem. 如何使用Fragments中的xml onClick处理按钮单击,可能会帮助您解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM