Android - 无法开始活动
Posted
技术标签:
【中文标题】Android - 无法开始活动【英文标题】:Android - Unable to start activity 【发布时间】:2015-12-21 21:45:39 【问题描述】:我正在尝试为 android 开发一个简单的应用程序。我试图制作一个侦听器,当单击按钮时将打开另一个活动。
监听器在 MainActivity。当我将应用程序启动到模拟器中并按下按钮时,应用程序立即崩溃.. 花了最后 2 个小时试图找出原因。
错误
09-24 09:25:17.086:E/AndroidRuntime(2445):java.lang.RuntimeException:无法启动活动 ComponentInfocom.idanofek.locationshare/com.idanofek.locationshare.SecondActivity:java.lang。空指针异常
MainActivity.class
package com.idanofek.locationshare;
import android.support.v7.*;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.*;
import android.graphics.Color; /* Colors */
import android.content.res.*;
import android.util.TypedValue;
import android.text.method.*;
import android.content.*;
import android.view.*; /* OnClickListener */
import android.view.View.OnClickListener;
import android.R.id; /* Connect XML and JAVA */
public class MainActivity extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
RelativeLayout layout;
setContentView(R.layout.activity_main);
layout = (RelativeLayout)findViewById(R.id.MainLayout);
final Button loginB = (Button)findViewById(R.id.button1);
loginB.setOnClickListener(new Button.OnClickListener()
public void onClick(View v)
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
);
TextView loginT = (TextView)findViewById(R.id.textView2);
loginT.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
);
@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);
SecondActivity.class
package com.idanofek.locationshare
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SecondActivity extends Activity
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
final RelativeLayout layout;
setContentView(R.layout.second_activity);
layout = (RelativeLayout)findViewById(R.id.SecondLayout);
/* Text - email sent with password recover */
final TextView rec_text = (TextView)findViewById(R.id.textView2);
rec_text.setVisibility(View.INVISIBLE);
/* field - enter your email for recover */
final EditText mail_recover = (EditText)findViewById(R.id.mail_recover);
mail_recover.setVisibility(View.INVISIBLE);
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
TextView forgot_pass = (TextView)findViewById(R.id.forgot_pass);
forgot_pass.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stuba
mail_recover.setVisibility(View.VISIBLE);
final Button login = (Button)findViewById(R.id.button1);
RelativeLayout.LayoutParams LOGIN = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
final RelativeLayout.LayoutParams REC_TEXT = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
LOGIN.setMargins(0, 125, 0, 0);
login.setText("Recover");
login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
REC_TEXT.setMargins(0, 125, 0, 0);
rec_text.setVisibility(View.VISIBLE);
rec_text.setText("Email sent!\nCheck your email for your new password.");
);
);
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.idanofek.locationshare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />s
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
谁能帮忙?
【问题讨论】:
请分享完整的堆栈跟踪getActionBar();
可能为空
是的,用 ActionBarActivity 扩展 secondActivity,我希望它会修复。
非常感谢! ActionBar 确实返回了 null.. 现在可以工作了!
ActionBarActivity
已弃用。请改用AppCompatActivity
【参考方案1】:
这里是问题ActionBar actionBar = getActionBar();
请扩展 SecondActivity
和 AppCompatActivity
或 ActionBarActivity
以获得 ActionBar 。
【讨论】:
以上是关于Android - 无法开始活动的主要内容,如果未能解决你的问题,请参考以下文章