我在我的Android应用程序中有imageview我使用图像视图作为按钮当我按下图像视图按钮。它的图像应该被更改。现在我得到错误,请找到我使用的下面的代码。
package com.example.tesfragement;
import com.maintab.CompareNow;
import com.maintab.Login;
import com.maintab.Search;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import com.maintab.*;
public class MainActivity extends Activity {
Fragment fragement;
ImageView loginbtn,comparebtn,searchbtn;
Resources res = getResources();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
loginbtn=(ImageView)findViewById(R.id.login_button);
comparebtn=(ImageView)findViewById(R.id.comapre_now_button);
searchbtn=(ImageView)findViewById(R.id.search_button);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
StartFragment myfragement=new StartFragment();
ft.add(R.id.myFragement, myfragement);
ft.commit();
loginbtn.setOnClickListener(btnOnClickListener);
comparebtn.setOnClickListener(btnOnClickListener);
searchbtn.setOnClickListener(btnOnClickListener);
//here change images according to touch
loginbtn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
//
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
((ImageView)v).setImageResource(R.drawable.compare_now_click);
break;
}
case MotionEvent.ACTION_CANCEL:{
((ImageView)v).setImageResource(R.drawable.compare_now_click);
break;
}
}
return true;
}
});
}
ImageView.OnClickListener btnOnClickListener=new ImageView.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment newFragment;
if(v==loginbtn)
{
newFragment=new Login();
}
else if(v==comparebtn)
{
newFragment=new CompareNow();
}
else if(v==searchbtn)
{
newFragment=new Search();
}
else
newFragment=new StartFragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.myFragement,newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
};
// ImageView.setOnTouchListener btnOntouchListner=new ImageView.setOnTouchListener();
}
Logcat 请在我的logcat下面找到
02-17 13:46:18.368: E/AndroidRuntime(6883): FATAL EXCEPTION: main
02-17 13:46:18.368: E/AndroidRuntime(6883): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tesfragement/com.example.tesfragement.MainActivity}: java.lang.NullPointerException
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.os.Looper.loop(Looper.java:137)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.ActivityThread.main(ActivityThread.java:4511)
02-17 13:46:18.368: E/AndroidRuntime(6883): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 13:46:18.368: E/AndroidRuntime(6883): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 13:46:18.368: E/AndroidRuntime(6883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-17 13:46:18.368: E/AndroidRuntime(6883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-17 13:46:18.368: E/AndroidRuntime(6883): at dalvik.system.NativeStart.main(Native Method)
02-17 13:46:18.368: E/AndroidRuntime(6883): Caused by: java.lang.NullPointerException
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
02-17 13:46:18.368: E/AndroidRuntime(6883): at com.example.tesfragement.MainActivity.<init>(MainActivity.java:29)
02-17 13:46:18.368: E/AndroidRuntime(6883): at java.lang.Class.newInstanceImpl(Native Method)
02-17 13:46:18.368: E/AndroidRuntime(6883): at java.lang.Class.newInstance(Class.java:1319)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
02-17 13:46:18.368: E/AndroidRuntime(6883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
02-17 13:46:18.368: E/AndroidRuntime(6883): ... 11 more
02-17 13:51:33.133: D/AndroidRuntime(7457): Shutting down VM
02-17 13:51:33.133: W/dalvikvm(7457): threadid=1: thread exiting with uncaught exception (group=0x40c491f8)
02-17 13:51:33.138: E/AndroidRuntime(7457): FATAL EXCEPTION: main
02-17 13:51:33.138: E/AndroidRuntime(7457): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tesfragement/com.example.tesfragement.MainActivity}: java.lang.NullPointerException
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.os.Looper.loop(Looper.java:137)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.ActivityThread.main(ActivityThread.java:4511)
02-17 13:51:33.138: E/AndroidRuntime(7457): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 13:51:33.138: E/AndroidRuntime(7457): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 13:51:33.138: E/AndroidRuntime(7457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-17 13:51:33.138: E/AndroidRuntime(7457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-17 13:51:33.138: E/AndroidRuntime(7457): at dalvik.system.NativeStart.main(Native Method)
02-17 13:51:33.138: E/AndroidRuntime(7457): Caused by: java.lang.NullPointerException
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
02-17 13:51:33.138: E/AndroidRuntime(7457): at com.example.tesfragement.MainActivity.<init>(MainActivity.java:29)
02-17 13:51:33.138: E/AndroidRuntime(7457): at java.lang.Class.newInstanceImpl(Native Method)
02-17 13:51:33.138: E/AndroidRuntime(7457): at java.lang.Class.newInstance(Class.java:1319)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
02-17 13:51:33.138: E/AndroidRuntime(7457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
02-17 13:51:33.138: E/AndroidRuntime(7457): ... 11 more
答案 0 :(得分:4)
getResources();
仅适用于onCreate()
之后或之内,因为Activity
之前没有Context
。
在Resources
中填写onCreate()
变量:
...
Resources res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
res = getResources();
...
答案 1 :(得分:4)
将其移至onCreate
Resources res = getResources();
getResources()
需要Context
。创建活动后可以使用Context
参考:
http://developer.android.com/reference/android/content/Context.html#getResources()
编辑:
发表在帖子下方的评论
Resources res; //declare as instance variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
res = getResources(); // initialize
答案 2 :(得分:1)
在res
方法内初始化您的onCreate()
...
Resources res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
res = getResources();
}
替换此行...
((ImageView)v).setImageResource(R.drawable.compare_now_click);
这个....
((ImageView)v).setImageDrawable(res.getDrawable(R.drawable.compare_now_click));