0

This is my home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal" >

        <ImageButton
            android:id="@+id/concerts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/concerts_style"
            android:contentDescription="@string/concerts" />
        <!-- android:onClick="gotoConcertsActivity" -->

        <ImageButton
            android:id="@+id/cinema"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/cinema_style"
            android:contentDescription="@string/cinema" />
        <!-- android:onClick="gotoCinemaActivity" -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <ImageButton
            android:id="@+id/theater"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/theater_style"
            android:contentDescription="@string/theater" />
        <!-- android:onClick="gotoTheaterActivity" -->

        <ImageButton
            android:id="@+id/cultural_events"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/cultural_style"
            android:contentDescription="@string/cultural_events" />
        <!-- android:onClick="gotoCulturalEventsActivity" -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center_horizontal" >

        <ImageButton
            android:id="@+id/food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@drawable/food_style"
            android:contentDescription="@string/food" />

        <!-- android:onClick="android:onClick="gotoFoodActivity"" -->

        <ImageButton
            android:id="@+id/sports"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/sports_style"
            android:contentDescription="@string/sports" />
        <!-- android:onClick="gotoSportsActivity"" -->
    </LinearLayout>
</LinearLayout>

This is the Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.larissaeventguide"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Splash Activity -->
        <activity
            android:name="com.example.larissaeventguide.SplashScreenWelcome"
            android:label="@string/title_activity_splash_screen_welcome" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main Activity -->

        <activity
            android:name="com.example.larissaeventguide.MainActivity"
            android:label="@string/app_name" >
        </activity>

        <!-- Concert Activity -->
        <activity android:name="com.example.larissaeventguide.ConcertsActivity" />

        <!-- Cinema Activity -->
        <activity android:name="com.example.larissaeventguide.CinemaActivity" />

        <!-- TheaterActivity -->
        <activity android:name="com.example.larissaeventguide.TheaterActivity" />

        <!-- Food Activity -->
        <activity android:name="com.example.larissaeventguide.FoodActivity" />

        <!-- CulturalEventsActivity -->
        <activity android:name="com.example.larissaeventguide.CulturalEventsActivity" />

        <!-- SportsActivity -->
        <activity android:name="com.example.larissaeventguide.SportsActivity" />

        <!-- Happens Now Activity -->
        <activity android:name="com.example.larissaeventguide.HappensNowActivity" />

        <!-- TabPagerAdapte -->
        <activity android:name=".TabPagerAdapter" />
    </application>

</manifest>

And this is my MainActivity

package com.example.larissaeventguide;
import info.LarissaEventGuide.tabsswipe.adapter.TabsPagerAdapter;
import com.example.larissaeventguide.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private static final String LOGTAG = "MainActivity";
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Home", "About", "Map" };
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(LOGTAG,"onCreate");

        //Buttons concerts
        ImageButton concerts = (ImageButton)findViewById(R.id.concerts);

        concerts.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, ConcertsActivity.class);
                startActivity(intent);

            }
        });
        //Button cinema
        ImageButton cinema = (ImageButton)findViewById(R.id.cinema);
            cinema.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, CinemaActivity.class);
                startActivity(intent);
                }
        });

        //Button Theater
        ImageButton theater = (ImageButton)findViewById(R.id.theater);
            theater.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this,TheaterActivity.class);
                startActivity(intent);

            }
        });

        //Button cultural_events
        ImageButton cultural_events = (ImageButton)findViewById(R.id.cultural_events);
            cultural_events.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, CulturalEventsActivity.class);
                startActivity(intent);
                }
        });
            //Button Food
        ImageButton food = (ImageButton)findViewById(R.id.food);

        food.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, FoodActivity.class);
                startActivity(intent);
                }
        });
            //Button Sports
        ImageButton sports = (ImageButton)findViewById(R.id.sports);
            sports.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, SportsActivity.class);
                startActivity(intent);
                }
        });

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));}
    }
        @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
        @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }
        @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        }); 

    }

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


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(LOGTAG,"onResume");
        }
        @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d(LOGTAG,"onStart");
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(LOGTAG,"onPause");
    }
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.d(LOGTAG,"onStop");
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(LOGTAG,"onDestroy");
    }
    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.d(LOGTAG,"onRestart");
    }   
}

An i get this log output

05-13 13:16:17.507: E/AndroidRuntime(1303): FATAL EXCEPTION: main
05-13 13:16:17.507: E/AndroidRuntime(1303): Process: com.example.larissaeventguide, PID: 1303
05-13 13:16:17.507: E/AndroidRuntime(1303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.larissaeventguide/com.example.larissaeventguide.MainActivity}: java.lang.NullPointerException
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.os.Looper.loop(Looper.java:136)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at dalvik.system.NativeStart.main(Native Method)
05-13 13:16:17.507: E/AndroidRuntime(1303): Caused by: java.lang.NullPointerException
05-13 13:16:17.507: E/AndroidRuntime(1303):     at com.example.larissaeventguide.MainActivity.onCreate(MainActivity.java:46)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.Activity.performCreate(Activity.java:5231)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 13:16:17.507: E/AndroidRuntime(1303):     ... 11 more

I am new to Android Programming so i follow some manuals.

After I made the imagebuttons, eclipse send me this error and I am not sure what's going on

gas
  • 13
  • 6
  • You are accessing a null object on line 46; your line numbers have been corrupted since that run, but it is probably the concerts.setOnClickListener line. Most likely, your ImageButton is not found in your layout - perhaps it is in a different one. – Chris Stratton May 13 '14 at 17:49
  • But why?i mean this is the only place where i have those buttons. – gas May 13 '14 at 17:59
  • It looks like your buttons are in home_fragment.xml, but you have set your content view to activity_main.xml – Chris Stratton May 13 '14 at 18:05
  • I tried to change that but unfortunately it didn't work – gas May 13 '14 at 18:10
  • "unfortunately it didn't work" is not a problem report we can help you with – Chris Stratton May 13 '14 at 18:10
  • I tried to put home_fragment onon content view but the same log appears.Will it help if i post the manifest.xml? – gas May 13 '14 at 18:29
  • Is your code & logs from the same version? Because the error logs complain about line 46 but when I check your code line 46 is actually ```@override``` for OnCLickEvent on cinema ImageButton. – RobGThai May 13 '14 at 19:04
  • yeap.it is my falt as i copied here the code i fixed the spaces between lines sorry about that. i will edit it – gas May 13 '14 at 19:52

3 Answers3

1

This happens because you mixed your main.xml with your fragment.xml. I think you don't know how to work with fragment first of all you see this may help

Community
  • 1
  • 1
Bhadresh
  • 115
  • 2
  • 3
  • 13
0

Looks like you're mixing up your fragments and your activity. In onCreate you're setting your layout to be R.layout.activity_main, but you're trying to get views from home_fragment.xml. It doesn't look like you're adding a fragment in the activity, so that could be your issue right there.

Were you intending to add a home fragment that contains the layout you posted above? In that case, you'll have to fetch the views and assign the click listeners in the fragment itself. If you weren't planning on adding a home fragment, you'll have to adjust your activity_main layout instead and add the buttons there.

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28
0

05-13 13:16:17.507: E/AndroidRuntime(1303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.larissaeventguide/com.example.larissaeventguide.MainActivity}: java.lang.NullPointerException................

The error comes because In your 'MainActivity' you are setting your content view to 'activity_main.xml' and defining the fields of home_fragment.xml. If you want to use the fragment layout in your activity then define a Fragment class with it's layout and inflate it like this :

Define a layout in your activity.xml file:

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/drawable_gradient_animation_list"
            android:id="@+id/spinning_wheel_image"
            tools:context=".MainActivity">

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </FrameLayout>

        </android.support.constraint.ConstraintLayout>

and Define a Fragment class HomeFragment like this:

public class HomeFragment extends Fragment {


     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.home_fragment, container, false);

        return view;

}


}

and use it in your MainActivity's onCreate() method to inflate your 'home_fragment.xml' like this :

             Fragment  fragment = new HomeFragment();
             android.support.v4.app.FragmentTransaction fragmentTransaction = 
             getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment).commit();

It will solve your problem.

Chandsi Gupta
  • 107
  • 2
  • 5