Android LayoutFlator在相同语法上返回空对象引用为什么?
我从一个扩展活动的类访问布局,当我在该类中使用Android LayoutFlator在相同语法上返回空对象引用为什么?,android,Android,我从一个扩展活动的类访问布局,当我在该类中使用LayoutInflator时,它返回空对象引用,但当我在主活动中使用相同的方法时,它工作正常,请有人帮助我为什么会发生这种情况 public class MenipulateCard extends Activity{ public LinearLayout myCard( LayoutInflater myInflator = (LayoutInflater)getApplicationContext().getSystemService(C
LayoutInflator
时,它返回空对象引用,但当我在主活动中使用相同的方法时,它工作正常,请有人帮助我为什么会发生这种情况
public class MenipulateCard extends Activity{
public LinearLayout myCard(
LayoutInflater myInflator =
(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
CardView view = (CardView) myInflator.inflate(R.layout.user_layout,null);
LinearLayout ml = new LinearLayout(this);
ml.addView(view);
return ml;
}
它返回空对象引用:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
但是当我在主要活动中使用相同的方法时,我可以得到线性布局。请帮助我,朋友们改用此方法,但请确保您的上下文不是空的
View v = LayoutInflater.from(yourContext).inflate(int resource, ViewGroup root, boolean attachToRoot);
请改用此方法,但请确保您的上下文不是
null
View v = LayoutInflater.from(yourContext).inflate(int resource, ViewGroup root, boolean attachToRoot);
你试过context.getSystemServic()而不是getApplicationContext().getSystemService吗?android studio不会弹出任何上下文。getApplicationContext只会弹出getBaseContext和getApplicationContext。我的意思是。getSystemService()试着简单地说:getSystemService(context.LAYOUT\u INFLATER\u SERVICE);` java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.mudassir_ktk1.assignment/com.example.mudassir_ktk1.assignment.MainActivity}:java.lang.IllegalStateException:onCreate()之前的活动无法使用系统服务`在调用getSystemService时仅尝试了context.getSystemServic()代替getApplicationContext().getSystemService?android studio不会弹出任何上下文。只弹出getBaseContext和getApplicationContext的getApplicationContext我的意思是。getSystemService()只需尝试:getSystemService(context.LAYOUT\u INFLATER\u SERVICE);` java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.mudassir_ktk1.assignment/com.example.mudassir_ktk1.assignment.MainActivity}:java.lang.IllegalStateException:仅在调用getSystemService()时onCreate()之前的活动无法使用系统服务