我有2个微调器,基于它的组合,在按钮点击时打开一个新的xml,其中将根据微调器的组合显示图像。
基本上我面临着两个问题。
按钮onClickListner()不起作用,我评论了该功能并向前推进
image.setImageResource()无效logcat表示他无法启动活动。
那究竟我做错了什么?
这是主要的活动代码..尝试之前的一切工作完美..但是onclick listner()崩溃了代码..但是评论prg工作..
try
{
/*btn=(Button)this.findViewById(R.id.button);
System.out.println(btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {*/
if(subtxt.equals("Maths"))
{
if(Integer.parseInt(yeartxt)==1990)
{
Intent ip = new Intent(MainActivity.this, maths.class);
startActivity(ip);
}
.
//the prg executes until start activity perfectly and continues onto maths.class
.
.
这是maths.java中的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView image;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.heart_maths);
setContentView(R.layout.physics);
}
所有活动都在android mainfest中指定.. 这是日志猫
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.os.Handler.dispatchMessage(Handler.java:102)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.os.Looper.loop(Looper.java:136)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-13 00:29:20.908: E/AndroidRuntime(1485): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 00:29:20.908: E/AndroidRuntime(1485): at java.lang.reflect.Method.invoke(Method.java:515)
02-13 00:29:20.908: E/AndroidRuntime(1485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-13 00:29:20.908: E/AndroidRuntime(1485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-13 00:29:20.908: E/AndroidRuntime(1485): at dalvik.system.NativeStart.main(Native Method)
02-13 00:29:20.908: E/AndroidRuntime(1485): Caused by: java.lang.NullPointerException
02-13 00:29:20.908: E/AndroidRuntime(1485): at com.example.subjects.maths.onCreate(maths.java:16)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.Activity.performCreate(Activity.java:5231)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-13 00:29:20.908: E/AndroidRuntime(1485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-13 00:29:20.908: E/AndroidRuntime(1485): ... 11 more
那么这两个错误面临的原因究竟是什么呢? ??
编辑 答案
1) the on clicklistner() error can be avoided by declaring all variables globally in mainactivity.java
2) the image.setImageResource() should be given after setcontentview..
这就是全部。
答案 0 :(得分:2)
你错了:首先setContentView(R.layout.physics)
以及获得image
oncreate()
方法之后:
您应格式如下:
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.heart_maths);
setContentView(R.layout.physics);
将其替换为
setContentView(R.layout.physics);
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.heart_maths);
答案 1 :(得分:0)
您需要先设置视图,然后再使用资源。将行setContentView(R.layout.physics);
移到顶部。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.physics);
ImageView image;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.heart_maths);
}
答案 2 :(得分:0)
更改此代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView image;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.heart_maths);
setContentView(R.layout.physics);
}
到
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.physics);
ImageView image;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.heart_maths);
}