我正在尝试为要求最低API级别8的应用程序创建自定义数字选择器,到目前为止,我已经获得了这段代码,这很简单,但是我不知道如何解决此错误。
到目前为止的代码是这样的:
包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);
}
});
}
}
XML是这样的:
<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>
在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
我想我已经改变了几件事,第24行是这样的:
add.setOnClickListener(new View.OnClickListener() {
感谢您的帮助。
最佳答案
从您的评论和
tools:context="com.example.symbol_temp.MainActivity$PlaceholderFragment"
我认为视图属于
fragment_main.xml
。您夸大了错误的布局。您正在寻找布局错误的视图。
改变这个
setContentView(R.layout.activity_main);
至
setContentView(R.layout.fragment_main);
也改变这个
Integer counter=0;
至
int counter; // primitive data type
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter =0;
关于java - Android应用程序开发人员,API级别8的自定义数字选择器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23061544/