【问题标题】:has no default constructor in android manifest在 android manifest 中没有默认构造函数
【发布时间】:2016-01-05 07:36:21
【问题描述】:

我的应用不断崩溃,它说:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Hugler/com.Hugler.ActMain}: 
android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.Hugler.CB.SoundClsRslt}; 
have you declared this activity in your AndroidManifest.xml?

我已在我的 android 清单文件中声明了该活动,但出现错误提示“'com.Hugler.CB.SoundClsRslt' 没有默认构造函数”

<uses-feature android:name="android.hardware.usb." />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/Hugler"
    android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
    <activity
        android:name="com.Hugler.ActMain"
        android:label="@string/Hugler" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.Hugler.CB.SoundClsRslt" />
</application>

这是我的 SoundClsRslt 代码:

    package com.Hugler.CB;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.Hugler.ActMain;
import com.Hugler.TeleOp.TeleOp;
import com.SoundClassification.SoundClassification.ISC;

import android.app.Activity;
import android.content.Intent;
import android.os.Environment;
import android.telephony.SmsManager;

public class SoundClsRslt extends Activity implements ISC {

    public static int interact;

    private ActMain m_pMain;
    private static int m_iID;
    private static int m_iID1;
    private FileOutputStream _fLog;

    public SoundClsRslt(ActMain pMain) {
        m_pMain = pMain;
        m_iID = 0;
        m_iID1 = 0;
        _fLog = null;
    }

    public void StartLog(String sFile) {
        if (_fLog != null)
            return;
        try {
            _fLog = new FileOutputStream(sFile);
            String sHdr = "Time,Event,Confidence\n";
            _fLog.write(sHdr.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void OnLog(String sLog) {
    }

    public void OnLog2(String sLog) {
    }

    public void OnSegment(double[] pBuf, int iLen, String sCls, double dConf) {
        sCls = sCls.toLowerCase(Locale.getDefault());
        {
            if (sCls.contains("glass")) {

                m_iID1++;
                m_pMain.Show2(m_iID1 + "." + sCls);
                m_pMain.Show3("" + dConf);

                //convert short to byte
                ByteBuffer myByteBuffer = ByteBuffer.allocate(iLen * 2);
                myByteBuffer.order(ByteOrder.LITTLE_ENDIAN);

                //open pcm
                ShortBuffer myShortBuffer = myByteBuffer.asShortBuffer();
                for (int i = 0; i < iLen ; i++) {
                    myShortBuffer.put((short)(pBuf[i]));
                }
                File file;

                FileChannel out = null;

                try {
                    String filepath = Environment.getExternalStorageDirectory().getPath();
                    SimpleDateFormat pSDF = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.UK);
                    String sNow = pSDF.format(new Date());
                    file = new File(filepath,"AudioRecorder" + "/segment_" + sNow + ".pcm");

                    Intent intent = getIntent();

                    SimpleDateFormat smsdate = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", Locale.US);
                    String messageToSend = "Glass breaking sound detected at "+ smsdate.format(new Date()) + " (yyyy/MM/dd_HH:mm:ss)";
                    String number = intent.getExtras().getString("numberfrom");
                    SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null, null);

                    // if file doesnt exists, then create it
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    out = new FileOutputStream(file).getChannel();

                    //write to pcm
                    out.write(myByteBuffer);
                    //close pcm
                    out.close();
                } catch (IOException e) {
            e.printStackTrace();
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            } else {
            m_iID++;
            m_pMain.Show0(m_iID + "." + sCls);
                m_pMain.Show1("" + dConf);
            }
        }
        TeleOp.SendInteraction();

        if (_fLog != null) {
            SimpleDateFormat pSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
            String sNow = pSDF.format(new Date());
            String sLog = sNow + "," + sCls + "," + dConf + "\n";
            try {
                _fLog.write(sLog.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

如何以及在哪里添加一个空的构造函数?提前致谢! :)

【问题讨论】:

标签: java android android-activity android-manifest


【解决方案1】:

不需要您的public SoundClsRslt(ActMain pMain) 构造函数,因为您不会自己实例化此类(您不会显式实例化Activity 子类),并且Android 不会使用该构造函数来创建新的活动。

如果您删除该构造函数(允许生成默认的无参数构造函数),或者只是删除其参数,您将拥有一个无参数构造函数,这正是您所需要的。

【讨论】:

  • 通过删除“public SoundClsRslt(ActMain pMain)”,我的 ActMain 活动将出现错误。我已经编辑了我的问题并添加了我的 ActMain 代码。当我尝试将字符串从 ActMain 传递到 SoundClsRslt 时,会发生整个错误。
  • 不,实际上您的问题是您正在尝试使用 new 关键字实例化 Activity。那是你的问题。你不能那样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 2023-03-20
  • 2016-07-18
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
相关资源
最近更新 更多