0
這是一個非常簡單的android應用程序。我剛學,現在寫爲Android的代碼,而且我不確定爲什麼行:add.setOnClickListener(新View.OnClickListener()拋出一個NullPointerException創建OnClickListener時的NullPointerException(Android)
public class StartingPoint extends ActionBarActivity{
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
counter--;
display.setText("Your total is " + 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.thenewboston.jaredh.StartingPoint$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/total"
android:textSize="40sp"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvDisplay"
android:layout_below="@+id/tvDisplay"
android:layout_gravity="center"
android:text="@string/addButton"
android:textSize="20sp"
android:id="@+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bAdd"
android:layout_below="@+id/bAdd"
android:layout_gravity="center"
android:text="@string/subButton"
android:textSize="20sp"
android:id="@+id/bSub"/>
Logcat Error:
03-29 17:44:24.930: D/AndroidRuntime(25367): Shutting down VM
03-29 17:44:24.930: W/dalvikvm(25367): threadid=1: thread exiting with uncaught exception (group=0x41831898)
03-29 17:44:24.940: E/AndroidRuntime(25367): FATAL EXCEPTION: main
03-29 17:44:24.940: E/AndroidRuntime(25367): java.lang.RuntimeException: Unable to start activity ComponentInf{com.thenewboston.jaredh/com.thenewboston.jaredh.StartingPoint}: java.lang.NullPointerException
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.access$700(ActivityThread.java:165)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.os.Looper.loop(Looper.java:137)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.main(ActivityThread.java:5455)
03-29 17:44:24.940: E/AndroidRuntime(25367): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 17:44:24.940: E/AndroidRuntime(25367): at java.lang.reflect.Method.invoke(Method.java:525)
03-29 17:44:24.940: E/AndroidRuntime(25367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-29 17:44:24.940: E/AndroidRuntime(25367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-29 17:44:24.940: E/AndroidRuntime(25367): at dalvik.system.NativeStart.main(Native Method)
03-29 17:44:24.940: E/AndroidRuntime(25367): Caused by: java.lang.NullPointerException
03-29 17:44:24.940: E/AndroidRuntime(25367): at com.thenewboston.jaredh.StartingPoint.onCreate(StartingPoint.java:39)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.Activity.performCreate(Activity.java:5372)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
03-29 17:44:24.940: E/AndroidRuntime(25367): ... 11 more
正如你可以看到這是一個非常簡單的應用程序只需加1時,無論是按鈕被點擊的文本字段。任何幫助,可以大大apprecitated。只是不清楚爲什麼NullPointerExcepti正在導致。
請發佈logcat錯誤 –
這是'activity_starting_point'的代碼嗎? –
概率你的bAdd有些問題,它不能被實例化,發佈一些logcat錯誤消息,我看不到它在哪裏下降 –