0

It's been a few days since I decided to build an android app on my own and it seems I need a little help. I have experience in java and programming in general , though I'm completely noob in android app development. After following several tutorials and videos, I ended up using eclipse on my Linux mint OS. The results of the java version checks :

java -version java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode) and as for the compiler ones :

javac -version
javac 1.8.0_66

The first sample "hello world" app seems to run fine on both Virtual Device and my LG. So one of the first trials is to create a simple app with 2 text fields and 2 buttons, clicking on which results in printing hey in different field. My core files are :

java file

package com.alexkarvou.proj_1;


import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
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.widget.*;



public class MainActivity extends Activity {

    EditText input;
    EditText output;
    Button one;
    ImageButton two;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input =(EditText)findViewById(R.id.editText1);
        output =(EditText)findViewById(R.id.editText2);
        one=(Button)findViewById(R.id.button1);
        two=(ImageButton)findViewById(R.id.imageButton1);

        one.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                if(v==one){

                    Context context =getApplicationContext();
                    CharSequence text ="Hey dude";
                    int duration =Toast.LENGTH_LONG;

                    final Toast toast =Toast.makeText(context,text,duration);
                    toast.show();

                }

            }

        });
        two.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                if(v==two){

                    output.setText("Hey dude");
                }

            }

        });

        }






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

fragment_main XML file

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

    <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="17dp"
        android:text="@string/Title_ref"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="60sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="32dp"
        android:ems="10" android:inputType="text">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10" android:inputType="text"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="53dp"
        android:text="@string/button_ref" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_alignTop="@+id/button1"
        android:layout_marginRight="19dp"
        android:src="@drawable/ic_launcher" android:contentDescription="@string/image_ref"/>

</RelativeLayout>

activity_main XML file

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

logcat file

11-15 16:46:48.936: I/art(1385): Not late-enabling -Xcheck:jni (already on)
11-15 16:46:48.938: I/art(1385): Late-enabling JIT
11-15 16:46:49.049: I/art(1385): JIT created with code_cache_capacity=2MB compile_threshold=1000
11-15 16:46:49.785: W/System(1385): ClassLoader referenced unknown path: /data/app/com.alexkarvou.proj_1-2/lib/arm
11-15 16:46:50.570: D/AndroidRuntime(1385): Shutting down VM
11-15 16:46:50.570: D/AndroidRuntime(1385): --------- beginning of crash
11-15 16:46:50.575: E/AndroidRuntime(1385): FATAL EXCEPTION: main
11-15 16:46:50.575: E/AndroidRuntime(1385): Process: com.alexkarvou.proj_1, PID: 1385
11-15 16:46:50.575: E/AndroidRuntime(1385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexkarvou.proj_1/com.alexkarvou.proj_1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.os.Looper.loop(Looper.java:148)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at java.lang.reflect.Method.invoke(Native Method)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-15 16:46:50.575: E/AndroidRuntime(1385): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
11-15 16:46:50.575: E/AndroidRuntime(1385):     at com.alexkarvou.proj_1.MainActivity.onCreate(MainActivity.java:35)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.Activity.performCreate(Activity.java:6237)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-15 16:46:50.575: E/AndroidRuntime(1385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-15 16:46:50.575: E/AndroidRuntime(1385):     ... 9 more

I would appreciate some help or at least some redirection as to where I should look to dribble my errors and get a grasp of the whole process. Thanks in advance.

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
  • 1
    Suggestion - Google drops support for Eclipse, you should use Android Studio for next projects. – Damian Kozlak Nov 15 '15 at 15:11
  • i switched to android studio after your comment to check if your right and my app ran fine with no problems. Dunno how i worked it out, but thanks – Alex Karvouniaris Nov 18 '15 at 15:03
  • Good to hear that. But I still think that you were trying to find component in layout, where it does not exists. Anyway, I have edited my answer, you can mark it as accepted if you want. – Damian Kozlak Nov 18 '15 at 16:10

2 Answers2

0

you have a java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

before creating your activity the View v in the buttons in null.. try first without if(v==one){:

one.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Click one!!", Toast.LENGTH_SHORT).show();
        }

    });
two.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

       Toast.makeText(getApplicationContext(), "Click two", Toast.LENGTH_SHORT).show();
        }

    });

then make sure there is an option where the view is null or try something different

Mounir Elfassi
  • 2,242
  • 3
  • 23
  • 38
0

Change

    input =(EditText)findViewById(R.id.editText1);
    output =(EditText)findViewById(R.id.editText2);
    one=(Button)findViewById(R.id.button1);
    two=(ImageButton)findViewById(R.id.imageButton1);

to

    View view = getLayoutInflater().inflate(R.layout.fragment_main, null);
    input =(EditText)view.findViewById(R.id.editText1);
    output =(EditText)view.findViewById(R.id.editText2);
    one=(Button)view.findViewById(R.id.button1);
    two=(ImageButton)view.findViewById(R.id.imageButton1);

The reason is that you are trying to find your EditText, Button and ImageButton components within activity_main.xml layout, where it belongs to fragment_main.xml.


Also suggestion - Google drops support for Eclipse, you should use Android Studio for next projects.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51