使用ListView和G calss的NullPointerException

时间:2016-03-14 12:48:53

标签: android

我做了一个G级课程:

public class G extends Application {

    public static Context                  context;
    public static LayoutInflater           inflater;
    public static Activity                 currentActivity;
    public static ArrayList<StructAnatomy> notes = new ArrayList<StructAnatomy>();


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}

并在AndroidManifest.xml中定义了G类:

<application
    android:name=".G"  //******************************** here
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:name=".Home"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Activity1"
        android:label="@string/app_name" >
    </activity>
</application>

我在Activity1类中创建了一个列表,它是activity1.xml(包含列表)。程序从Activity1启动时没有错误,但是当程序由Home Activity启动然后进入Anatomy1 Activity时,会产生错误(NullPointerException)。 该问题的解决方案是什么?

这是我的Anatomy1活动:

public class Anatomy1 extends Activity {

    public ArrayList<StructAnatomy> anatomys1 = new ArrayList<StructAnatomy>();

    public ArrayAdapter             adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anatomy1);

        ListView listAnatomy1 = (ListView) findViewById(R.id.listAnatomy1);

        adapter = new AdapterAnatomy(anatomys1);
        listAnatomy1.setAdapter(adapter);

        for (int i = 0; i < 10; i++) {
            StructAnatomy anatomy1 = new StructAnatomy();
            anatomy1.title = "ANTERIOR VIEW" + i;

            anatomys1.add(anatomy1);
        }

        adapter.notifyDataSetChanged();
    }
}

LogCat错误: 03-14 12:54:16.288:E / AndroidRuntime(1753):at m.k.Home $ 1.onTouch(Home.java:45)

家庭活动(部分代码):

anatomy1_1.setOnTouchListener(new OnTouchListener(){

    final ImageView anatomy1_1 = (ImageView) findViewById(R.id.anatomy1_1);

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            anatomy1_1.setImageResource(R.drawable.anatomy1_2);
            Intent intent = new Intent(Home.this, Anatomy1.class);
            Home.this.startActivity(intent);
            return false;
        }
    });

1 个答案:

答案 0 :(得分:1)

虽然发布的错误声明未完成因此无法进行调试,但我可以看到您正在尝试打开Anatomy1,但这未在manifest文件中定义。

在清单

中添加此内容
<activity
    android:name=".Anatomy1"
    android:label="@string/app_name" >
</activity>