Приложение основано на шаблоне прокрутки.
Это метод OnCreate:
Код: Выделить всё
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speedometer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
speedText = (TextView) findViewById(R.id.speedText);
unitText = (TextView) findViewById(R.id.unitText);
startButton = (Button) findViewById(R.id.startButton);
resetButton = (Button) findViewById(R.id.resetButton);
if (!runtimePermissions()) //Will call a method that checks Location services are permitted.
{
enableButtons();
}
}
Код: Выделить всё
private void enableButtons() //using this method will not require the use of two buttons for On/Off
{
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startClickCount++; //Counts how many time the button has been clicked
if (startClickCount % 2 == 0) //If it is even
{
Intent i = new Intent(getApplicationContext(), GPSService.class);
startService(i);
}
else
{
Intent i = new Intent(getApplicationContext(), GPSService.class);
stopService(i);
}
}
});
}
Код: Выделить всё
Код: Выделить всё
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amp.systems.performancespeedometer, PID: 4090
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amp.systems.performancespeedometer/com.amp.systems.performancespeedometer.Speedometer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.amp.systems.performancespeedometer.Speedometer.enableButtons(Speedometer.java:160)
at com.amp.systems.performancespeedometer.Speedometer.onCreate(Speedometer.java:154)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Код: Выделить всё
Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/402 ... droid-stud