我创建了一个 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" />
对于为什么应用程序开始运行后立即停止工作有什么想法吗?
谢谢。
最佳答案
我想您在第 31 行左右会收到 NullPointerException
。您收到 NullPointerException
的原因是因为您试图在 ActionBarActivity
正在膨胀到您的 PlaceholderFragment
中。按如下方式更改您的代码:
// import statements omitted
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit();
}
}
// onCreateOptionsMenu() and onOptionsItemSelected()
// method remain the same.
public static class PlaceholderFragment extends Fragment {
int counter = 0;
Button add, sub;
TextView display;
public PlaceholderFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
add = (Button) rootView.findViewById(R.id.btnAdd);
sub = (Button) rootView.findViewById(R.id.btnSub);
display = (TextView) rootView.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);
}
});
return rootView;
}
}
}
关于java - Android 应用程序立即停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432072/