将点击监听器设置为markushi / android-circle按钮

时间:2015-02-27 22:04:41

标签: android onclicklistener

我想将点击监听器设置为this circular button

我正在使用Android Studio 1.1.0并运行应用程序,但在模拟器和Nexus 5(使用API​​ 21)上都会引发异常并崩溃。

知道造成这种情况的原因是什么?

按钮位于框架内,整个屏幕是一个片段。

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=".MainActivity" tools:ignore="MergeRootFrame"    />

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:id="@+id/fra">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/infoButton"
    android:clickable="true"
    android:src="@drawable/info_button_image"
    android:contentDescription="@string/desc"
    android:adjustViewBounds="false"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Chronometer
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chronometer1"
    android:layout_alignBottom="@+id/infoButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">

    </Chronometer>


<FrameLayout
    android:layout_width="90dp"
    android:layout_height="84dp"
    android:id="@+id/start_timing_frame"
    android:layout_gravity="center_horizontal|top"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <at.markushi.ui.CircleButton
        android:layout_width="90dp"
        android:layout_height="84dp"
        android:clickable="true"
        app:cb_color="#ff99cc00"
        app:cb_pressedRingWidth="5dip"
        android:id="@+id/circleButton"
        android:layout_gravity="right|top"
        android:layout_alignBottom="@+id/infoButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:text="@string/start_timing"
        android:id="@+id/startTimingTextView"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</FrameLayout>

MainActivity类代码:

public class MainActivity extends ActionBarActivity {

private int m_isChronometerOn = 0;
private Chronometer m_contractionChronometer;
private at.markushi.ui.CircleButton m_circleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    m_contractionChronometer = (Chronometer)findViewById(R.id.chronometer1);

    m_circleButton = (at.markushi.ui.CircleButton)findViewById(R.id.circleButton);
    m_circleButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (1 == m_isChronometerOn)
            {
                StopTiming();
                m_isChronometerOn = 0;
            }

            else
            {
                // reset the timer
                SharedPreferenceManager.instance().persistTimeSpentOnLevel(0);
                StartTiming();
                m_isChronometerOn = 1;
            }
        }


    } );

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void StartTiming(){
    m_contractionChronometer.setBase(SystemClock.elapsedRealtime() +
            SharedPreferenceManager.instance().getTimeSpentOnLevel());
    m_contractionChronometer.start();
}

private void StopTiming(){

    long timeWhenStopped = m_contractionChronometer.getBase() - SystemClock.elapsedRealtime();
    SharedPreferenceManager.instance().persistTimeSpentOnLevel(timeWhenStopped);
    m_contractionChronometer.stop();
}



/**
 * 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:

java.lang.RuntimeException: Unable to start activity ComponentInfo{MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void at.markushi.ui.CircleButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void at.markushi.ui.CircleButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at MainActivity.onCreate(MainActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

1 个答案:

答案 0 :(得分:2)

您的按钮位于片段内,因此当您尝试使用findById(...)时,您将获得空指针。碎片不是在这个时刻构建的。

尝试

m_circleButton = (at.markushi.ui.CircleButton) rootView.findViewById(R.id.circleButton);
m_circleButton.setOnClickListener(new View.OnClickListener() { ... });
onCreateView()

中的