我的应用有问题。我正在尝试打开第二个活动,但它正在收到此错误
04-22 13:39:50.634: E/AndroidRuntime(1016): FATAL EXCEPTION: main
04-22 13:39:50.634: E/AndroidRuntime(1016): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finale/com.example.finale.EmployeeActivity}: java.lang.NullPointerException
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.os.Looper.loop(Looper.java:137)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-22 13:39:50.634: E/AndroidRuntime(1016): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 13:39:50.634: E/AndroidRuntime(1016): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 13:39:50.634: E/AndroidRuntime(1016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-22 13:39:50.634: E/AndroidRuntime(1016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-22 13:39:50.634: E/AndroidRuntime(1016): at dalvik.system.NativeStart.main(Native Method)
04-22 13:39:50.634: E/AndroidRuntime(1016): Caused by: java.lang.NullPointerException
04-22 13:39:50.634: E/AndroidRuntime(1016): at com.example.finale.EmployeeActivity.onCreate(EmployeeActivity.java:35)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.Activity.performCreate(Activity.java:4465)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-22 13:39:50.634: E/AndroidRuntime(1016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-22 13:39:50.634: E/AndroidRuntime(1016): ... 11 more
我的主要活动是这个
package com.example.finale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText textfeld1 = (EditText) findViewById(R.id.editText1);
final EditText textfeld2 = (EditText) findViewById(R.id.editText2);
final Button login = (Button) findViewById(R.id.button1);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(textfeld1.getText().toString().equals("employee") && textfeld2.getText().toString().equals("123"))
{
Intent intent = new Intent(MainActivity.this, EmployeeActivity.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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container,
false);
return rootView;
}
}
}
这是我的第二个活动
package com.example.finale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class EmployeeActivity extends ActionBarActivity {
int temp = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button2 = (Button) findViewById(R.id.Button01);
final Button button3 = (Button) findViewById(R.id.Button02);
final Button button4 = (Button) findViewById(R.id.Button03);
final Button button5 = (Button) findViewById(R.id.Button04);
final EditText EditText1 = (EditText) findViewById(R.id.EditText01);
final EditText EditText2 = (EditText) findViewById(R.id.EditText02);
final EditText EditText3 = (EditText) findViewById(R.id.EditText03);
final EditText EditText4 = (EditText) findViewById(R.id.EditText04);
final EditText EditText5 = (EditText) findViewById(R.id.EditText05);
final EditText EditText6 = (EditText) findViewById(R.id.EditText06);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText1.setVisibility(View.VISIBLE);
EditText2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
//Spalte 2 löschen
if(button2.getText().equals("-"))
{
if(temp == 3)
{
EditText1.setText(EditText3.getText());
EditText2.setText(EditText4.getText());
EditText3.setText(EditText5.getText());
EditText4.setText(EditText6.getText());
EditText5.setVisibility(View.INVISIBLE);
EditText6.setVisibility(View.INVISIBLE);
button4.setText("+");
}
if(temp == 2)
{
EditText1.setText(EditText3.getText());
EditText2.setText(EditText4.getText());
EditText3.setVisibility(View.INVISIBLE);
EditText4.setVisibility(View.INVISIBLE);
button3.setText("+");
button4.setVisibility(View.INVISIBLE);
}
if(temp == 1)
{
EditText1.setVisibility(View.INVISIBLE);
EditText2.setVisibility(View.INVISIBLE);
button2.setText("+");
button3.setVisibility(View.INVISIBLE);
button4.setVisibility(View.INVISIBLE);
}
temp--;
}
else
{
button2.setText("-");
temp++;
EditText1.setText("");
EditText2.setText("");
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText3.setVisibility(View.VISIBLE);
EditText4.setVisibility(View.VISIBLE);
button4.setVisibility(View.VISIBLE);
if(button3.getText().equals("-"))
{
if(temp == 3)
{
EditText3.setText(EditText5.getText());
EditText4.setText(EditText6.getText());
EditText5.setVisibility(View.INVISIBLE);
EditText6.setVisibility(View.INVISIBLE);
button4.setText("+");
}
if(temp == 2)
{
EditText3.setVisibility(View.INVISIBLE);
EditText4.setVisibility(View.INVISIBLE);
button4.setVisibility(View.INVISIBLE);
button3.setText("+");
}
temp--;
}
else
{
button3.setText("-");
temp++;
EditText3.setText("");
EditText4.setText("");
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText5.setVisibility(View.VISIBLE);
EditText6.setVisibility(View.VISIBLE);
if(button4.getText().equals("-"))
{
EditText5.setVisibility(View.INVISIBLE);
EditText6.setVisibility(View.INVISIBLE);
button4.setText("+");
temp--;
}
else
{
temp++;
button4.setText("-");
EditText5.setText("");
EditText6.setText("");
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//VERSENDEN
}
});
}
public void ActivityEintragen()
{
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main,
container, false);
return rootView;
}
}
}
我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finale"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.finale.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.finale.EmployeeActivity"
android:label="@string/title_activity_employee" >
</activity>
</application>
我尝试了很多东西但每次都会发生错误。有人知道解决方案吗?
答案 0 :(得分:1)
您在两项活动中都设置了相同的布局activity_main
。在EmployeeActivity
中,您应该在setContentView()
中拥有其他内容。尝试findViewById()
来自activity_main
布局的null
按钮导致setOnClickListener()
并在null
上调用{{1}}方法会导致NPE崩溃您的应用。