这是我修改后的代码: 但是,我仍然得到同样的错误。 请帮忙,我做错了。
public class MainActivity extends ActionBarActivity {
int counter;
Button addOne;
Button subOne;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOne=(Button) findViewById(R.id.badd);
subOne= (Button) findViewById(R.id.bsub);
display =(TextView) findViewById(R.id.tv1);
counter=0;
addOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("Counter is"+ counter);
}
});
subOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("counter is "+ counter);
//display.setText("counter is"+ counter);
}
});
}
我的XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bat"
android:orientation="vertical"
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.iitb.practice.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/total"
android:textSize="35sp" >
</TextView>
<Button
android:id="@+id/bsub"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:text="@string/sub" />
<Button
android:id="@+id/badd"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:text="@string/add" />
我收到以下错误: logcat的:
05-19 03:42:58.520: D/AndroidRuntime(1281): Shutting down VM
05-19 03:42:58.520:W / dalvikvm(1281):threadid = 1:线程退出未捕获的异常(group = 0xb3a96ba8) 05-19 03:42:58.540:E / AndroidRuntime(1281):致命异常:主要 05-19 03:42:58.540:E / AndroidRuntime(1281):进程:com.iitb.practice,PID:1281 05-19 03:42:58.540:E / AndroidRuntime(1281):java.lang.RuntimeException:无法启动活动ComponentInfo {com.iitb.practice / com.iitb.practice.MainActivity}:java.lang.NullPointerException 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.os.Handler.dispatchMessage(Handler.java:102) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.os.Looper.loop(Looper.java:136) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.ActivityThread.main(ActivityThread.java:5017) 05-19 03:42:58.540:E / AndroidRuntime(1281):at java.lang.reflect.Method.invokeNative(Native Method) 05-19 03:42:58.540:E / AndroidRuntime(1281):at java.lang.reflect.Method.invoke(Method.java:515) 05-19 03:42:58.540:E / AndroidRuntime(1281):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 05-19 03:42:58.540:E / AndroidRuntime(1281):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-19 03:42:58.540:E / AndroidRuntime(1281):at dalvik.system.NativeStart.main(Native Method) 05-19 03:42:58.540:E / AndroidRuntime(1281):引起:java.lang.NullPointerException 05-19 03:42:58.540:E / AndroidRuntime(1281):at com.iitb.practice.MainActivity.onCreate(MainActivity.java:39) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.Activity.performCreate(Activity.java:5231) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 05-19 03:42:58.540:E / AndroidRuntime(1281):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 05-19 03:42:58.540:E / AndroidRuntime(1281):... 11更多 05-19 03:47:59.190:I / Process(1281):发送信号。 PID:1281 SIG:9
答案 0 :(得分:1)
在初始化变量之前,首先需要在onCreate()内部调用“setContentView()”。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOne=(Button)findViewById(R.id.badd);
subOne= (Button) findViewById(R.id.bsub);
display =(TextView) findViewById(R.id.tv1);
答案 1 :(得分:0)
使用此
更改您的oncreate起始码 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOne=(Button)findViewById(R.id.badd);
subOne= (Button) findViewById(R.id.bsub);
display =(TextView) findViewById(R.id.tv1);
......
}
您的代码存在的问题是,在扩充包含视图的布局之前,您正在查找视图。因此,您需要首先膨胀布局然后找到视图,如按钮
答案 2 :(得分:0)
在setContentView之前调用findViewById,这会使布局膨胀。这些视图直到该调用之后才会存在,因为它创建它们。同样作为一般规则想要在任何事情之前调用super.onCreate。
尝试将这两行移到顶部,如下所示。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOne=(Button)findViewById(R.id.badd);
subOne= (Button) findViewById(R.id.bsub);
display =(TextView) findViewById(R.id.tv1);
...
}