【问题标题】:android- error after adding constructor to main classandroid-将构造函数添加到主类后出错
【发布时间】:2013-04-10 15:21:51
【问题描述】:

当我在我的主类中添加构造函数时,由于下面的代码,它会给出错误。我能知道为什么吗? (没有构造函数它工作正常。)

 public class AndroidGPSTrackingActivity extends Activity implements OnClickListener{

        Button btnShowLocation ;

     private final Context mContext;

            public AndroidGPSTrackingActivity(Context context) {
                    this.mContext = context.getApplicationContext();
                    //tpMethod();
                }

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


            Log.d("Main", "class");

            btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
            // show location button click event
            btnShowLocation.setOnClickListener(this); 
    }
}

错误: java.lang.RuntimeException:无法实例化活动 ComponentInfo
java.lang.InstantiationException

【问题讨论】:

  • 贴出整个logcat trace和相关代码。
  • java.lang.instantiationexception 无法实例化类没有空的构造函数
  • 为什么还要向 Activity 添加构造函数?
  • @RaghavSood 实际上我想在另一个类中调用我的主类的方法,请关注我的上一篇文章。 stackoverflow.com/questions/15926742/…

标签: android constructor runtime-error


【解决方案1】:

我认为您的活动未在清单中注册...

尝试将您的活动添加到 androidmanifest.xml

<activity
    android:name="com.xyz.Main.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>

试试这个...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2020-08-26
    • 2021-10-26
    相关资源
    最近更新 更多