-2

这是 java 类中使用的代码。它在 eclipse 中没有显示任何错误。logcat 显示空指针异常和类种姓异常

package com.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;


public class MyfirstappActivity extends Activity {
/** Called when the activity is first created. */

int count ;
Button add,sub;
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    count = 0;
    add = (Button) findViewById(R.id.add_one);
    sub = (Button) findViewById(R.id.sub_one);
    txt = (Button) findViewById(R.id.display);
    setContentView(R.layout.main);
    add.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count = count + 1;
            txt.setText("Your total is " + count);
        }
    });

    sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count = count - 1;
            txt.setText("Your total is " + count);
        }
    }); 

}

这是创建的 main.xml 布局文件。我同时尝试清理项目。尝试使用 try catch 机制。当我评论引用上述代码部分的按钮时,应用程序运行良好

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="60dp" 
        android:text="@string/hello"
        android:id="@+id/display"
        android:textSize="40dp"
        android:gravity="center" />

   <Button
       android:text="@string/add1"
       android:layout_gravity="center" 
       android:layout_width="fill_parent"
       android:layout_height="60dp"
       android:id="@+id/add_one"
       android:textSize="40dp"
       android:gravity="center">
   </Button>

   <Button
       android:text="@string/sub1"
       android:layout_gravity="center" 
       android:layout_width="fill_parent"
       android:layout_height="60dp"
       android:id="@+id/sub_one"
       android:textSize="40dp"
       android:gravity="center">
   </Button>

</LinearLayout>
4

6 回答 6

2

改变

 txt = (Button) findViewById(R.id.display);

 txt = (TextView) findViewById(R.id.display);

因为您试图将 TextView 转换为 Button 并将其分配给 TextView 实例。setContentView(R.layout.main);在初始化 UI 元素之前也会移动 :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   //<<< set layout for Activity here
    // access UI elements here
于 2013-01-19T06:04:25.230 回答
0

请这样写...

public void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);

   setContentView(R.layout.main);
   count = 0;
   add = (Button) findViewById(R.id.add_one);
   sub = (Button) findViewById(R.id.sub_one);
   txt = (Button) findViewById(R.id.display);
}

并改变

txt = (Button) findViewById(R.id.display);

txt = (Textview) findViewById(R.id.display);
于 2013-01-19T06:04:38.647 回答
0

txtTextView,您正在将其投射到按钮。利用

txt = (TextView) findViewById(R.id.display);

您还需要为此设置布局资源。所以使用,

super.onCreate();
setContentView(R.layout.main);

编辑 -你的代码应该是这样的,

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
count = 0;
add = (Button) findViewById(R.id.add_one);
sub = (Button) findViewById(R.id.sub_one);
txt = (TextView) findViewById(R.id.display);
于 2013-01-19T06:05:21.923 回答
0

setcontentview 需要在 super.oncreate 之后编写,例如:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
于 2013-01-19T06:05:53.590 回答
0

您在初始化视图对象后设置布局,这就是为什么将空指针更改为此

super.onCreate();
setContentView(R.layout.main);
// then init the object, Here need to set layout first then find view by id
add = (Button) findViewById(R.id.add_one); 
sub = (Button) findViewById(R.id.sub_one);
txt = (TextView) findViewById(R.id.display); // here you cast to button change to TextView
于 2013-01-19T06:06:04.163 回答
0

因为您将 textview 转换为按钮,并且您还在 setcontentview 之前调用了按钮和文本视图的 ID,所以这就是您出错的原因。尝试这个 :-

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
count = 0;
add = (Button) findViewById(R.id.add_one);
sub = (Button) findViewById(R.id.sub_one);
txt = (TextView) findViewById(R.id.display);
add.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        count = count + 1;
        txt.setText("Your total is " + count);
    }
    });

sub.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        count = count - 1;
        txt.setText("Your total is " + count);
    }
}); 

}
}
于 2013-01-19T08:03:59.227 回答