Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Eleni Minadaki
Eleni Minadaki
3,687 Points

APP HAS STOP!

Hi everyone! i try to create an pp based in interactive story app. Have already put a setMovementMetod in my textView but my app stops and can't find why. Below are my messages: if someone knows something about this please send me. Thanks! 12-23 13:32:06.400 25898-25898/com.example.eleni.interactivestory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.eleni.interactivestory, PID: 25898 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eleni.interactivestory/com.example.eleni.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference at com.example.eleni.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:39) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 Answers

Gavin Ralston
Gavin Ralston
28,770 Points

You're getting a null pointer exception, so you'll need to find out why you're looking in a place you "shouldn't" or why the collection you're looking at doesn't hold what you think it does.

Here seems to be the relevant line in the stack trace, it points to line 39 in your StoryActivity class. I'd start there.

Caused by: 
java.lang.NullPointerException: 
Attempt to invoke virtual method 
     'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' 
on a null object reference at 
     com.example.eleni.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:39) 

I broke up the lines so it wouldn't scroll ninety feet to the right in the code box. :)

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hi Gavin, thanks a lot for your reply. This sound complicated for me because i am newbie! i try to make my text scroll because is little large and that's why i put the MovementMethod.But something goes wrong with the way i wrote it or in the place i put it. i write my code below and if you can please have a look. The warnings now are: Error:(23) No resource identifier found for attribute 'max_Lines' in package 'android' Error:Execution failed for task ':app:processDebugResources'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\Programs\SdkAndroid\build-tools\23.0.2\aapt.exe'' finished with non-zero exit value 1

Thanks a lot anyway!

StoryActivity.java

public class StoryActivity extends AppCompatActivity {

public static final String TAG = StoryActivity.class.getSimpleName();


private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Button mChoice1;
private Button mChoice2;
private String mName;
private Page mCurrentPage;


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

    mTextView.setMovementMethod(new ScrollingMovementMethod());


    Intent intent = getIntent();
    mName = intent.getStringExtra(getString(R.string.key_name));

    if(mName == null){
        mName = "Friend";
    }

    Log.d(TAG, mName);

    mImageView = (ImageView)findViewById(R.id.storyImageView);
    mTextView = (TextView)findViewById(R.id.storyTextView);
    mChoice1 = (Button)findViewById(R.id.choiceButton1);
    mChoice2 = (Button)findViewById(R.id.choiceButton2);

    loadPage(0);
}
private void loadPage(int choice){
 mCurrentPage = mStory.getPage(choice);


    Drawable drawable  = getResources().getDrawable(mCurrentPage.getImageId());
    mImageView.setImageDrawable(drawable);

    String pageText = mCurrentPage.getText();
    //add the name if placeholder included.
    pageText  = String.format(pageText,mName);
    mTextView.setText(mCurrentPage.getText());

    if(mCurrentPage.isFinal()){
        mChoice1.setVisibility(View.INVISIBLE);
        mChoice2.setText("PLAY AGAIN");
        mChoice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });

    }else{

    mChoice1.setText(mCurrentPage.getChoice1().getText());
    mChoice2.setText(mCurrentPage.getChoice2().getText());

   mChoice1.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           mCurrentPage.getChoice1().getNextPage();
           int nextPage = mCurrentPage.getChoice1().getNextPage();
           loadPage(nextPage);
       }
   });

    mChoice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentPage.getChoice2().getNextPage();
            int nextPage = mCurrentPage.getChoice2().getNextPage();
            loadPage(nextPage);
        }
    });

xml file Story

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".ui.StoryActivity" tools:showIn="@layout/activity_story2" android:background="@android:color/white">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/storyImageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/page0"
    android:scaleType="fitXY"
    android:adjustViewBounds="true" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low."
    android:id="@+id/storyTextView"
    android:layout_below="@+id/storyImageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="15dp"
    android:lineSpacingMultiplier="1.2"
    android:max_Lines = "15"
    android:scrollbars = "vertical"/>
Gavin Ralston
Gavin Ralston
28,770 Points

This is saying there is nothing defined as max_Lines anywhere in your project.

Error:(23) No resource identifier found for attribute 'max_Lines' in package 'android' 

In your xml file:

android:max_Lines = "15"

if it uses an underscore, chances are it'd follow the same style as layout_width or layout_height and not be camelcased. otherwise the attribute is likely camel-cased and doesn't use underscores, like maxLines

It looks like the attribute is android:maxLines in this case.