这是我的homepage.java,其中我的图像按钮位于顶部
package com.nb.list;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
public class HomePage extends Activity {
ImageButton stats;
ListView lv;
String[] info = { "Mary(Wife)", "Alex(child-1)", "Steven(child-2)",
"Robert(Father)", "Diana(Mother)" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
stats = (ImageButton) findViewById(R.id.imgstts);
stats.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), android.R.layout.simple_list_item_1,
info);
lv.setAdapter(adapter);
}
});
}
}
我的list.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
单击图像按钮时出现错误,应用停止 并在日食 它显示
10-23 17:23:30.914: E/AndroidRuntime(1296): FATAL EXCEPTION: main
10-23 17:23:30.914: E/AndroidRuntime(1296): java.lang.NullPointerException
10-23 17:23:30.914: E/AndroidRuntime(1296): at com.nb.vaultHR.HomePage$5.onClick(HomePage.java:88)
10-23 17:23:30.914: E/AndroidRuntime(1296): at android.view.View.performClick(View.java:4204)
10-23 17:23:30.914: E/AndroidRuntime(1296): at android.view.View$PerformClick.run(View.java:17355)
10-23 17:23:30.914: E/AndroidRuntime(1296): at android.os.Handler.handleCallback(Handler.java:725)
10-23 17:23:30.914: E/AndroidRuntime(1296): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 17:23:30.914: E/AndroidRuntime(1296): at android.os.Looper.loop(Looper.java:137)
10-23 17:23:30.914: E/AndroidRuntime(1296): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-23 17:23:30.914: E/AndroidRuntime(1296): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 17:23:30.914: E/AndroidRuntime(1296): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 17:23:30.914: E/AndroidRuntime(1296): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-23 17:23:30.914: E/AndroidRuntime(1296): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-23 17:23:30.914: E/AndroidRuntime(1296): at dalvik.system.NativeStart.main(Native Method)
10-23 17:23:33.303: I/Process(1296): Sending signal. PID: 1296 SIG: 9
有人可以指导我,因为我出错了,我需要做些什么才能让它发挥作用 感谢你
答案 0 :(得分:1)
你的logcat是这样说的:
java.lang.NullPointerException
at com.nb.vaultHR.HomePage$5.onClick(HomePage.java:88)
我想这一行是以下内容:
lv.setAdapter(adapter);
如果我阅读了整个代码,我可以找到ListView lv
的声明,但永远不会分配值。所以lv
永远保持为空。
您必须在lv = findViewById(android.R.id.list);
方法中添加onCreate()
之类的内容。
答案 1 :(得分:0)
您的列表视图为空。这应该这样做:
stats.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), android.R.layout.simple_list_item_1,
info);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(adapter);
}
});