有人可以在此代码中指出错误。我正在学习编码,我正试图从我看到的教程中运行我的第一个应用程序,直到我发现这个消息"不幸的是,新波士顿已停止"。这是我每次尝试在安装后打开它时得到的消息。我甚至试过三种不同的手机和我的模拟器,同样的事情发生了。我查看了我的代码,发现任何代码行都没有错误。然后我尝试调试它。这里分别是整个代码和logcat报告。
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.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);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
add = (Button) findViewById(R.id.bAdd);
add = (Button) findViewById(R.id.sSub);
display = (TextView) findViewById(R.id.tvDisplat);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
display.setText("Your total is" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
display.setText("Your total is" + counter);
}
});
}
}
@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;
}
}
}
Logcat输出
07-18 21:28:21.047: E/AndroidRuntime(10601): FATAL EXCEPTION: main
07-18 21:28:21.047: E/AndroidRuntime(10601): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewbostone/com.thenewbostone.MainActivity}: java.lang.NullPointerException
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.os.Looper.loop(Looper.java:123)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 21:28:21.047: E/AndroidRuntime(10601): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 21:28:21.047: E/AndroidRuntime(10601): at java.lang.reflect.Method.invoke(Method.java:521)
07-18 21:28:21.047: E/AndroidRuntime(10601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
07-18 21:28:21.047: E/AndroidRuntime(10601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
07-18 21:28:21.047: E/AndroidRuntime(10601): at dalvik.system.NativeStart.main(Native Method)
07-18 21:28:21.047: E/AndroidRuntime(10601): Caused by: java.lang.NullPointerException
07-18 21:28:21.047: E/AndroidRuntime(10601): at com.thenewbostone.MainActivity.onCreate(MainActivity.java:34)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 21:28:21.047: E/AndroidRuntime(10601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-18 21:28:21.047: E/AndroidRuntime(10601): ... 11 more
答案 0 :(得分:1)
你遇到问题,sub应该在第二行,而不是添加。
add = (Button) findViewById(R.id.bAdd);
add = (Button) findViewById(R.id.sSub);
所以,它在这一行崩溃了
sub.setOnClickListener(new View.OnClickListener() {
原因sub未初始化。