为什么我的应用程序暗恋? [英] Why does my App crush?
问题描述
有人能在这code点我出了错误。我学习到code和我试图从教程中,我看到了我的运行第一个应用程序,直到我发现这个消息不幸的是,新的波士顿已经停止。这是我得到的时候每次我试过一次安装后打开它的消息。我甚至尝试了三种不同的手机和我的模拟器,同样的事情发生了。我看了看我的code,我发现没有错误在code任意行。然后我试着调试。这里是整个code和分别logcat的报告。
进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.TextView;
进口android.os.Build;
公共类MainActivity扩展ActionBarActivity {
INT计数器;
按钮添加,子;
TextView的显示;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
如果(savedInstanceState == NULL){
getSupportFragmentManager()的BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())
。承诺();
添加=(按钮)findViewById(R.id.bAdd);
添加=(按钮)findViewById(R.id.sSub);
显示器=(TextView中)findViewById(R.id.tvDisplat);
add.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
反++;
display.setText(你总为+专柜);
}
});
sub.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
计数器 - ;
display.setText(你总为+专柜);
}
});
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
/ **
*包含一个简单的视图的占位符片段。
* /
公共静态类PlaceholderFragment扩展片段{
公共PlaceholderFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
返回rootView;
}
}
}
logcat的输出
07-18 21:28:21.047:E / AndroidRuntime(10601):致命异常:主要
07-18 21:28:21.047:E / AndroidRuntime(10601):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.thenewbostone / com.thenewbostone.MainActivity}:显示java.lang.NullPointerException
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.os.Handler.dispatchMessage(Handler.java:99)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.os.Looper.loop(Looper.java:123)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 21:28:21.047:E / AndroidRuntime(10601):在java.lang.reflect.Method.invokeNative(本机方法)
07-18 21:28:21.047:E / AndroidRuntime(10601):在java.lang.reflect.Method.invoke(Method.java:521)
07-18 21:28:21.047:E / AndroidRuntime(10601):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:871)
07-18 21:28:21.047:E / AndroidRuntime(10601):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
07-18 21:28:21.047:E / AndroidRuntime(10601):在dalvik.system.NativeStart.main(本机方法)
07-18 21:28:21.047:E / AndroidRuntime(10601):由:显示java.lang.NullPointerException
07-18 21:28:21.047:E / AndroidRuntime(10601):在com.thenewbostone.MainActivity.onCreate(MainActivity.java:34)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 21:28:21.047:E / AndroidRuntime(10601):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-18 21:28:21.047:E / AndroidRuntime(10601):11 ...更多
您刚才有问题,这一点,个子应该在第二行,而不是增加。
添加=(按钮)findViewById(R.id.bAdd);
添加=(按钮)findViewById(R.id.sSub);
所以,它崩溃然后在此行
sub.setOnClickListener(新View.OnClickListener(){
原因子没有初始化。
Can someone point me out the error in this code. I am learning to code and I was trying to run my first app from tutorial I saw, until I found this message "Unfortunately, The New Boston has stopped". This the message I get when every time I tried to open it after installation. I even tried on three different phone and in my Emulator and the same thing happened. I looked at my code and I found no error on any line of code. And then I tried to debug it. Here is the entire code and the logcat report respectively.
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 output
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
You just have problem with this, sub should be on the second line, not add.
add = (Button) findViewById(R.id.bAdd);
add = (Button) findViewById(R.id.sSub);
So, it crashes then on this line
sub.setOnClickListener(new View.OnClickListener() {
cause sub wasn't initialized.
这篇关于为什么我的应用程序暗恋?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!