#30 - MemeCreator is crashing on an actual device (Nexus 5)

+4 Hashim Thaçi · December 27, 2014
/images/forum/upload/2014-12-27/2db562d77c34f8d3674527e16b4f9a2c.png

- LOGCAT -


12-27 12:43:00.871  17192-17192/com.example.hashim.memecreator I/art? Late-enabling -Xcheck:jni
12-27 12:43:01.162  17192-17212/com.example.hashim.memecreator D/OpenGLRenderer? Render dirty regions requested: true
12-27 12:43:01.170  17192-17192/com.example.hashim.memecreator D/Atlas? Validating map...
12-27 12:43:01.234  17192-17212/com.example.hashim.memecreator I/Adreno-EGL? <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
12-27 12:43:01.235  17192-17212/com.example.hashim.memecreator I/OpenGLRenderer? Initialized EGL, version 1.4
12-27 12:43:01.248  17192-17212/com.example.hashim.memecreator D/OpenGLRenderer? Enabling debug mode 0
12-27 12:43:13.378  17192-17192/com.example.hashim.memecreator D/AndroidRuntime? Shutting down VM
12-27 12:43:13.387  17192-17192/com.example.hashim.memecreator E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.hashim.memecreator, PID: 17192
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.example.hashim.memecreator.BottomPictureFragment.setMemeText(BottomPictureFragment.java:26)
            at com.example.hashim.memecreator.MainActivity.createMeme(MainActivity.java:21)
            at com.example.hashim.memecreator.TopSectionFragment.buttonClicked(TopSectionFragment.java:57)
            at com.example.hashim.memecreator.TopSectionFragment$1.onClick(TopSectionFragment.java:46)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Post a Reply

Replies

Oldest  Newest  Rating
0 Bucky Roberts · December 27, 2014
Are you getting any error messages?
0 Hashim Thaçi · December 27, 2014
Nope
App is working very well until I press the "DANK MEME BRO" button
0 Bucky Roberts · December 27, 2014
Looks like you are trying to use some variable that is null. Can you post your source code?
0 Hashim Thaçi · December 27, 2014
MainActivity.java

package com.example.hashim.memecreator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity implements TopSectionFragment.TopSectionListener {

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

    //This gets called by the Top Fragment when the user clicks the button
    @Override
    public void createMeme(String top, String bottom) {
        BottomPictureFragment bottomFragment = (BottomPictureFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        bottomFragment.setMemeText(top, bottom);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


TopSectionFragment.java

package com.example.hashim.memecreator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;

public class TopSectionFragment extends Fragment {

    private static EditText topTextInput;
    private static EditText bottomTextInput;

    TopSectionListener activityCommander;

    public interface TopSectionListener{
        public void createMeme(String top, String bottom);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            activityCommander = (TopSectionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString());
        }
    }


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

        topTextInput = (EditText) view.findViewById(R.id.topTextInput);
        bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
        final Button button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        buttonClicked(v);
                    }
                }
        );

        return view;

    }

    //Calls this when the button is clicked
    public void buttonClicked(View view) {
        activityCommander.createMeme(topTextInput.getText().toString(), bottomTextInput.getText().toString());
    }
}





BottomPictureFragment.java

package com.example.hashim.memecreator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BottomPictureFragment extends Fragment {

    private static TextView topMemeText;
    private static TextView bottomMemeText;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_picture_fragment, container, false);
        topMemeText = (TextView) view.findViewById(R.id.topTextInput);
        bottomMemeText = (TextView) view.findViewById(R.id.bottomTextInput);
        return view;

    }

    public void setMemeText(String top, String bottom) {
        topMemeText.setText(top);
        bottomMemeText.setText(bottom);
    }

}
+2 Bucky Roberts · December 27, 2014
Ok, on  BottomPictureFragment.java it looks like you are trying to change the text for the input (where the user types) and not the text on the meme.

Change
 topMemeText = (TextView) view.findViewById(R.id.topTextInput);
bottomMemeText = (TextView) view.findViewById(R.id.bottomTextInput);

to
 topMemeText = (TextView) view.findViewById(R.id.topMemeText);
 bottomMemeText = (TextView) view.findViewById(R.id.bottomMemeText);
+1 Hashim Thaçi · December 27, 2014
http://gph.is/1hGDkxd  :D:D

Keep it up, you're awesome !!
Thank YOU.
0 Paarth Tandon · January 28, 2015
I have the same exact code, but my app immediately crashes. Before the main activity is even shown. I even copy and pasted the exact same text from this post and changed what Bucky said to.
0 nagesh katke · May 1, 2016
@paarth I also have the same problem.
0 Gary Whitney · May 1, 2016
I just finished the fragment tutorials with Studio 2.0 and it all works.
I made a few mistakes along the way but it all works.
I should mention that I started with an Empty Activity and left it extend AppCompatActivity.

The only thing that is confusing is having the TopSectionFragment implement its own interface.
I spent a couple of days playing with fragments and I am pretty sure you can just have the button's onClick attribute call a local onClick method.
As long as the local method calls the interface implementation in the Main Activity.
0 Alice Densil · May 15, 2016
I made a few mistakes along the way but it all works.
  • 1

Java / Android Development

107,277 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator