0

I don't understand why this code does not running:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_matchpage);

    SeekBar sb1 = (SeekBar) findViewById(R.id.seek1);
            sb1.setEnabled(false);

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

sb1 will be null. Why?

Kemal Duran
  • 1,478
  • 1
  • 13
  • 19
  • `seek1` is not in `activity_matchpage` layout. Probably it's in the fragment layout. http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto Jun 07 '14 at 19:15
  • do you have component with id seek1? Also what error do you get? – source.rar Jun 07 '14 at 19:15
  • no, seek1 is in matchpage. – Kemal Duran Jun 07 '14 at 19:17
  • when I run this code in button click, it runs: SeekBar sb1 = (SeekBar) findViewById(R.id.seek1); sb1.setEnabled(false); – Kemal Duran Jun 07 '14 at 19:17
  • 1
    Sow your xml code for matchpage – Alejandro Alcalde Jun 07 '14 at 19:19
  • very long page, so this is the only seekbar – Kemal Duran Jun 07 '14 at 19:24
  • **"sb1 will be null. Why?"** : There is only one possible reason why sb1 is null and that is, quite simply, `findViewById(R.id.seek1)` is unable to find a view in your current content view with that resource id. First, try using Clean to rebuild your project then try running your app again. If you still have a problem then post your logcat (which you should have done in the first place). – Squonk Jun 07 '14 at 19:45

2 Answers2

2

I will try to answer one thing from my experience that the error is coming in SeekBar sb1 = (SeekBar) findViewById(R.id.seek1); but what i have noticed android compiles code from bottom so it might happen that the reason is above or somewhere else so what i will suggest replace the code in this manner :-

  1. protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matchpage);
    }
    
  2. then if it executes then just add SeekBar sb1 = (SeekBar) findViewById(R.id.seek1); below setContentview

its bit big but since you didnt provided the xml and we cant figure out if its package issue or something else so i tried to go really primitive and fundamental...

hope it helps.

Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
Ahmad
  • 437
  • 1
  • 4
  • 12
1

You likely are using a Fragment. You should have in your code a class called PlaceHolderFragment, that has an onCreateView method.

In that project you have two xml layout, one named activity_matchpage and other for the fragment, fragment_main. You need to modify the latter and keep the first like this:

<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="youor.packaque.MainActivity"
tools:ignore="MergeRootFrame" />

And in fragment_main, you declare your Views.

<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.tformacion.finale.MainActivity$PlaceholderFragment" >

        <!-- Your Views here -->

</RelativeLayout>

Then, in your Java code, in PlaceHolderFragment, inside of onCreateView:

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

        seekBar = (seekBar) rootView.findViewById(R.id.yourSeekBarId);

    }

If you want to get rid of the fragment you simply create your XML in activity_matchpage and do not add the fragment, so, remove this:

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79