Wednesday, April 23, 2014

android4.4 component initialization problem, null pointer exception


posts by u014041041 edited 2014-03-25 22:49:00
New android4.4.2 project's layout in the res directory becomes fragment_main.xml and activity_main.xml two files
In MainActivity, the overridden method can have, as follows:
 
public class MainActivity extends ActionBarActivity {

/ **
* A placeholder fragment containing a simple view.
* /
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment () {
}

@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
return rootView;
}
}

Button btn = null;
@ Override
protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);



if (savedInstanceState == null) {
getSupportFragmentManager (). beginTransaction ()
. Add (R.id.container, new PlaceholderFragment ()) commit ();.
}


}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {

/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

@ Override
public boolean onOptionsItemSelected (MenuItem item) {
/ / Handle action bar item clicks here. The action bar will
/ / Automatically handle clicks on the Home / Up button, so long
/ / As you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId ();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected (item);
}
}


Now I have a problem that I fragment_main.xml Button component initialization file will be reported under the null pointer in the onCreate method exception
Wrote in setContentView (R.layout.activity_main); below.
 
btn = (Button) findViewById (R.id.btn);
btn.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
Toast.makeText (MainActivity.this, "click on the button", Toast.LENGTH_LONG);
}

});

If I change my btn = (Button) findViewById (R.id.btn);'s position, change to super.onCreate (savedInstanceState); front, will be reported:
requestFeature () must be called before adding content errors.
Why is this? I should write a component initialization position where? Solving ~ ~ ~
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
lz posted the complete code
Reply:
cited a floor ysjian_pingcx reply:
lz posted the complete code

fragment_main.xml code:
 
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 = "com.example.testhello.MainActivity $ PlaceholderFragment">

android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string / hello_world" />



activity_main.xml code:
 
xmlns: tools = "http://schemas.android.com/tools"
android: id = "@ + id / container"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
tools: context = "com.example.testhello.MainActivity"
tools: ignore = "MergeRootFrame" />


MainActivity.java code:
 
package com.example.testhello;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

/ **
* A placeholder fragment containing a simple view.
* /
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment () {
}

@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
return rootView;
}
}

Button btn = null;
@ Override
protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

btn = (Button) findViewById (R.id.btn);
btn.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
Toast.makeText (MainActivity.this, "click on the button", Toast.LENGTH_LONG);
}

});

if (savedInstanceState == null) {
getSupportFragmentManager (). beginTransaction ()
. Add (R.id.container, new PlaceholderFragment ()) commit ();.
}

}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {

/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

@ Override
public boolean onOptionsItemSelected (MenuItem item) {
/ / Handle action bar item clicks here. The action bar will
/ / Automatically handle clicks on the Home / Up button, so long
/ / As you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId ();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected (item);
}
}

AndroidMainFest.xml code:
 

package = "com.example.jsonproject"
android: versionCode = "1"
android: versionName = "1.0">

android: minSdkVersion = "8"
android: targetSdkVersion = "17" />






android: allowBackup = "true"
android: icon = "@ drawable / ic_launcher"
android: label = "@ string / app_name"
android: theme = "@ style / AppTheme">
android: name = "com.example.jsonproject.MainActivity"
android: label = "@ string / app_name">










Reply:
Button button is not equal to null it ....
Reply:
I do not know lz not solve the problem, I do not have to deploy your code, I use 4.4, not ActionBarActivity, but the landlord said I should know where the problem lies
null pointer
 setContentView (R.layout.activity_main); 

btn = (Button) findViewById (R.id.btn);

Layout file in onCreate method you give ActionBarActivity settings is activity_main, and the button layout is not in this document, so here is out of reach, there is little you use the Fragment, this is the increase in Android3, and your AndroidManifest.xml file:
 android: minSdkVersion = "8" 
android: targetSdkVersion = "17" />

Should be minSdkVersion = "11",

Find the problem, lz can try to modify this implementation, in other places to go to get the Button, for instance initialization Fragment, but to call attention to the order of, I do not know lz lz can help solve the problem.
Reply:
Landlord variables you define two layouts confusion

btn = (Button) findViewById (R.id.btn);

You try to change the next button layout
Reply:
references, 4th Floor ysjian_pingcx reply:
not know lz not solve the problem, I do not have to deploy your code, I use 4.4, not ActionBarActivity, and But the landlord said I should know where the problem lies
null pointer
 setContentView (R.layout.activity_main); 

btn = (Button) findViewById (R.id.btn);

Layout file in onCreate method you give ActionBarActivity settings is activity_main, and the button layout is not in this document, so here is out of reach, there is little you use the Fragment, this is the increase in Android3, and your AndroidManifest.xml file:
 android: minSdkVersion = "8" 
android: targetSdkVersion = "17" />

Should be minSdkVersion = "11",

Find the problem, lz can try to modify this implementation, in other places to go to get the Button, for instance initialization Fragment, but to call attention to the order of, I do not know lz lz can help solve the problem.


Great God, the phrase ~ seeking advice, I would like to know which parts of the knowledge you need to know so I can understand the problem with this call sequence. Question exactly as you said, I just do not understand the configuration files and layout files, and the calling sequence MainActivity. There is how to build the project can not inherit ActionBarActivity, before the establishment of the project when the class is not inherited Activity, no two layout files. Now there are two, why?
Reply:
references, 5th Floor a1006570862 reply:
landlord layout you define two variables confusion

btn = (Button) findViewById (R.id.btn);

You try to change the layout of the next button down

Well - a lot of online search, they say that there is a call to order layout problem, but I do not know how to solve it. Button code to initialize static method written in the words that would not be an error, but the event does not respond. Continue debugging. . .
Reply:
reference to the third floor u010683904 reply:
Button button is not equal to null it ....

In fact, this does not affect.
Reply:
reference to the 6th floor u014041041 reply:
Quote: references to the 4th floor ysjian_pingcx reply:

I do not know lz not solve the problem, I do not have to deploy your code, I use 4.4, not ActionBarActivity, but the landlord said I should know where the problem lies
null pointer
 setContentView (R.layout.activity_main); 

btn = (Button) findViewById (R.id.btn);

Layout file in onCreate method you give ActionBarActivity settings is activity_main, and the button layout is not in this document, so here is out of reach, there is little you use the Fragment, this is the increase in Android3, and your AndroidManifest.xml file:
 android: minSdkVersion = "8" 
android: targetSdkVersion = "17" />

Should be minSdkVersion = "11",

Find the problem, lz can try to modify this implementation, in other places to go to get the Button, for instance initialization Fragment, but to call attention to the order of, I do not know lz lz can help solve the problem.


Great God, the phrase ~ seeking advice, I would like to know which parts of the knowledge you need to know so I can understand the problem with this call sequence. Question exactly as you said, I just do not understand the configuration files and layout files, and the calling sequence MainActivity. There is how to build the project can not inherit ActionBarActivity, before the establishment of the project when the class is not inherited Activity, no two layout files. Now there are two, why?



This is the latest ADT modify the place, the previous version of the default build of Android Activity project is inherited Activity, nor engage in so complicated, I have not updated this, did not do it, when relatively fast method recommended lz Activity by modifying a version prior to the latest version of the sdk and adt, or manually, and I hope to help you ~
Reply:
reference to the 9th floor ysjian_pingcx reply:
Quote: references to the 6th floor u014041041 reply:

Quote: 4th Floor reply ysjian_pingcx of references:

I do not know lz not solve the problem, I do not have to deploy your code, I use 4.4, not ActionBarActivity, but the landlord said I should know where the problem lies
null pointer
 setContentView (R.layout.activity_main); 

btn = (Button) findViewById (R.id.btn);

Layout file in onCreate method you give ActionBarActivity settings is activity_main, and the button layout is not in this document, so here is out of reach, there is little you use the Fragment, this is the increase in Android3, and your AndroidManifest.xml file:
 android: minSdkVersion = "8" 
android: targetSdkVersion = "17" />

Should be minSdkVersion = "11",

Find the problem, lz can try to modify this implementation, in other places to go to get the Button, for instance initialization Fragment, but to call attention to the order of, I do not know lz lz can help solve the problem.


Great God, the phrase ~ seeking advice, I would like to know which parts of the knowledge you need to know so I can understand the problem with this call sequence. Question exactly as you said, I just do not understand the configuration files and layout files, and the calling sequence MainActivity. There is how to build the project can not inherit ActionBarActivity, before the establishment of the project when the class is not inherited Activity, no two layout files. Now there are two, why?



This is the latest ADT modify the place, the previous version of the default build of Android Activity project is inherited Activity, nor engage in so complicated, I have not updated this, did not do it, when relatively fast method recommended lz Prior to the latest version with a version of the sdk and adt, or manually modify Activity, I hope to help you ~

I also would like to ask, you know, in
 public View onCreateView (LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {}

After the execution of this program also went
 
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager (). beginTransaction ()
. Add (R.id.container, new PlaceholderFragment ()) commit ();.
}
}

This code?
Because components written in fragment_main.xml years, so initialization time to write in
 setContentView (R.layout.fragment_main); 
this latter, and these components are in the id
 View rootView = inflater.inflate (R.layout.fragment_main, container, 
false);

This rootView inside
If I write that I
 setContentView (R.layout.activity_main); 

Is the corresponding "activity_main.xml" in the components of the how to initialize?
I do not know I'm asking if you can understand?
Anyway, on this post, I was really grateful to you for giving me the inspiration ~ ~ ~
Reply:
This will certainly be an error ah. Because btn is fragment_main.xml inside, but you setContentView (R.layout.activity_main); Get btn below, due activity_main.xml only one FrameLayout layout.


So get btn code to write in
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
Below:
btn = (Button) rootView.findViewById (R.id.btn);

I do not know whether lz resolved, I hope you can help.

Reply:
reference to the 11th floor xiehaibomm reply:
this certainly will complain ah. Because btn is fragment_main.xml inside, but you setContentView (R.layout.activity_main); Get btn below, due activity_main.xml only one FrameLayout layout.


So get btn code to write in
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
Below:
btn = (Button) rootView.findViewById (R.id.btn);

I do not know whether lz resolved, I hope you can help.

Solved, thank you ~
But you can tell me this load order it?
fragment_main.xml and activity_main.xml two loaded in the order in which MainActivity.
Reply:
How did you solve, I'm still stuck in this issue, I still can not find the controls
Reply:
reference to the 13th floor u012745013 reply:
How do you solve, I am now stuck in this issue, I still can not find the controls

Components written in fragment_main.xml Lane,
Initialization write
 
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment () {
}
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
Button btn = (Button) rootView.findViewById (R.id.btn);
TextView tv = (TextView) rootView.findViewById (R.id.tv01);
}

View rootView = back ......., look at this example. To add this rootView front findViewById
Reply:
How would become so troublesome after ADT update? What good is it doing? I created the first project where the error after updating the thought of it!
Reply:
reference to the 14th floor u014041041 reply:
Quote: references to the 13th floor u012745013 reply:

How did you solve, I'm still stuck in this issue, I still can not find the controls

Components written in fragment_main.xml Lane,
Initialization write
 
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment () {
}
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
Button btn = (Button) rootView.findViewById (R.id.btn);
TextView tv = (TextView) rootView.findViewById (R.id.tv01);
}

View rootView = back ......., look at this example. findViewById front to add this rootView
I am also the same problem, ask the landlord what the Settings button in the listening.
Reply:
 public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment () {
}
@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
Button bt_dial = (Button) rootView.findViewById (R.id.bt_dial);
bt_dial.setOnClickListener (new MyListener ());
EditText st_number = (EditText) rootView.findViewById (R.id.st_number);
return rootView;

}

}


findViewById not reported null pointer wrong
But there is no way to call the method defined in other EditText st_number = (EditText) rootView.findViewById (R.id.st_number);
bt_dial.setOnClickListener (new MyListener ()); there is no other way to call a method not know heroes can be difficult to solve this problem, this problem has troubled me for a long time.
Reply:
I do not know the great God has not been resolved, I also encountered the same problem here, wrote a dialer, but on the phone a test on abnormal exit, unable to perform setOnListener () method
Reply:
You use Log.i ("->", "1");
Log.i ("->", "2"); such a label to each method, and then run the program once you know that after the first load loaded.
Reply:
Get fragment_main.xml
the controls inside the static class PlaceholderFragment class MainActivity class method in onCreateView
 
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment () {
}

@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
Button btnFirstButton = (Button) rootView.findViewById (R.id.btn_firstButton);
btnFirstButton.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
Toast.makeText (getActivity (), "rootView.findViewById | fragment_main | getActivity | - #", Toast.LENGTH_LONG) show ();.
}
});
return rootView;
}
}

No comments:

Post a Comment