Android app dev, custom numberpicker for api level 8
-
26-12-2019 - |
Question
I am trying to create a custom number picker for applications that require minimum of API level 8, so far I got this code, it is simple but I don't know how to fix this error I get.
The code so far is this:
package com.example.symbol_temp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Integer counter=0;
Button add,sub;
final TextView display;
add = (Button) findViewById(R.id.plus);
sub = (Button) findViewById(R.id.minus);
display = (TextView) findViewById(R.id.showtemperature);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText( "" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
display.setText( "" + counter);
}
});
}
}
and the XML is this:
<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.symbol_temp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/showtemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/minus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/showtemperature"
android:layout_centerHorizontal="true"
android:text="+"
android:onClick="add"/>
<Button
android:id="@+id/plus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/minus"
android:layout_below="@+id/showtemperature"
android:text="-" />
</RelativeLayout>
Getting this in LogCat:
04-14 13:31:59.222: E/AndroidRuntime(359): FATAL EXCEPTION: main
04-14 13:31:59.222: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.symbol_temp/com.example.symbol_temp.MainActivity}: java.lang.NullPointerException
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.os.Looper.loop(Looper.java:123)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 13:31:59.222: E/AndroidRuntime(359): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:31:59.222: E/AndroidRuntime(359): at java.lang.reflect.Method.invoke(Method.java:521)
04-14 13:31:59.222: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 13:31:59.222: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-14 13:31:59.222: E/AndroidRuntime(359): at dalvik.system.NativeStart.main(Native Method)
04-14 13:31:59.222: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
04-14 13:31:59.222: E/AndroidRuntime(359): at com.example.symbol_temp.MainActivity.onCreate(MainActivity.java:24)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-14 13:31:59.222: E/AndroidRuntime(359): ... 11 more
I think I have changed few things, line number 24 is this :
add.setOnClickListener(new View.OnClickListener() {
Thank your for your help.
Solution
From your comment and
tools:context="com.example.symbol_temp.MainActivity$PlaceholderFragment"
I assume that the views belong to fragment_main.xml
.
You inflate the wrong layout. And you are looking for the view in the wrong layout.
Change this
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
Also change this
Integer counter=0;
to
int counter; // primitive data type
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter =0;
OTHER TIPS
Your counter variable is declared inside method thats why when you trying, move your variables above onCreate method.
NOTE:
This answer is posted as answer for coment posted by person who asked this question. I've posted this as answer for brevity
Use this method
protected void onSaveInstanceState(Bundle savedInstance) {
super.onSaveInstanceState(savedInstance);
savedInstance.putInt("myCounter",counter); }
Then in
onCreate(Bundle savedInstanceState){
if(savedInstanceState != null)
counter = savedInstanceState.getInt("myCounter")}
From savedInstance that is object of Bundle class you can retrive your saved value and assign it on create. Remember to check if value is null, because if you run your app first time your counter may not be instantiated yet and it will throw exception.
Also when you retriving savedValues make sure you don't overwrite them.