Android应用程序立即停止工作

时间:2014-03-16 01:54:26

标签: java android

我创建了一个Android应用程序,它具有添加按钮,减法按钮和计数器变量。

但是,只要应用程序在我的设备(Samsung Galaxy S3)上打开,我就会收到一条错误消息,指出该应用已停止工作。

下面是我的MainActivity.java文件。

package com.example.first;

import android.support.v7.app.ActionBarActivity;
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.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

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.btnAdd);
    sub = (Button) findViewById(R.id.btnSub);
    display = (TextView) findViewById(R.id.tvDisplay);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter += 1;
            display.setText("The total is " + counter);             
        }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter -= 1;
            display.setText("The 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;
    }
}

}

fragment_main.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.first.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvDisplay"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="54dp"
    android:text="@string/add_button" />

<Button
    android:id="@+id/btnSub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnAdd"
    android:layout_below="@+id/btnAdd"
    android:layout_marginTop="40dp"
    android:text="@string/sub_button" />

有没有想法为什么应用程序在开始运行后立即停止工作?

谢谢。

2 个答案:

答案 0 :(得分:1)

我认为您在第31行附近获得了NullPointerException。您获得NullPointerException的原因是因为您正试图在{{1}中查找视图{1}}正在夸大你的ActionBarActivity。按如下方式更改您的代码:

PlaceholderFragment

答案 1 :(得分:0)

删除counter = 0;并且就这样做:

add.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    counter++;
                    tv1.setText("The total is : " + String.valueOf(counter));

                }
            });


     sub.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    counter--;
                    tv1.setText("The total is : " + String.valueOf(counter));

                }
            });

希望这会有所帮助..