java - Android app dev, custom numberpicker for api level 8 -
i trying create custom number picker applications require minimum of api level 8, far got code, simple don't know how fix error get.
the code far 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 xml 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 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 start activity componentinfo{com.example.symbol_temp/com.example.symbol_temp.mainactivity}: java.lang.nullpointerexception 04-14 13:31:59.222: e/androidruntime(359): @ android.app.activitythread.performlaunchactivity(activitythread.java:2663) 04-14 13:31:59.222: e/androidruntime(359): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2679) 04-14 13:31:59.222: e/androidruntime(359): @ android.app.activitythread.access$2300(activitythread.java:125) 04-14 13:31:59.222: e/androidruntime(359): @ android.app.activitythread$h.handlemessage(activitythread.java:2033) 04-14 13:31:59.222: e/androidruntime(359): @ android.os.handler.dispatchmessage(handler.java:99) 04-14 13:31:59.222: e/androidruntime(359): @ android.os.looper.loop(looper.java:123) 04-14 13:31:59.222: e/androidruntime(359): @ android.app.activitythread.main(activitythread.java:4627) 04-14 13:31:59.222: e/androidruntime(359): @ java.lang.reflect.method.invokenative(native method) 04-14 13:31:59.222: e/androidruntime(359): @ java.lang.reflect.method.invoke(method.java:521) 04-14 13:31:59.222: e/androidruntime(359): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 04-14 13:31:59.222: e/androidruntime(359): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 04-14 13:31:59.222: e/androidruntime(359): @ 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): @ com.example.symbol_temp.mainactivity.oncreate(mainactivity.java:24) 04-14 13:31:59.222: e/androidruntime(359): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 04-14 13:31:59.222: e/androidruntime(359): @ android.app.activitythread.performlaunchactivity(activitythread.java:2627) 04-14 13:31:59.222: e/androidruntime(359): ... 11 more
i think have changed few things, line number 24 :
add.setonclicklistener(new view.onclicklistener() {
thank your help.
from comment and
tools:context="com.example.symbol_temp.mainactivity$placeholderfragment"
i assume views belong fragment_main.xml
.
you inflate wrong layout. , looking view in 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;
Comments
Post a Comment