添加Onclick监听器到android

时间:2014-04-22 06:52:36

标签: android

我使用的是Adt 22.0版。

我在onCreate方法内的按钮中添加了一个oclick监听器。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter=0;
    add=(Button)findViewById(R.id.bAdd);
    sub=(Button)findViewById(R.id.bSub);
    display=(TextView)findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }



}

当我像上面的应用程序崩溃一样添加onclick监听器时。

这是我的日志Cat错误。

04-22 01:08:36.900: I/Choreographer(836): Skipped 33 frames!  The application may be doing too much work on its main thread.
04-22 01:08:37.460: D/gralloc_goldfish(836): Emulator without GPU emulation detected.
04-22 02:27:35.010: D/AndroidRuntime(1183): Shutting down VM
04-22 02:27:35.010: W/dalvikvm(1183): threadid=1: thread exiting with uncaught exception (group=0xb2a9fba8)
04-22 02:27:35.020: E/AndroidRuntime(1183): FATAL EXCEPTION: main
04-22 02:27:35.020: E/AndroidRuntime(1183): Process: com.example.androidtest, PID: 1183
04-22 02:27:35.020: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtest/com.example.androidtest.MainActivity}: java.lang.NullPointerException
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.os.Looper.loop(Looper.java:136)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invoke(Method.java:515)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at dalvik.system.NativeStart.main(Native Method)
04-22 02:27:35.020: E/AndroidRuntime(1183): Caused by: java.lang.NullPointerException
04-22 02:27:35.020: E/AndroidRuntime(1183):     at com.example.androidtest.MainActivity.onCreate(MainActivity.java:36)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.Activity.performCreate(Activity.java:5231)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-22 02:27:35.020: E/AndroidRuntime(1183):     ... 11 more

我无法在oncreate方法中添加onclick侦听器 请帮助解决这个问题

LogCat for tushar narang的回答

04-22 02:27:35.020: E/AndroidRuntime(1183): FATAL EXCEPTION: main
04-22 02:27:35.020: E/AndroidRuntime(1183): Process: com.example.androidtest, PID: 1183
04-22 02:27:35.020: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtest/com.example.androidtest.MainActivity}: java.lang.NullPointerException
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.os.Looper.loop(Looper.java:136)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invoke(Method.java:515)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:595)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at dalvik.system.NativeStart.main(Native Method)
04-22 02:27:35.020: E/AndroidRuntime(1183): Caused by: java.lang.NullPointerException
04-22 02:27:35.020: E/AndroidRuntime(1183):     at com.example.androidtest.MainActivity.onCreate(MainActivity.java:36)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.Activity.performCreate(Activity.java:5231)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-22 02:27:35.020: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-22 02:27:35.020: E/AndroidRuntime(1183):     ... 11 more

这是主要的节日文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidtest" android:versionCode="1" android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.androidtest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

LayoutFile

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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.androidtest.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Your Total is 0"
    android:textSize="30dp"
    android:layout_gravity="center"
    android:gravity="center" />

<Button
    android:id="@+id/bAdd" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add one"/>
<Button 
    android:id="@+id/bSub" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Substract one"/>

</LinearLayout>

5 个答案:

答案 0 :(得分:1)

请检查此项,确保编辑按钮名称或textview

public static class PlaceholderFragment extends Fragment {

    private TextView txtSensor ;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
        txtSensor = (TextView)rootView.findViewById(R.id.txtSensor);
        txtSensor.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});
        return rootView;
    }

如果这不起作用,请发布您的代码

答案 1 :(得分:0)

protected class MainActivity extends Activity implements OnClickListener { 

Button add,sub;
 TextView display;
       protected void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter=0;
        add=(Button)findViewById(R.id.bAdd);
        sub=(Button)findViewById(R.id.bSub);
        display=(TextView)findViewById(R.id.tvDisplay);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);


        }


      public void onClick(View v) 

          {

                if (v == add)

                      {
Toast.makeText(getBaseContext(), "add pressed", Toast.LENGTH_LONG).show();

                       }

                           if (v == sub)

                         {


Toast.makeText(getBaseContext(), "sub pressed", Toast.LENGTH_LONG).show();
                          }
                      }




    }  

答案 2 :(得分:0)

您确定activity_main中的按钮ID是否正确?

add=(Button)findViewById(R.id.bAdd);
sub=(Button)findViewById(R.id.bSub);

您是否可以在代码中添加断点并检查是否添加&#39;正在填充正确的视图?

答案 3 :(得分:0)

我猜按钮&amp; textviews位于你的fragment_main.xml&amp;您是通过activity_main.xml引用它们的 尝试在占位符片段

中的OncreateView方法中引用它们

答案 4 :(得分:0)

您使用此代码

  

按钮添加;添加=(按钮)findViewById(R.id.bAdd);   add.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});

或您使用

  

类Myclass扩展了Activity实现OnClickListener {

     

按钮添加;

   protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add=(Button)findViewById(R.id.bAdd);
    add.setOnClickListener(this);
     

} public void onClick(查看v)

      {
Switch(v.getId()){
Case:R.id.bAdd{
////Write your code 
     

中断; }

     

}

     

}

     

}