Java 带有片段和自定义列表的NullPointerException
我正在开发一个android应用程序,我从一个例子中得到了这段代码作为列表。此页面是3页ViewPager的片段活动部分。代码:Java 带有片段和自定义列表的NullPointerException,java,android,eclipse,android-fragments,nullpointerexception,Java,Android,Eclipse,Android Fragments,Nullpointerexception,我正在开发一个android应用程序,我从一个例子中得到了这段代码作为列表。此页面是3页ViewPager的片段活动部分。代码: import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class OverviewFragment extends Fragment {
private ViewGroup mContainerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
// Initialize
mContainerView = (ViewGroup)getActivity().findViewById(R.id.container);
// Add some items
addItem();
addItem();
addItem();
addItem();
return rootView;
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu, null);
getActivity().getMenuInflater().inflate(R.menu.activity_overviewfragment, menu);
return true;
}
private void addItem() {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(
R.layout.upcoming_list_item, mContainerView, false);
// Set the text in the new row to a random country.
((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);
// Set a click listener for the "X" button in the row that will remove the row.
/**
newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Remove the row from its parent (the container view).
// Because mContainerView has android:animateLayoutChanges set to true,
// this removal is automatically animated.
mContainerView.removeView(newView);
// If there are no rows remaining, show the empty view.
if (mContainerView.getChildCount() == 0) {
findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}
}
});
*/
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 0);
}
private static final String[] UPCOMING = new String[] {
"Apartmen loan -- 200 $", "Car fix invoice -- 300 $", "Internet bill -- 70$", "dinner -- 20$"
};
}
错误:
06-23 13:57:44.440: E/AndroidRuntime(6200): FATAL EXCEPTION: main
06-23 13:57:44.440: E/AndroidRuntime(6200): Process: com.acceleratedcode.apps.myApp, PID: 6200
06-23 13:57:44.440: E/AndroidRuntime(6200): java.lang.NullPointerException
06-23 13:57:44.440: E/AndroidRuntime(6200): at com.acceleratedcode.apps.myApp.OverviewFragment.addItem(OverviewFragment.java:47)
06-23 13:57:44.440: E/AndroidRuntime(6200): at com.acceleratedcode.apps.myApp.OverviewFragment.onCreateView(OverviewFragment.java:27)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-23 13:57:44.440: E/AndroidRuntime(6200): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
错误在addItem方法的某个地方(非常确定)
Xml:
您的问题最有可能出现在这一行:
((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);
将其更改为:(假设布局中的TextView
具有id“text1”)
您引用的id已被Android占用,并且(可能)在布局文件中找不到
除此之外:
尝试使用View而不是
ViewGroup
作为“新视图”。并使用LinearLayout
而不是ViewGroup
来创建“mContainerView”。android.R.id.text1不能直接用于下面的代码中
((TextView)newView.findviewbyd(android.R.id.text1)).setText(
即将到来的[(int)(Math.random()*即将到来的.length)]代码>
你应该创建一个文本视图,然后使用它。你的概览片段的第47行到底在哪里?如果不手动计算,我怎么能找到呢?我对eclipse有点陌生:((TextView)newView.findViewById(android.R.id.text1)).setText(好的,请看Philipp Jahoda的回答,这很可能是问题所在。现在我在第27行:addItem()和第71行:mContainerView.addView(newView,0);@Philipp Jahoda仍然在这一行抛出NullPoinerException:mContainerView=(LinearLayout)getView().findViewById(R.id.container);Magic!将其第25行从getView()更改为rootView.findViewById,成功了!很好!谢谢;)
((TextView) newView.findViewById(android.R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);
((TextView) newView.findViewById(R.id.text1)).setText(
UPCOMING[(int) (Math.random() * UPCOMING.length)]);