找不到方法... OnClickListener错误

时间:2014-04-15 21:07:42

标签: java android eclipse

04-16 07:45:39.612:D / AndroidRuntime(362):关闭VM     04-16 07:45:39.612:W / dalvikvm(362):threadid = 1:线程退出未捕获异常(组= 0x40014760)     04-16 07:45:39.632:E / AndroidRuntime(362):致命异常:主要     04-16 07:45:39.632:E / AndroidRuntime(362):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.asd / com.example.asd.MainActivity}:java.lang.NullPointerException     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.ActivityThread.access $ 1500(ActivityThread.java:123)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:993)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.os.Handler.dispatchMessage(Handler.java:99)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.os.Looper.loop(Looper.java:126)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.ActivityThread.main(ActivityThread.java:3997)     04-16 07:45:39.632:E / AndroidRuntime(362):at java.lang.reflect.Method.invokeNative(Native Method)     04-16 07:45:39.632:E / AndroidRuntime(362):at java.lang.reflect.Method.invoke(Method.java:491)     04-16 07:45:39.632:E / AndroidRuntime(362):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)     04-16 07:45:39.632:E / AndroidRuntime(362):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)     04-16 07:45:39.632:E / AndroidRuntime(362):at dalvik.system.NativeStart.main(Native Method)     04-16 07:45:39.632:E / AndroidRuntime(362):引起:java.lang.NullPointerException     04-16 07:45:39.632:E / AndroidRuntime(362):at com.example.asd.MainActivity.onCreate(MainActivity.java:28)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)     04-16 07:45:39.632:E / AndroidRuntime(362):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)     04-16 07:45:39.632:E / AndroidRuntime(362):... 11更多

main_activity:

package com.example.asd;

import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
    int counter;
    Button add,sub;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter=1;
        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
                counter++;
                display.setText("your total is"+counter);
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("your total is"+counter);
            }
        });

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

    @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;
        }
    }

}

清单:

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

    <uses-sdk
        android:minSdkVersion="9"
        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.asd.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"  >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

它仍然有同样的问题它没有运行我需要帮助请

1 个答案:

答案 0 :(得分:1)

minSDKVersion需要至少为9,因为版本9中引入了getActivityLogo,将清单文件更新为以下内容:

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

API信息:https://developer.android.com/reference/android/content/pm/PackageManager.html#getActivityLogo%28android.content.Intent%29