-3

I know there are a couple of questions similar to this but I've tried different things suggested such as cleaning and rebuilding and still nothing is working for me. I'm new to android and working on Eclipse. I'm trying to make a simple test app where a user clicks an image button (a ball) and there is a textview which changes text when the button is clicked.

As I'm new to android, I've noticed there is a fragment main.xml and a main.xml so I'm wondering if there is meant to be 2.

Does anyone know why this isn't running on emulator or device? Thanks for any help.

main -

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

  </menu>

fragment main -

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

   <ImageButton
    android:id="@+id/ball"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:scaleType="fitXY"
    android:src="@drawable/ball"
     />

   <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/ball"
    android:layout_marginBottom="36dp"
    android:layout_marginLeft="48dp"
    android:layout_toRightOf="@+id/ball"
    android:text="@string/click" />

   </RelativeLayout>

Main Activity - package com.example.imagebutton;

   import android.app.Activity;
   import android.app.ActionBar;
   import android.app.Fragment;
   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.Button;
   import android.widget.ImageButton;
   import android.widget.TextView;
   import android.os.Build;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton ballButton = (ImageButton) findViewById(R.id.ball);

    ballButton.setOnClickListener(new View.OnClickListener(){

        @Override

        public void onClick (View v){

        TextView text = (TextView) findViewById(R.id.tv);

        text.setText("It has been clicked!");
        }



        });
     }
 }

Manifest -

<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" >
    <activity
        android:name="com.example.imagebutton.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

  </manifest>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
coderoligist
  • 25
  • 1
  • 6
  • What is the error you are getting? Does the app not run at all or does it crash? – user1406716 Jun 10 '14 at 16:22
  • the views belong to the fragment xml. change to `setContentView(R.layout.fragment_main);` for testing. But you are encouraged to use fragments – Raghunandan Jun 10 '14 at 16:23
  • for fragments check this http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – Raghunandan Jun 10 '14 at 16:25

1 Answers1

0

This is because findViewById() searches in the activity_main layout, while the button is located in the fragment's layout fragment_main.

Move that piece of code in the onCreateView() method of the fragment:

//...
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageButton ballButton = (ImageButton) findViewById(R.id.ball);
ballButton.setOnClickListener(new View.OnClickListener(){    
        @Override    
        public void onClick (View v){    
        TextView text = (TextView) findViewById(R.id.tv);    
        text.setText("It has been clicked!");
        }            
 });
//...

Notice that now you access it through rootView view:

ImageButton ballButton = (ImageButton)rootView.findViewById(R.id.ball);

otherwise you would get again NullPointerException.

Lal
  • 14,726
  • 4
  • 45
  • 70