android中的简单递增/递减程序

时间:2014-03-24 03:52:31

标签: java android eclipse android-intent

我正在编写一个带有两个按钮的简单加法减法程序。一个按钮递增1,另一个递减1.如果我删除onclicklistener方法,程序工作正常。我无法弄清楚我做错了什么。

这是我的代码:

  package com.newboston.thefirst;

   import android.support.v7.app.ActionBarActivity;
   import android.support.v7.app.ActionBar;
   import android.support.v4.app.Fragment;
   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.view.ViewGroup;
   import android.widget.Button;
   import android.widget.TextView;
   import android.os.Build;

   public class MainActivity extends ActionBarActivity {

int counter;
Button add, sub;
TextView display;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter=0;
    add= (Button) findViewById(R.id.buttonAdd);
    sub= (Button) findViewById(R.id.buttonSub);
    display= (TextView) findViewById(R.id.textView1);
    add.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Your total is "+counter);
        }
    });
    sub.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Your total is "+counter);
        }
    });
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();

    }
}


     /*   @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
     }

     @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
        return super.onOptionsItemSelected(item);
    }

    /**
    * A placeholder fragment containing a simple view.
     */
   public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
   }

  }

这些是我得到的错误:

03-24 03:31:38.592: I/Process(361): Sending signal. PID: 361 SIG: 9

03-24 03:38:44.952: I/dalvikvm(389): Could not find method 

android.content.pm.PackageManager.getActivityLogo, referenced from method 

android.support.v7.internal.widget.ActionBarView.<init>

03-24 03:38:44.952: W/dalvikvm(389): VFY: unable to resolve virtual method 312: 

Landroid/content/pm/PackageManager;.getActivityLogo 

(Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;

03-24 03:38:44.952: D/dalvikvm(389): VFY: replacing opcode 0x6e at 0x008b

03-24 03:38:44.962: I/dalvikvm(389): Could not find method 

android.content.pm.ApplicationInfo.loadLogo, referenced from method 

android.support.v7.internal.widget.ActionBarView.<init>

03-24 03:38:44.962: W/dalvikvm(389): VFY: unable to resolve virtual method 308: 

Landroid/content/pm/ApplicationInfo;.loadLogo 

(Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;

03-24 03:38:44.962: D/dalvikvm(389): VFY: replacing opcode 0x6e at 0x0099

03-24 03:38:44.972: D/dalvikvm(389): VFY: dead code 0x008e-0092 in 

Landroid/support/v7/internal/widget/ActionBarView;.<init> 

(Landroid/content/Context;Landroid/util/AttributeSet;)V

03-24 03:38:44.972: D/dalvikvm(389): VFY: dead code 0x009c-00a0 in 

Landroid/support/v7/internal/widget/ActionBarView;.<init> 

(Landroid/content/Context;Landroid/util/AttributeSet;)V

03-24 03:38:45.122: D/AndroidRuntime(389): Shutting down VM

03-24 03:38:45.122: W/dalvikvm(389): threadid=1: thread exiting with uncaught exception 

(group=0x4001d800)

03-24 03:38:45.132: E/AndroidRuntime(389): FATAL EXCEPTION: main

03-24 03:38:45.132: E/AndroidRuntime(389): java.lang.RuntimeException: Unable to start 

activity ComponentInfo{com.newboston.thefirst/com.newboston.thefirst.MainActivity}: 

java.lang.NullPointerException

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.ActivityThread.access$2300(ActivityThread.java:125)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.os.Handler.dispatchMessage(Handler.java:99)

03-24 03:38:45.132: E/AndroidRuntime(389):  at android.os.Looper.loop(Looper.java:123)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.ActivityThread.main(ActivityThread.java:4627)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

java.lang.reflect.Method.invokeNative(Native Method)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

java.lang.reflect.Method.invoke(Method.java:521)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

03-24 03:38:45.132: E/AndroidRuntime(389):  at dalvik.system.NativeStart.main(Native 

Method)

03-24 03:38:45.132: E/AndroidRuntime(389): Caused by: java.lang.NullPointerException

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

com.newboston.thefirst.MainActivity.onCreate(MainActivity.java:31)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

03-24 03:38:45.132: E/AndroidRuntime(389):  at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

03-24 03:38:45.132: E/AndroidRuntime(389):  ... 11 more

修改

XML代码

    <?xml version="1.0" encoding="utf-8"?>
   <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.newboston.thefirst.MainActivity$PlaceholderFragment" >

    <TextView
      android:id="@+id/textView1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Your total is 0"
      android:textSize="30sp"
      android:gravity="center"
     />
    <Button
      android:layout_width="200sp"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="71dp"
      android:gravity="center"
      android:text="Subtract one"
      android:id="@+id/buttonSub" />

    <Button
       android:layout_width="200sp"
       android:layout_height="wrap_content"
       android:layout_below="@+id/textView1"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="110dp"
       android:gravity="center"
       android:text="Add one"
       android:id="@+id/buttonAdd" />

  </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你正在比较一个空值,这就是为什么抛出空指针的原因:

if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();

    }

你应该试试

if(savedInstanceState != null){
// leave it blank if you don't intend 
// to do anything if savedInstanceState is not null
}else{
 // your codes here ..
}