简体   繁体   English

Android-无法开始活动

[英]Android - Unable to start activity

I'm trying to develop a simple app for android. 我正在尝试为Android开发一个简单的应用程序。 I've tried to make a listener that will open another activity when a button is clicked. 我试图使一个监听器在单击按钮时将打开另一个活动。

The listener is on MainActivity. 侦听器位于MainActivity上。 When i launch the app into the emulator and press the button, the app immediately crushes.. Spent the last 2 hours trying to figure out why. 当我将应用程序启动到仿真器中并按下按钮时,应用程序立即崩溃。。花了最后2个小时试图弄清原因。

Error 错误

09-24 09:25:17.086: E/AndroidRuntime(2445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idanofek.locationshare/com.idanofek.locationshare.SecondActivity}: java.lang.NullPointerException 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 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 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 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>

Can anyone help ? 有人可以帮忙吗?

Here is issue ActionBar actionBar = getActionBar(); 这是问题ActionBar actionBar = getActionBar();

please extends SecondActivity with AppCompatActivity or ActionBarActivity to get ActionBar . 请使用AppCompatActivityActionBarActivity扩展SecondActivity以获得ActionBar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM