Tuesday, 17 September 2013

Android Fragments: Immediate Null Point Exception

Android Fragments: Immediate Null Point Exception

I know this information is all over the internet, but I can't for the life
of me figure out what I'm doing wrong. When I try to run my code it
crashes immediately with a null pointer exception, but I can't seem to
figure out why that is happening or where it is happening. I imagine I am
missing something fairly simple, but I am new to android so I don't know
what it is. Here is the code:
MainActivity:
public class MainActivity extends Activity implements
GetConnInfoFragment.ConnInfoReceiver {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
GetConnInfoFragment frag = new GetConnInfoFragment();
fragmentTransaction.add(R.id.mainFragmentContainer, frag);
fragmentTransaction.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;
}
....
GetConnInfoFragment:
public class GetConnInfoFragment extends Fragment {
ConnInfoReceiver ownerActivity;
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button goButton = (Button) getView().findViewById(R.id.connectButton);
goButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText portBox = (EditText) v.findViewById(R.id.portBox);
int port = Integer.parseInt(portBox.getText().toString());
EditText ipBox = (EditText) v.findViewById(R.id.ipAddressBox);
String address = ipBox.getText().toString();
// create sockets, might these be garbage collected ?
// in which case the main activity should create them. That makes
// more sense anyway
// activate next fragment -- call back up to the main activity
to trigger this
ownerActivity.receiveConnInfo(port, address);
}
});
return inflater.inflate(R.layout.getconninfofragment, container, false);
}
Here is the error message:
E/AndroidRuntime(1470): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.htpccontrol/com.htpccontrol.MainActivity}:
java.lang.NullPointerException

No comments:

Post a Comment