Android Studio:无法启动活动 java.lang.NullPointerException [重复]
Posted
技术标签:
【中文标题】Android Studio:无法启动活动 java.lang.NullPointerException [重复]【英文标题】:Android Studio: Unable to start activity java.lang.NullPointerException [duplicate] 【发布时间】:2018-05-13 08:27:49 【问题描述】:我正在处理一个项目,当我单击一个按钮打开另一个活动时,这是它在崩溃时给我的消息。我知道这可能很简单,但我似乎找不到我做错了什么。
这是它崩溃时给我的消息。
"java.lang.RuntimeException: Unable to start activity ComponentInfoualr.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"
这是清单
<?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>
这是xml代码
<?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_
android:layout_
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="ualr.capstone.arkansasfloater.MainActivity">
<TextView
android:layout_
android:layout_
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_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="click"
android:text="River Info" />
<Button
android:id="@+id/Rtweets"
android:layout_
android:layout_
android:layout_below="@+id/continuebutton"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:text="River Tweets"/>
</RelativeLayout>
这是主要活动
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);
);
这是它应该打开的活动。
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();
第一个按钮可以正常工作,第二个按钮不是在我为 twitter 输入代码之后(从我构建的另一个应用程序复制粘贴。)再次感谢您的帮助。
【问题讨论】:
可能activity_main
不是RiverTweet
的布局
【参考方案1】:
您正在第二个 Activity
(RiverTweet
) 上使用第一个活动的布局:
setContentView(R.layout.activity_main);
这就是当您尝试通过 id 查找时您的按钮为空的原因 :)
【讨论】:
谢谢!我总是讨厌这样简单的事情!以上是关于Android Studio:无法启动活动 java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章
无法识别启动活动:升级到 Android Studio 4.0 后未找到默认活动
Android Studio无法启动活动性ComponentInfo
Android Studio:无法启动活动 java.lang.NullPointerException [重复]