我有两个java类,其中一个是activity类。当我在Font类中调用function GetRobotoRegularFont时,我想调用第二个类的函数,它不是activity class.but,它显示了一个错误,“由:
java.lang.NullPointerException
at com.ojaswi.font.Font.GetRobotoRegularFont(Font.java:16)
at com.ojaswi.bookingscapemob.LoginActivity.onCreate(LoginActivity.java:29)“两个java文件的..the代码is..please谁能帮我回答..
第一个Java文件的代码
public class LoginActivity extends Activity {
EditText email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
email = (EditText)findViewById(R.id.edtTextUname);
email.setTypeface(new Font().GetRobotoRegularFont());
}}
第二个Java文件的代码
public class Font {
Typeface tf;
Context myContext;
public final Typeface GetRobotoRegularFont() {
String fontPath = "fonts/Roboto-Regular.ttf";
tf = Typeface.createFromAsset(myContext.getAssets(), fontPath);
return tf;
}}
发布于 2013-03-12 18:27:07
在LoginActivity中,这样写
email.setTypeface(new Font().GetRobotoRegularFont(this));然后在Font类中
public class Font {
Typeface tf;
Context myContext;
public final Typeface GetRobotoRegularFont(Context context) {
myContext = context;
String fontPath = "fonts/Roboto-Regular.ttf";
tf = Typeface.createFromAsset(myContext.getAssets(), fontPath);
return tf;
}发布于 2013-03-12 17:36:31
您永远不会在Font类中设置上下文。
选项:
将context添加到constructor
那样内联代码
发布于 2013-03-12 17:36:34
假设该文件位于assets文件夹中,您可以执行以下操作。
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf");
email = (EditText)findViewById(R.id.edtTextUname);
email.setTypeface(tf,Typeface.BOLD);https://stackoverflow.com/questions/15357572
复制相似问题