【问题标题】:Simple increment/decrement program in androidandroid中的简单递增/递减程序
【发布时间】:2014-03-24 03:52:31
【问题描述】:

我正在编写一个带有两个按钮的简单加减法程序。一个按钮增加 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>

【问题讨论】:

  • 您正在取消引用一个空指针。您正在搜索的按钮 ID 似乎不存在。您是否加载了正确的布局?如果是这样,请发布您的布局 xml
  • java.lang.NullPointerException 03-24 03:38:45.132: E/AndroidRuntime(389): at com.newboston.thefirst.MainActivity.onCreate(MainActivity.java:31)
  • 我认为问题出在 xml 文件上,你确定你已经声明了 buttonAdd 和 buttonsub,因为对于 textview 你使用了 textview1 但对于按钮你没有使用 button1 和 button2
  • 我认为 Siva 和 Gabe 可能是正确的。您必须在某处定义 buttonAdd 才能编译,但它可能不在您的布局 activity_main 中。如果 findViewById 找不到您在您正在使用的布局中指定的 id,它将返回 null。
  • @CharlieCollins,Siva,Gabe 你是对的..但如果是这种情况,那么她的代码甚至无法编译。

标签: java android eclipse android-intent


【解决方案1】:

您在这里比较的是空值,这就是抛出空指针的原因:

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 ..
}

【讨论】:

    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多