I followed the tutorial, created a new activity through the wizard, and when I ran the OnCreate method called the Findviewbyid () method times the wrong nullpointerexception.
Layout XML (fragment_main.xml):
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= "PackageName. Mainactivity$placeholderfragment ">
<view
Android:layout_width= "100DP"
android:layout_height= "100DP"
Android:id= "@+id/something"/>
</RelativeLayout>
Processing methods
The tutorial has obviously expired. You should try to create an activity-based active UI instead of fragment based, and your code is obviously based on fragment
Refer to the following code:
1234567891011 |
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);
// not activity findViewById()
something.setOnClickListener(
new
View.OnClickListener() { ... });
return
rootView;
}
|
The Findviewbyid method is in Super.onstart () after OnCreate (). The back executes, they have order.
Original address: http://www.itmmd.com/201410/37.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.
Visit the Views OnCreate () method report NullPointerException