Android应用程序无法启动活动

时间:2017-01-16 08:28:49

标签: java android next

我想知道为什么它无法打开新的java类。 当我尝试开始新的活动时,它会崩溃。

public class MainActivity extends ActionBarActivity {
    EditText emailEt;
    TextView registerET;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        emailEt = (EditText) findViewById(R.id.email);
        registerET = (TextView) findViewById(R.id.register);
        registerET.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("clicks","You Clicked register page");
                Intent myIntent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(myIntent);
                MainActivity.this.finish();
            }
        });
    }
    public void OnLogin(View view) {
        String username = emailEt.getText().toString();
        String type = "login";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username);
    }

}

这可能表现出问题吗?我是否为活动做错了?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a20_1discussboard">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>"
<application
    android:allowBackup="true"
    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" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
    <activity android:label="@string/app_name" android:name=".RegisterActivity"/>
</manifest>

看起来可以打开新的java类,但它仍然显示MainActivity。 错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.a20_1discussboard, PID: 2194
                  java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'register'
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                      at android.view.View.performClick(View.java:4780)
                      at android.view.View$PerformClick.run(View.java:19866)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

5 个答案:

答案 0 :(得分:0)

请务必将RegisterActivity添加到AndroidManifest.xml<application>标记内:

<application ...>

    ...

    <activity
       android:label="@string/app_name"   
       android:name=".RegisterActivity"/>

</application>

答案 1 :(得分:0)

使用正确的Menifest格式:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a20_1discussboard">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>"
<application
android:allowBackup="true"
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" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:label="@string/app_name"     
          android:name=".RegisterActivity"/>   //<---here
</application>
</manifest>

请勿拨打finish(),这会导致您的崩溃:

    registerET.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("clicks","You Clicked register page");
            Intent myIntent = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(myIntent);
            //remove finish();
        }
    });

答案 2 :(得分:0)

在AndroidManifest.xml中尝试在应用程序中声明活动,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a20_1discussboard">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>"
<application
    android:allowBackup="true"
    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" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/app_name"
              android:name=".RegisterActivity"/>
</application>
</manifest>

答案 3 :(得分:0)

从onCreate方法中删除此行:MainActivity.this.finish();

如果要停止或暂停MainActivity,请使用:

@Override
public void onPause() { } 

@Override
public void onStop() { }
在MainActivity类中。

答案 4 :(得分:0)

谢谢大家 将RegisterActivity添加到UITableViewController标记内的button1并删除AndroidManifest.xml

现在正在运作。