0

I am working on a project and when I clicked on a button to open another activity this is the message it gave me when it crashed. I know it is probably something simple but I can't seem to find what I did wrong.

Here is the Message it is giving me when it crashed.

"java.lang.RuntimeException: Unable to start activity ComponentInfo{ualr.capstone.arkansasfloater/ualr.capstone.arkansasfloater.RiverTweet}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference"

Here is the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ualr.capstone.arkansasfloater">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".RList"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity
        android:name=".InfoActivity"
        android:parentActivityName=".RList">
    </activity>

    <activity android:name=".RiverTweet">
    </activity>
</application>

</manifest>

This is the xml Code

<?xml version="1.0" encoding="utf-8"?>
<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="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="ualr.capstone.arkansasfloater.MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Welcome! This app provides water height data for Arkansas 
  Rivers, press a button to continue"
    android:textAlignment="center"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"/>

<Button
    android:id="@+id/continuebutton"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="click"
    android:text="River Info" />

<Button
    android:id="@+id/Rtweets"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_below="@+id/continuebutton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp"
    android:text="River Tweets"/>

</RelativeLayout>

This is the Main Activity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


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


    Button welcome = (Button) findViewById(R.id.continuebutton);
    Button tweets = (Button) findViewById(R.id.Rtweets);

    welcome.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            Intent i = new Intent(v.getContext(), RList.class);
            startActivity(i);
        }



    });

    tweets.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            Intent intent = new Intent(MainActivity.this, RiverTweet.class);
            startActivity(intent);
        }



    });




}
}

This is the activity it should be opening.

import java.util.ArrayList;
import java.util.List;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuth2Token;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class RiverTweet extends Activity {
Button bigpiney;
Button buffalo;
Button ouachita;
Button white;
Button spring;
private final String TWIT_CONS_KEY = "my cons";
private final String TWIT_CONS_SEC_KEY = 
"my secret";
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spring = (Button)findViewById(R.id.spring);
    white = (Button) findViewById(R.id.white);
    ouachita = (Button) findViewById(R.id.ouachita);
    bigpiney = (Button) findViewById(R.id.bigpiney);
    buffalo = (Button) findViewById(R.id.buffalo);
    list = (ListView) findViewById(R.id.list);

    spring.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new SearchOnTwitter().execute(spring.getText().toString());
        }
    });

    white.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new SearchOnTwitter().execute(white.getText().toString());
        }
    });

    ouachita.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new SearchOnTwitter().execute(ouachita.getText().toString());
        }
    });

    bigpiney.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new SearchOnTwitter().execute(bigpiney.getText().toString());
        }
    });

    buffalo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new SearchOnTwitter().execute(buffalo.getText().toString());
        }
    });
}

class SearchOnTwitter extends AsyncTask<String, Void, Integer> {
    ArrayList<Tweet> tweets;
    final int SUCCESS = 0;
    final int FAILURE = SUCCESS + 1;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setApplicationOnlyAuthEnabled(true);
            builder.setOAuthConsumerKey(TWIT_CONS_KEY);
            builder.setOAuthConsumerSecret(TWIT_CONS_SEC_KEY);

            OAuth2Token token = new 
TwitterFactory(builder.build()).getInstance().getOAuth2Token();

            builder = new ConfigurationBuilder();
            builder.setApplicationOnlyAuthEnabled(true);
            builder.setOAuthConsumerKey(TWIT_CONS_KEY);
            builder.setOAuthConsumerSecret(TWIT_CONS_SEC_KEY);
            builder.setOAuth2TokenType(token.getTokenType());
            builder.setOAuth2AccessToken(token.getAccessToken());

            Twitter twitter = new 
TwitterFactory(builder.build()).getInstance();

            Query query = new Query(params[0]);
            // YOu can set the count of maximum records here
            query.setCount(50);
            QueryResult result;
            result = twitter.search(query);
            List<twitter4j.Status> tweets = result.getTweets();
            StringBuilder str = new StringBuilder();
            if (tweets != null) {
                this.tweets = new ArrayList<Tweet>();
                for (twitter4j.Status tweet : tweets) {
                    str.append("@" + tweet.getUser().getScreenName() + " - " 
+ tweet.getText() + "\n");
                    System.out.println(str);
                    this.tweets.add(new Tweet("@" + 
tweet.getUser().getScreenName(), tweet.getText()));
                }
                return SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return FAILURE;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        if (result == SUCCESS) {
            list.setAdapter(new TweetAdapter(RiverTweet.this, tweets));
        } else {
            Toast.makeText(RiverTweet.this, getString(R.string.error), 
Toast.LENGTH_LONG).show();
        }
    }
}
}

The first button works fine, the second one is not after I put in the code for the twitter(copy paste from another app I have built.) Thanks again for the help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
J Fletcher
  • 35
  • 9

1 Answers1

5

You are using the layout of the first activity on your second Activity (RiverTweet) :

setContentView(R.layout.activity_main);

That's the reason that your button is null when you try to find by id :)

diegoveloper
  • 93,875
  • 20
  • 236
  • 194