我正在尝试为Android开发一个简单的应用程序。我已经尝试制作一个监听器,当单击一个按钮时,该监听器将打开另一个活动。
侦听器在MainActivity上。当我将应用程序启动到模拟器中并按下按钮时,应用程序会立即崩溃。花了最后2个小时试图找出原因。
错误
09-24 09:25:17.086:E / AndroidRuntime(2445):java.lang.RuntimeException:无法启动活动ComponentInfo {com.idanofek.locationshare / com.idanofek.locationshare.SecondActivity}:java.lang。的NullPointerException
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>
有人可以帮忙吗?
答案 0 :(得分:0)
问题ActionBar actionBar = getActionBar();
请使用SecondActivity
或AppCompatActivity
扩展ActionBarActivity
以获取ActionBar。