Question

I don't know why but I can't seem to figure out what is wrong with my code. As soon as I add an onclickListener the app crashes. I really need help. Also I looked at other threads but I can't seem to find out what I'm doing wrong. Thanks The code is below:

package com.example.medrec;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
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.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements OnClickListener{

Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

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


    login = (Button)findViewById(R.id.w_login);
    //Crashes when I add this line.
    login.setOnClickListener(this);
}





@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);
}

/**
 * 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;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

My activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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.medrec.MainActivity"
tools:ignore="MergeRootFrame" />

My 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="com.example.medrec.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/w_number_edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="21dp"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="118dp"
    android:text="@string/w_number" />

<Button
    android:id="@+id/w_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/w_number_edit_text"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:text="@string/w_login_text" />

Was it helpful?

Solution

try below code:-

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);

    login = (Button)rootView.findViewById(R.id.w_login);
    //Crashes when I add this line.
    login.setOnClickListener(this);
        return rootView;
    }
}

OTHER TIPS

I'm assumging it's crashing because your login is null, the reason it's null because w_login doesn't exist in your activity layout. Try this (rootView from your fragment),

login = (Button) rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);

Your app crashed it's because your button w_login belong to your fragment_main.xml and you have to set activity_main.xml layout to your Activity.

At code

login = (Button) rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);

You should reference this Button in your Fragment onCreateView(......)

  assign on click listener in  onCreateView

   login = (Button) rootView.findViewById(R.id.w_login);
        //Crashes when I add this line.
      login.setOnClickListener(this);

Your app crashes showing NullPointerException while searching the Button in your activity_main.xml but it doesn't find there. Rather you should add the button code in your PlaceholderFragment fragment.

Delete the following line from your MainActivity

login = (Button)findViewById(R.id.w_login);
    //Crashes when I add this line.
    login.setOnClickListener(this);

Add the following to your PlaceholderFragment

public static class PlaceholderFragment extends Fragment implements OnClickListener{

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Button login = (Button)rootView.findViewById(R.id.w_login);
        login.setOnClickListener(this);
        return rootView;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top