[英]Android App Stops Working Immediately
I have created an android app that features an add button, a subtract button, and a counter variable.我创建了一个 android 应用程序,其中包含一个添加按钮、一个减去按钮和一个计数器变量。
However, as soon as the app opens on my device (Samsung Galaxy S3), I receive an error message stating that the app has stopped working.但是,一旦该应用程序在我的设备(三星 Galaxy S3)上打开,我就会收到一条错误消息,指出该应用程序已停止工作。
Below is my MainActivity.java file.下面是我的 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;
}
}
}
The fragment_main.xml file is below: 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" />
Are there any ideas as to why the app stops working immediately after it starts running?有没有关于为什么应用程序在开始运行后立即停止工作的想法?
Thank you.谢谢你。
I imagine that you're getting a NullPointerException
around Line 31. The reason you're getting a NullPointerException
is because you're trying to find Views in your ActionBarActivity
that are being inflated into your PlaceholderFragment
.我想你会在第 31 行附近得到
NullPointerException
。你得到NullPointerException
的原因是因为你试图在你的ActionBarActivity
中找到被膨胀到你的PlaceholderFragment
视图。 Change your code as follows:更改您的代码如下:
// 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;
}
}
}
Remove counter = 0;删除计数器 = 0; and Just Do this:
就这样做:
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));
}
});
Hope this helps..希望这可以帮助..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.