此应用程序的目的是让EditText以线性布局在其上方的列表中创建新的TextView。每当"发送"按下按钮,它只是崩溃的应用程序。
这是我的代码:
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);
}
}
在xml文件中,该按钮具有以下属性:
机器人:的onClick =" sendText"
编辑:这是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
此外,这是在NotesFragment类中。 MainActivity中只包含片段信息。这是在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) {
}
}
答案 0 :(得分:3)
您的问题是在xml文件中设置属性OnClick =“sendText”并在片段中定义方法实际上您必须在MainActivity中定义它而不是片段
public void sendText(View view){
// your code ....
}
其实你这种方式不应用封装面向对象的属性 - 这样做的最好方法是: 你必须覆盖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;
}
并在您的Activity中实现OnButtonsClicked界面,如此
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener, NotesFragment.OnButtonsClicked {
// your code ....
@override
public void sendText(View v){
// button click code here
}
}
答案 1 :(得分:1)
应用程序将因illegalStateException而崩溃,因为您的MainActivity中没有定义sendText
方法。 android:onClick
仅适用于Activity,但不适用于片段。
您需要在片段中使用View.OnClickListener来处理onClick事件。
How to handle button clicks using the xml onClick within Fragments可以帮助您找到问题的可能解决方案。