我从
学习http://developer.android.com/reference/android/widget/Button.html
"而不是将OnClickListener应用于活动中的按钮,您可以使用android:onClick属性为XML布局中的按钮指定方法。"
但是,我只能使用XML方法完成它,我的监听器方法由以下指定,导致应用程序崩溃。
在我的fragment_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapp.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/try_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "TryButton"
android:onClick="onClick"
/>
</RelativeLayout>
在我的activity_main.xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapp.MainActivity"
tools:ignore="MergeRootFrame">
</FrameLayout>
在我的MainActivity.java中:
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button button = (Button)findViewById(R.id.try_button);
button.setOnClickListener(this);
}
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "I'm clicked!", Toast.LENGTH_SHORT).show();;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
LogCat说:
05-20 19:56:17.604: E/MoreInfoHPW_ViewGroup(566): Parent view is not a TextView
05-20 19:56:17.649: D/AndroidRuntime(566): Shutting down VM
05-20 19:56:17.649: W/dalvikvm(566): threadid=1: thread exiting with uncaught exception (group=0x4194ec08)
05-20 19:56:17.654: E/AndroidRuntime(566): FATAL EXCEPTION: main
05-20 19:56:17.654: E/AndroidRuntime(566): Process: com.example.myapp, PID: 566
05-20 19:56:17.654: E/AndroidRuntime(566): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: java.lang.NullPointerException
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.ActivityThread.access$900(ActivityThread.java:169)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.os.Handler.dispatchMessage(Handler.java:102)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.os.Looper.loop(Looper.java:136)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.ActivityThread.main(ActivityThread.java:5476)
05-20 19:56:17.654: E/AndroidRuntime(566): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 19:56:17.654: E/AndroidRuntime(566): at java.lang.reflect.Method.invoke(Method.java:515)
05-20 19:56:17.654: E/AndroidRuntime(566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
05-20 19:56:17.654: E/AndroidRuntime(566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
05-20 19:56:17.654: E/AndroidRuntime(566): at dalvik.system.NativeStart.main(Native Method)
05-20 19:56:17.654: E/AndroidRuntime(566): Caused by: java.lang.NullPointerException
05-20 19:56:17.654: E/AndroidRuntime(566): at com.example.myapp.MainActivity.onCreate(MainActivity.java:29)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.Activity.performCreate(Activity.java:5473)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
05-20 19:56:17.654: E/AndroidRuntime(566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
05-20 19:56:17.654: E/AndroidRuntime(566): ... 11 more
我在Windows 7上使用最新的Eclipse ADT v22.6.2,在Samsung Galaxy Note 10.1 2014上运行。
我的代码出了什么问题?
答案 0 :(得分:6)
试试这个:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
//Button button = (Button)findViewById(R.id.try_button);
//button.setOnClickListener(this);
}
/**
public void onClick(View v) {
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button button = (Button) rootView.findViewById(R.id.try_button);
button.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "I'm clicked!", Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:0)
您不需要实现OnClickListener,因为您的按钮会自动执行该方法。
编辑:
public class MainActivity extends Activity {
private OnClickListener onClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
initListener();//Initializing Listener.
Button button = (Button)findViewById(R.id.try_button);
//This command causes problem
button.setOnClickListener(onClickListener);
}
private void initListener(){
onclickListener = new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),
"Button Clicked",Toast.LENGTH_SHORT).show();
}
};
}
退出XML中的Onclick。
<Button
android:id="@+id/try_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "TryButton"
/>
答案 2 :(得分:0)
XML:android:onClick="onclickid"
活动:
public void onclickid(View view)
{
//Do something When the button is click
}
这应该有帮助,但我建议使用onclicklistener
答案 3 :(得分:0)
我已经解释了在博客上实现OnClickListener的不同方法: http://www.androiddevresources.com/guides/tutorial-how-to-implement-an-onclicklistener/
基本上有三种方式。 - 在activity类中实现监听器(并设置xml属性) - 在活动类(或其他类,例如片段)中创建变量。 - 根据新的OnClickListener()创建一个新的侦听器。
我在文章中添加了示例代码,并在GitHub上添加了一个示例项目,您可以下载。
答案 4 :(得分:0)
public class start extends Activity实现View.OnClickListener { 按钮bSMS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
bSMS = (Button) findViewById(R.id.button);
bSMS.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button: //"Block SMSs" button pressed
startActivity(new Intent(this,secret_main.class));
break;
}
}
}