Java 尝试查找dviewbyd时引发的Nullpointerexception
我有以下活动:Java 尝试查找dviewbyd时引发的Nullpointerexception,java,android,nullpointerexception,android-framelayout,Java,Android,Nullpointerexception,Android Framelayout,我有以下活动: public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) {
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StartFragment())
.commit();
}
Button login = (Button) findViewById(R.id.loginButton);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
});
}
当我尝试调用R.id.loginButton
的findViewByID
时,我得到了一个NPE,我猜这是因为loginButton
在一个单独的片段中,我有:
public static class StartFragment extends Fragment {
public StartFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
但是,我不确定如何解决这个问题,以便找到loginButton ID。我以前没有使用过片段,因此我意识到我可能没有正确地使用它们/实现它们
fragment\u main
在LinearLayout
中包含几个按钮,activity\u main
只有一个FrameLayout
尝试在fragment
中实现您的onCreateView(…)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
View something = rootView.findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... });
return rootView;
}
按钮位于片段布局(fragment\u main.xml
)中,而不在活动布局(activity\u main.xml
)中onCreate()
在生命周期中太早,无法在“活动”视图层次结构中找到它,因此会返回一个null
。在null
上调用方法会导致NPE。尝试在片段中实现onCreateView(…)
,如
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
View something = rootView.findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... });
return rootView;
}
按钮位于片段布局(fragment\u main.xml
)中,而不在活动布局(activity\u main.xml
)中onCreate()
在生命周期中太早,无法在“活动”视图层次结构中找到它,因此会返回一个null
。在null
上调用方法会导致NPE。尝试在片段中实现onCreateView(…)
,如
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
View something = rootView.findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... });
return rootView;
}
按钮位于片段布局(fragment\u main.xml
)中,而不在活动布局(activity\u main.xml
)中onCreate()
在生命周期中太早,无法在“活动”视图层次结构中找到它,因此会返回一个null
。在null
上调用方法会导致NPE。尝试在片段中实现onCreateView(…)
,如
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
View something = rootView.findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... });
return rootView;
}
按钮位于片段布局(fragment\u main.xml
)中,而不在活动布局(activity\u main.xml
)中onCreate()
在生命周期中太早,无法在“活动”视图层次结构中找到它,因此会返回一个null
。调用null
上的方法会导致NPE。从片段中编写初始化按钮的代码,因为您的按钮是在片段布局中,而不是在活动的布局中
并从活动
的onCreate
中删除登录按钮相关代码,因为您的按钮位于片段布局中,而不是活动布局中
并从活动
的onCreate
中删除登录按钮相关代码,因为您的按钮位于片段布局中,而不是活动布局中
并从活动
的onCreate
中删除登录按钮相关代码,因为您的按钮位于片段布局中,而不是活动布局中
并从活动
的onCreate
中删除登录按钮相关代码findViewById()
参照根视图工作。
如果首先没有视图,将引发空指针异常
在任何活动中,通过调用setContentView(someView)设置视图代码>。
因此,当您调用findViewById()
时,它将引用someView
。
另外,findViewById()仅当id位于该someView
中时才查找id。所以在您的例子中,空指针异常
对于片段、适配器、活动等。。。。任何视图的findViewById()
仅在id存在于视图中时才能找到
或者,如果您正在膨胀视图,则也可以使用inflatedView.findViewById()
从该inflatedView
简言之,确保您的布局中有id
,或者在适当的位置调用findViewById()
(例如适配器getView()
,活动的onCreate()
或onResume()
或onPause()
,片段onCreateView()
,…)
还应该了解UI和后台线程,因为您无法有效地更新bg线程中的UIfindViewById()
参照根视图工作。
如果首先没有视图,将引发空指针异常
在任何活动中,通过调用setContentView(someView)设置视图代码>。
因此,当您调用findViewById()
时,它将引用someView
。
另外,findViewById()仅当id位于该someView
中时才查找id。所以在您的例子中,空指针异常
对于片段、适配器、活动等。。。。任何视图的findViewById()
仅在id存在于视图中时才能找到
或者,如果您正在膨胀视图,则也可以使用inflatedView.findViewById()
从该inflatedView
简言之,确保您的布局中有id
,或者在适当的位置调用findViewById()
(例如适配器getView()
,活动的onCreate()
或onResume()
或onPause()
,片段onCreateView()
,…)
还应该了解UI和后台线程,因为您无法有效地更新bg线程中的UIfindViewById()
参照根视图工作。
如果首先没有视图,将引发空指针异常
在任何活动中,通过调用setContentView(someView)设置视图代码>。
因此,当您调用findViewById()
时,它将引用someView
。
另外,findViewById()仅在id位于