2017-01-16 76 views
0

我想知道爲什麼它無法打開新的java類。 當我嘗試開始新的活動時,它會崩潰。Android應用程序未能啓動活動

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) 
+0

你得到的錯誤是什麼?你是否在'AndroidMenifest'中定義了活動? –

+0

發佈'logcat'如果你正在尋求幫助 – OBX

+0

你能找到我們的日誌貓嗎?我們可以用logcat解決它。 –

回答

0

確保您添加RegisterActivityAndroidManifest.xml<application>標籤:

<application ...> 

    ... 

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

</application> 
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(); 
     } 
    }); 
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> 
0

從onCreate方法刪除此行:MainActivity.this.finish();

如果要停止或暫停MainActivity,用途:

@Override 
public void onPause() { } 

@Override 
public void onStop() { } 
在MainActivity類別

0

謝謝大家。
AndroidManifest.xml添加RegisterActivity內<application>標記並移除MainActivity.this.finish()

它現在的工作。