Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - has no default constructor in android manifest

My app keep crashing and it says:

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?

I have declared the activity in my android manifest file, but there's an error saying "'com.Hugler.CB.SoundClsRslt' has no default constructor "

<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>

Here is my code for 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
";
            _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 + "
";
            try {
                _fLog.write(sLog.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

How and where do I add an empty constructor? Thanks in advance! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your public SoundClsRslt(ActMain pMain) constructor is not needed, since you are not going to instantiate this class yourself (you don't instantiate Activity sub-classes explicitly), and Android is not going to use that constructor to create a new activity.

If you remove that constructor (allowing the default parameter-less constructor to be generated), or just remove its parameter, you'll have a parameter-less constructor, which is what you need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...