2017-03-10 6 views
0

ボタンをクリックするとログを記録するAndroidアプリケーションを作成しようとしていますが、コンソールにテキストを挿入しますが、コンパイルして実行するとエラーが表示されます私のギャラクシーS7エッジで7.0 APIバージョン24を実行中。java.lang.RuntimeException:activitをインスタンス化できません

I've read that I need to add something into the manifest fileしかし、私は何を追加する必要があるのか​​分かりません。

致命的な例外:メイン プロセス:me.adamstephenson.test.test1、PID:16405 java.lang.RuntimeException:インスタンス化することができない活性ComponentInfo {me.adamstephenson.test.test1/me.adamstephenson.test .test1.MainActivity}:java.lang.ClassCastExceptionが:me.adamstephenson.test.test1.MainActivityはandroid.app.Activity

package me.adamstephenson.test.test1; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity { 
    public class MyActivity extends Activity { 
     protected void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 

      setContentView(R.layout.activity_main); 

      final Button button = (Button) findViewById(R.id.RequestKey); 
      button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Log.d("RequestKey", "Clicked"); 
       } 
      }); 
     } 
    } 
} 

Source

にキャストすることはできません

はここで最後にここにレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="me.adamstephenson.test.test1.MainActivity"> 

<Button 
    android:id="@+id/RequestKey" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" 
    tools:layout_editor_absoluteX="196dp" 
    tools:layout_editor_absoluteY="129dp" /> 
</android.support.constraint.ConstraintLayout> 

であるあなたは、あなたのマニフェストに間違ったクラスを参照しているマニフェストファイル

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="me.adamstephenson.test.test1"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 
+1

がマニフェスト –

+0

@NikaKurdadze確かに示してください、ちょうど記事を編集した:あなたはそれをこの適切な方法を使用することができますが、私はあなたがこの方法を選んだ理由を任意のアイデアを持っていません。 – Adam

答えて

1

です。 MyActivityはアクティビティであり、MainActivityではありません。以下に、あなたのマニフェストを変更します。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="me.adamstephenson.test.test1"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity$MyActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

そして

あなたは同様にあなたの内側の活動staticを行う必要があります

public class MainActivity { 
    public static class MyActivity extends Activity { 
     ... 
    } 
} 
+0

これを実行しようとしました: 'java.lang.RuntimeException:アクティビティをインスタンス化できませんでした。ComponentInfo {me.adamstephenson.test.test1/me.adamstephenson.test.test1.MainActivity $ MyActivity}:java.lang.InstantiationException: java.lang.Class に引数なしのコンストラクタがありません。 ' 「引数なしのコンストラクタがありません」という意味はどこですか? – Adam

+1

編集を確認してください –

+1

「無効にするキャッシュ/再起動」を押す必要がありましたが、これは機能しましたが機能しました。私は解決されたように質問をマークしました。 – Adam

1

をごアクティビティクラスは内部クラスと静的として作成されているのでクラス。

public class MyActivity extends Activity { 

    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     setContentView(R.layout.activity_main); 

     final Button button = (Button) findViewById(R.id.RequestKey); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Log.d("RequestKey", "Clicked"); 
      } 
     }); 
    } 
    . 
    . 
    . 
    // other methods or inner class can exist here 
    } 
関連する問題